diff --git a/Pipfile b/Pipfile new file mode 100644 index 00000000..9141d17f --- /dev/null +++ b/Pipfile @@ -0,0 +1,14 @@ +[[source]] +name = "pypi" +url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] + +[packages] +python-dotenv = "*" +psycopg2-binary = "*" +pandas = "*" + +[requires] +python_version = "3.7" diff --git a/Pipfile.lock b/Pipfile.lock new file mode 100644 index 00000000..9005a1bb --- /dev/null +++ b/Pipfile.lock @@ -0,0 +1,134 @@ +{ + "_meta": { + "hash": { + "sha256": "60518732f4d1b8cd1642f16e6772ec4dfe41de6d77660cb1e0821adfaa94ec33" + }, + "pipfile-spec": 6, + "requires": { + "python_version": "3.7" + }, + "sources": [ + { + "name": "pypi", + "url": "https://pypi.org/simple", + "verify_ssl": true + } + ] + }, + "default": { + "numpy": { + "hashes": [ + "sha256:0172304e7d8d40e9e49553901903dc5f5a49a703363ed756796f5808a06fc233", + "sha256:34e96e9dae65c4839bd80012023aadd6ee2ccb73ce7fdf3074c62f301e63120b", + "sha256:3676abe3d621fc467c4c1469ee11e395c82b2d6b5463a9454e37fe9da07cd0d7", + "sha256:3dd6823d3e04b5f223e3e265b4a1eae15f104f4366edd409e5a5e413a98f911f", + "sha256:4064f53d4cce69e9ac613256dc2162e56f20a4e2d2086b1956dd2fcf77b7fac5", + "sha256:4674f7d27a6c1c52a4d1aa5f0881f1eff840d2206989bae6acb1c7668c02ebfb", + "sha256:7d42ab8cedd175b5ebcb39b5208b25ba104842489ed59fbb29356f671ac93583", + "sha256:965df25449305092b23d5145b9bdaeb0149b6e41a77a7d728b1644b3c99277c1", + "sha256:9c9d6531bc1886454f44aa8f809268bc481295cf9740827254f53c30104f074a", + "sha256:a78e438db8ec26d5d9d0e584b27ef25c7afa5a182d1bf4d05e313d2d6d515271", + "sha256:a7acefddf994af1aeba05bbbafe4ba983a187079f125146dc5859e6d817df824", + "sha256:a87f59508c2b7ceb8631c20630118cc546f1f815e034193dc72390db038a5cb3", + "sha256:ac792b385d81151bae2a5a8adb2b88261ceb4976dbfaaad9ce3a200e036753dc", + "sha256:b03b2c0badeb606d1232e5f78852c102c0a7989d3a534b3129e7856a52f3d161", + "sha256:b39321f1a74d1f9183bf1638a745b4fd6fe80efbb1f6b32b932a588b4bc7695f", + "sha256:cae14a01a159b1ed91a324722d746523ec757357260c6804d11d6147a9e53e3f", + "sha256:cd49930af1d1e49a812d987c2620ee63965b619257bd76eaaa95870ca08837cf", + "sha256:e15b382603c58f24265c9c931c9a45eebf44fe2e6b4eaedbb0d025ab3255228b", + "sha256:e91d31b34fc7c2c8f756b4e902f901f856ae53a93399368d9a0dc7be17ed2ca0", + "sha256:ef627986941b5edd1ed74ba89ca43196ed197f1a206a3f18cc9faf2fb84fd675", + "sha256:f718a7949d1c4f622ff548c572e0c03440b49b9531ff00e4ed5738b459f011e8" + ], + "version": "==1.18.5" + }, + "pandas": { + "hashes": [ + "sha256:034185bb615dc96d08fa13aacba8862949db19d5e7804d6ee242d086f07bcc46", + "sha256:0c9b7f1933e3226cc16129cf2093338d63ace5c85db7c9588e3e1ac5c1937ad5", + "sha256:1f6fcf0404626ca0475715da045a878c7062ed39bc859afc4ccf0ba0a586a0aa", + "sha256:1fc963ba33c299973e92d45466e576d11f28611f3549469aec4a35658ef9f4cc", + "sha256:29b4cfee5df2bc885607b8f016e901e63df7ffc8f00209000471778f46cc6678", + "sha256:2a8b6c28607e3f3c344fe3e9b3cd76d2bf9f59bc8c0f2e582e3728b80e1786dc", + "sha256:2bc2ff52091a6ac481cc75d514f06227dc1b10887df1eb72d535475e7b825e31", + "sha256:415e4d52fcfd68c3d8f1851cef4d947399232741cc994c8f6aa5e6a9f2e4b1d8", + "sha256:519678882fd0587410ece91e3ff7f73ad6ded60f6fcb8aa7bcc85c1dc20ecac6", + "sha256:51e0abe6e9f5096d246232b461649b0aa627f46de8f6344597ca908f2240cbaa", + "sha256:698e26372dba93f3aeb09cd7da2bb6dd6ade248338cfe423792c07116297f8f4", + "sha256:83af85c8e539a7876d23b78433d90f6a0e8aa913e37320785cf3888c946ee874", + "sha256:982cda36d1773076a415ec62766b3c0a21cdbae84525135bdb8f460c489bb5dd", + "sha256:a647e44ba1b3344ebc5991c8aafeb7cca2b930010923657a273b41d86ae225c4", + "sha256:b35d625282baa7b51e82e52622c300a1ca9f786711b2af7cbe64f1e6831f4126", + "sha256:bab51855f8b318ef39c2af2c11095f45a10b74cbab4e3c8199efcc5af314c648" + ], + "index": "pypi", + "version": "==1.0.4" + }, + "psycopg2-binary": { + "hashes": [ + "sha256:008da3ab51adc70a5f1cfbbe5db3a22607ab030eb44bcecf517ad11a0c2b3cac", + "sha256:07cf82c870ec2d2ce94d18e70c13323c89f2f2a2628cbf1feee700630be2519a", + "sha256:08507efbe532029adee21b8d4c999170a83760d38249936038bd0602327029b5", + "sha256:107d9be3b614e52a192719c6bf32e8813030020ea1d1215daa86ded9a24d8b04", + "sha256:17a0ea0b0eabf07035e5e0d520dabc7950aeb15a17c6d36128ba99b2721b25b1", + "sha256:3286541b9d85a340ee4ed42732d15fc1bb441dc500c97243a768154ab8505bb5", + "sha256:3939cf75fc89c5e9ed836e228c4a63604dff95ad19aed2bbf71d5d04c15ed5ce", + "sha256:40abc319f7f26c042a11658bf3dd3b0b3bceccf883ec1c565d5c909a90204434", + "sha256:51f7823f1b087d2020d8e8c9e6687473d3d239ba9afc162d9b2ab6e80b53f9f9", + "sha256:6bb2dd006a46a4a4ce95201f836194eb6a1e863f69ee5bab506673e0ca767057", + "sha256:702f09d8f77dc4794651f650828791af82f7c2efd8c91ae79e3d9fe4bb7d4c98", + "sha256:7036ccf715925251fac969f4da9ad37e4b7e211b1e920860148a10c0de963522", + "sha256:7b832d76cc65c092abd9505cc670c4e3421fd136fb6ea5b94efbe4c146572505", + "sha256:8f74e631b67482d504d7e9cf364071fc5d54c28e79a093ff402d5f8f81e23bfa", + "sha256:930315ac53dc65cbf52ab6b6d27422611f5fb461d763c531db229c7e1af6c0b3", + "sha256:96d3038f5bd061401996614f65d27a4ecb62d843eb4f48e212e6d129171a721f", + "sha256:a20299ee0ea2f9cca494396ac472d6e636745652a64a418b39522c120fd0a0a4", + "sha256:a34826d6465c2e2bbe9d0605f944f19d2480589f89863ed5f091943be27c9de4", + "sha256:a69970ee896e21db4c57e398646af9edc71c003bc52a3cc77fb150240fefd266", + "sha256:b9a8b391c2b0321e0cd7ec6b4cfcc3dd6349347bd1207d48bcb752aa6c553a66", + "sha256:ba13346ff6d3eb2dca0b6fa0d8a9d999eff3dcd9b55f3a890f12b0b6362b2b38", + "sha256:bb0608694a91db1e230b4a314e8ed00ad07ed0c518f9a69b83af2717e31291a3", + "sha256:c8830b7d5f16fd79d39b21e3d94f247219036b29b30c8270314c46bf8b732389", + "sha256:cac918cd7c4c498a60f5d2a61d4f0a6091c2c9490d81bc805c963444032d0dab", + "sha256:cc30cb900f42c8a246e2cb76539d9726f407330bc244ca7729c41a44e8d807fb", + "sha256:ccdc6a87f32b491129ada4b87a43b1895cf2c20fdb7f98ad979647506ffc41b6", + "sha256:d1a8b01f6a964fec702d6b6dac1f91f2b9f9fe41b310cbb16c7ef1fac82df06d", + "sha256:e004db88e5a75e5fdab1620fb9f90c9598c2a195a594225ac4ed2a6f1c23e162", + "sha256:eb2f43ae3037f1ef5e19339c41cf56947021ac892f668765cd65f8ab9814192e", + "sha256:fa466306fcf6b39b8a61d003123d442b23707d635a5cb05ac4e1b62cc79105cd" + ], + "index": "pypi", + "version": "==2.8.5" + }, + "python-dateutil": { + "hashes": [ + "sha256:73ebfe9dbf22e832286dafa60473e4cd239f8592f699aa5adaf10050e6e1823c", + "sha256:75bb3f31ea686f1197762692a9ee6a7550b59fc6ca3a1f4b5d7e32fb98e2da2a" + ], + "version": "==2.8.1" + }, + "python-dotenv": { + "hashes": [ + "sha256:25c0ff1a3e12f4bde8d592cc254ab075cfe734fc5dd989036716fd17ee7e5ec7", + "sha256:3b9909bc96b0edc6b01586e1eed05e71174ef4e04c71da5786370cebea53ad74" + ], + "index": "pypi", + "version": "==0.13.0" + }, + "pytz": { + "hashes": [ + "sha256:a494d53b6d39c3c6e44c3bec237336e14305e4f29bbf800b599253057fbb79ed", + "sha256:c35965d010ce31b23eeb663ed3cc8c906275d6be1a34393a1d73a41febf4a048" + ], + "version": "==2020.1" + }, + "six": { + "hashes": [ + "sha256:30639c035cdb23534cd4aa2dd52c3bf48f06e5f4a941509c8bafd8ce11080259", + "sha256:8b74bedcbbbaca38ff6d7491d76f2b06b3592611af620f8426e82dddb04a5ced" + ], + "version": "==1.15.0" + } + }, + "develop": {} +} diff --git a/module1-introduction-to-sql/assignment1-sql/buddymove_holidayiq.csv b/module1-introduction-to-sql/assignment1-sql/buddymove_holidayiq.csv new file mode 100644 index 00000000..d162ec2b --- /dev/null +++ b/module1-introduction-to-sql/assignment1-sql/buddymove_holidayiq.csv @@ -0,0 +1,250 @@ +User Id,Sports,Religious,Nature,Theatre,Shopping,Picnic +User 1,2,77,79,69,68,95 +User 2,2,62,76,76,69,68 +User 3,2,50,97,87,50,75 +User 4,2,68,77,95,76,61 +User 5,2,98,54,59,95,86 +User 6,3,52,109,93,52,76 +User 7,3,64,85,82,73,69 +User 8,3,54,107,92,54,76 +User 9,3,64,108,64,54,93 +User 10,3,86,76,74,74,103 +User 11,3,107,54,64,103,94 +User 12,3,103,60,63,102,93 +User 13,3,64,82,82,75,69 +User 14,3,93,54,74,103,69 +User 15,3,63,82,81,78,69 +User 16,3,82,79,75,75,82 +User 17,5,59,131,103,54,86 +User 18,5,56,124,108,56,85 +User 19,4,85,67,111,65,72 +User 20,5,114,83,65,114,102 +User 21,4,93,82,79,79,90 +User 22,4,105,52,75,113,78 +User 23,5,69,118,74,66,101 +User 24,4,71,123,64,59,102 +User 25,5,88,94,81,79,91 +User 26,5,83,99,89,74,91 +User 27,5,69,133,74,54,101 +User 28,5,128,53,74,117,105 +User 29,5,74,123,69,61,101 +User 30,5,79,93,118,90,72 +User 31,4,51,115,110,51,84 +User 32,5,69,93,93,81,79 +User 33,4,100,53,86,112,78 +User 34,4,88,69,108,71,77 +User 35,4,93,84,75,84,112 +User 36,4,79,86,110,93,73 +User 37,4,87,72,112,63,71 +User 38,5,65,128,79,56,101 +User 39,4,91,66,110,69,71 +User 40,4,93,79,74,92,90 +User 41,5,59,118,108,63,85 +User 42,5,65,103,90,80,79 +User 43,4,87,89,76,89,111 +User 44,4,93,85,79,85,91 +User 45,5,93,94,79,79,113 +User 46,5,74,130,74,53,101 +User 47,4,118,59,69,119,97 +User 48,4,101,61,76,113,78 +User 49,5,88,99,79,83,114 +User 50,4,74,87,87,79,79 +User 51,14,148,59,104,208,119 +User 52,5,93,89,74,89,93 +User 53,4,79,103,68,103,85 +User 54,4,76,92,111,90,71 +User 55,8,94,140,89,118,92 +User 56,8,108,109,89,89,143 +User 57,6,98,89,133,79,79 +User 58,8,84,118,102,94,90 +User 59,8,138,74,79,143,118 +User 60,6,74,109,103,89,88 +User 61,8,118,104,94,94,118 +User 62,8,94,128,89,128,99 +User 63,8,64,155,118,64,97 +User 64,6,84,108,138,103,79 +User 65,8,108,109,94,99,138 +User 66,6,89,108,128,116,81 +User 67,6,103,79,148,74,79 +User 68,6,103,109,99,89,108 +User 69,8,108,99,94,94,138 +User 70,8,108,109,104,79,133 +User 71,8,138,59,84,138,128 +User 72,8,79,143,113,69,109 +User 73,8,84,153,79,64,113 +User 74,8,74,148,118,64,104 +User 75,8,74,153,118,59,99 +User 76,6,118,64,101,133,92 +User 77,6,69,153,113,59,99 +User 78,6,123,68,89,128,90 +User 79,6,99,104,128,64,78 +User 80,6,124,59,84,143,88 +User 81,6,84,131,128,93,81 +User 82,6,113,64,89,141,86 +User 83,6,118,64,99,134,88 +User 84,8,89,143,84,64,118 +User 85,6,98,89,133,69,79 +User 86,6,98,104,109,91,101 +User 87,8,84,143,74,74,133 +User 88,8,123,64,93,148,90 +User 89,6,84,113,141,98,81 +User 90,6,59,148,125,59,97 +User 91,8,143,69,79,143,123 +User 92,8,94,133,84,125,97 +User 93,8,84,108,113,93,90 +User 94,8,84,138,89,69,123 +User 95,6,84,108,133,98,81 +User 96,6,99,84,138,69,83 +User 97,8,113,94,84,109,128 +User 98,6,148,64,74,138,128 +User 99,8,84,138,113,64,114 +User 100,6,98,79,138,79,79 +User 101,8,133,84,84,133,118 +User 102,6,79,113,106,87,88 +User 103,8,113,104,84,94,148 +User 104,6,69,138,113,79,99 +User 105,6,83,103,98,99,90 +User 106,8,69,143,123,71,97 +User 107,8,118,94,99,99,143 +User 108,6,74,133,99,74,113 +User 109,12,115,94,163,109,92 +User 110,12,123,124,109,114,168 +User 111,12,94,133,157,133,95 +User 112,12,104,123,163,128,89 +User 113,14,99,198,89,74,153 +User 114,14,113,148,99,148,110 +User 115,14,94,163,99,94,153 +User 116,12,133,104,119,129,133 +User 117,14,163,69,94,188,143 +User 118,12,74,158,158,74,119 +User 119,12,123,124,109,129,143 +User 120,12,94,137,148,113,100 +User 121,12,123,129,139,99,143 +User 122,12,89,163,99,79,143 +User 123,14,81,173,143,79,122 +User 124,14,135,84,158,139,92 +User 125,12,108,113,133,114,100 +User 126,14,138,119,119,124,158 +User 127,12,128,122,134,109,120 +User 128,14,79,203,153,69,119 +User 129,12,84,150,128,104,102 +User 130,12,148,69,104,173,138 +User 131,14,133,124,119,124,168 +User 132,12,89,128,148,111,102 +User 133,12,79,183,133,70,113 +User 134,12,96,113,113,99,97 +User 135,14,84,161,153,84,116 +User 136,12,109,135,158,123,92 +User 137,12,133,74,114,175,102 +User 138,14,133,139,124,109,128 +User 139,12,128,124,114,111,126 +User 140,10,97,123,108,119,111 +User 141,12,123,104,104,134,153 +User 142,10,113,89,168,89,89 +User 143,12,109,148,94,133,119 +User 144,10,165,64,94,173,97 +User 145,12,93,123,123,124,100 +User 146,14,143,98,119,163,105 +User 147,12,89,158,89,89,158 +User 148,10,113,111,153,84,97 +User 149,12,138,86,114,158,102 +User 150,12,79,158,148,69,119 +User 151,14,84,178,138,87,116 +User 152,14,84,168,148,80,113 +User 153,14,84,173,143,69,110 +User 154,12,104,130,178,118,92 +User 155,10,99,123,153,128,103 +User 156,12,93,128,118,119,100 +User 157,12,128,119,104,119,173 +User 158,14,94,178,109,69,143 +User 159,12,148,69,104,168,114 +User 160,12,113,114,109,119,143 +User 161,12,153,74,94,163,158 +User 162,22,139,163,114,203,139 +User 163,14,163,99,94,158,143 +User 164,12,89,178,89,79,153 +User 165,12,79,183,104,69,148 +User 166,12,168,69,84,198,133 +User 167,22,128,158,183,158,130 +User 168,22,129,188,109,104,198 +User 169,16,178,74,114,198,129 +User 170,20,143,139,139,139,148 +User 171,20,114,198,124,114,153 +User 172,18,105,158,143,129,133 +User 173,20,120,193,104,148,138 +User 174,20,98,193,163,99,140 +User 175,22,148,174,129,129,153 +User 176,20,183,104,164,193,153 +User 177,18,99,203,129,94,158 +User 178,22,139,178,139,163,144 +User 179,22,90,188,163,109,138 +User 180,18,163,84,129,193,139 +User 181,18,88,178,178,89,140 +User 182,18,114,144,213,133,128 +User 183,22,143,124,208,134,124 +User 184,20,188,94,104,208,158 +User 185,18,99,228,104,84,163 +User 186,18,178,94,114,188,173 +User 187,22,119,198,149,114,168 +User 188,20,114,203,109,84,158 +User 189,18,99,193,158,99,144 +User 190,22,153,139,154,154,183 +User 191,20,162,94,144,198,135 +User 192,18,132,149,139,119,144 +User 193,18,104,193,114,99,173 +User 194,18,99,183,128,114,129 +User 195,20,178,89,154,198,183 +User 196,20,114,243,104,79,173 +User 197,22,133,183,114,183,140 +User 198,22,148,149,154,149,148 +User 199,22,114,213,129,94,178 +User 200,22,188,124,114,183,188 +User 201,25,153,159,129,139,218 +User 202,18,114,153,178,138,124 +User 203,22,184,84,129,233,133 +User 204,18,84,193,188,80,138 +User 205,18,178,129,104,183,163 +User 206,18,114,163,163,148,124 +User 207,16,132,94,178,109,120 +User 208,20,93,183,178,89,140 +User 209,16,137,104,173,109,130 +User 210,18,153,74,114,188,129 +User 211,18,128,114,168,124,124 +User 212,22,114,228,119,99,168 +User 213,22,178,89,114,203,139 +User 214,18,173,84,99,193,158 +User 215,20,168,89,124,183,129 +User 216,18,83,198,163,84,140 +User 217,22,144,173,134,173,149 +User 218,18,198,64,89,203,153 +User 219,20,114,208,114,119,168 +User 220,18,114,183,114,109,178 +User 221,18,114,173,183,128,124 +User 222,18,128,134,188,89,124 +User 223,18,143,154,129,129,178 +User 224,18,128,163,173,133,130 +User 225,18,148,99,139,168,129 +User 226,18,83,218,163,74,140 +User 227,22,120,168,158,144,133 +User 228,22,203,99,104,223,173 +User 229,18,143,139,119,139,203 +User 230,22,153,144,139,144,193 +User 231,18,147,144,129,149,144 +User 232,18,188,74,109,213,168 +User 233,25,158,154,154,154,153 +User 234,25,104,208,168,134,144 +User 235,25,139,153,193,158,134 +User 236,25,84,247,168,109,140 +User 237,25,173,89,124,233,158 +User 238,8,93,119,99,89,138 +User 239,22,124,168,208,148,124 +User 240,18,114,158,178,158,124 +User 241,20,188,94,94,223,153 +User 242,25,114,238,124,104,178 +User 243,18,94,188,148,99,139 +User 244,25,129,318,94,89,188 +User 245,18,139,148,129,129,168 +User 246,22,114,228,104,84,168 +User 247,20,124,178,104,158,174 +User 248,20,133,149,139,144,213 +User 249,20,143,149,139,159,143 diff --git a/module1-introduction-to-sql/assignment1-sql/buddymove_holidayiq.py b/module1-introduction-to-sql/assignment1-sql/buddymove_holidayiq.py new file mode 100644 index 00000000..74e77eae --- /dev/null +++ b/module1-introduction-to-sql/assignment1-sql/buddymove_holidayiq.py @@ -0,0 +1,29 @@ +import pandas as pd +import sqlite3 + +df = pd.read_csv("assignment1-sql/buddymove_holidayiq.csv") +df.columns = ['user_id', 'sports', 'religious', 'nature', 'theatre', 'shopping', 'picnic'] + +# df = pd.read_csv('buddymove_holidayiq.sqlite3') + +# connecting to the database +connection = sqlite3.connect('buddymove_holidayiq.sqlite3') + +# cursor +cursor = connection.cursor() + +# to insert data into new table +df.to_sql('review', connection, index = False) +cursor.execute('SELECT * FROM review').fetchall() + +print(df.shape) + +print(df.head) + +# Count how many rows you have - it should be 249! +query = "SELECT COUNT(*) FROM review" +print(cursor.execute(query).fetchall()[0][0]) + +# How many users who reviewed at least 100 Nature in the category also reviewed at least 100 in the Shopping category? +query2 = "SELECT COUNT(*) FROM review WHERE (Nature) >= 100 AND (Shopping) >= 100" +print(cursor.execute(query2).fetchall()[0][0]) \ No newline at end of file diff --git a/module1-introduction-to-sql/assignment1-sql/buddymove_holidayiq.sqlite3 b/module1-introduction-to-sql/assignment1-sql/buddymove_holidayiq.sqlite3 new file mode 100644 index 00000000..752404da Binary files /dev/null and b/module1-introduction-to-sql/assignment1-sql/buddymove_holidayiq.sqlite3 differ diff --git a/module1-introduction-to-sql/assignment1-sql/rpg_db.sqlite3 b/module1-introduction-to-sql/assignment1-sql/rpg_db.sqlite3 new file mode 100644 index 00000000..837d7f16 Binary files /dev/null and b/module1-introduction-to-sql/assignment1-sql/rpg_db.sqlite3 differ diff --git a/module1-introduction-to-sql/assignment1-sql/rpg_queries.py b/module1-introduction-to-sql/assignment1-sql/rpg_queries.py new file mode 100644 index 00000000..0789bb89 --- /dev/null +++ b/module1-introduction-to-sql/assignment1-sql/rpg_queries.py @@ -0,0 +1,69 @@ +import os +import sqlite3 + +# construct a path to wherever your database exists +#DB_FILEPATH = "module1-introduction-to-sql/chinook.db" +#DB_FILEPATH = os.path.join("module1-introduction-to-sql", "chinook.db") +#DB_FILEPATH = os.path.join(os.path.dirname(__file__), ",,", "module2-0...", ""chinook.db") +DB_FILEPATH = os.path.join(os.path.dirname(__file__), "rpg_db.sqlite3") + +# connecting to the database +connection = sqlite3.connect(DB_FILEPATH) + +# cursor +cursor = connection.cursor() + + +# How many total Characters are there? +query = "SELECT COUNT(distinct character_id) FROM charactercreator_character" +print(cursor.execute(query).fetchall()[0][0]) + +# How many of each specific subclass? +query1 = "SELECT COUNT(distinct character_ptr_id) FROM charactercreator_mage" +print(cursor.execute(query1).fetchall()[0][0]) + +query2 = "SELECT COUNT(distinct mage_ptr_id) FROM charactercreator_necromancer" +print(cursor.execute(query2).fetchall()[0][0]) + +query3 = "SELECT COUNT(distinct character_ptr_id) FROM charactercreator_fighter" +print(cursor.execute(query3).fetchall()[0][0]) + +query4 = "SELECT COUNT(distinct character_ptr_id) FROM charactercreator_cleric" +print(cursor.execute(query4).fetchall()[0][0]) + +query5 = "SELECT COUNT(distinct character_ptr_id) FROM charactercreator_thief" +print(cursor.execute(query5).fetchall()[0][0]) + +# How many total Items? +query6= "SELECT COUNT(distinct item_id) FROM armory_item" +print(cursor.execute(query6).fetchall()[0][0]) + +# How many of the Items are weapons? How many are not? +query7 = "SELECT COUNT(item_ptr_id) as weapons_count FROM armory_weapon" +print(cursor.execute(query7).fetchall()[0][0]) + +# How many Items does each character have? (Return first 20 rows) +query8 = "SELECT character_id,item_id FROM charactercreator_character_inventory LIMIT 20" +print(cursor.execute(query8).fetchall()[0][0]) + +# How many Weapons does each character have? (Return first 20 rows) +query9 = "SELECT character_id ,count(distinct item_id) as weapons_count FROM charactercreator_character_inventory WHERE item_id IN (SELECT distinct item_ptr_id FROM armory_weapon) GROUP BY character_id LIMIT 20" +print(cursor.execute(query9).fetchall()[0][0]) + +# On average, how many Items does each Character have? +query_10 = "SELECT ccc.character_id ,AVG(cinv.item_id) as items FROM charactercreator_character ccc LEFT JOIN charactercreator_character_inventory cinv ON ccc.character_id = cinv.character_id" +print(cursor.execute(query_10).fetchall()[0][0]) + +# On average, how many Weapons does each character have? +query_11 = "SELECT character_id ,count(item_id) as items FROM charactercreator_character_inventory WHERE item_id IN (SELECT distinct item_ptr_id from armory_weapon) GROUP BY character_id" +print(cursor.execute(query_11).fetchall()[0][0]) + +#result = cursor.execute(query) +#print("RESULT", result) #> returns cursor object w/o results (need to fetch the results) + +# result3 = cursor.execute(query).fetchone() +# print("RESULT 3", type(result3), result3) + +# for row in result3: +# print(type(row), row) + diff --git a/module1-introduction-to-sql/chinook.db b/module1-introduction-to-sql/chinook.db new file mode 100644 index 00000000..38a98b39 Binary files /dev/null and b/module1-introduction-to-sql/chinook.db differ diff --git a/module1-introduction-to-sql/chinook_queries.py b/module1-introduction-to-sql/chinook_queries.py new file mode 100644 index 00000000..70ccd0c8 --- /dev/null +++ b/module1-introduction-to-sql/chinook_queries.py @@ -0,0 +1,43 @@ +import os +import sqlite3 + +# construct a path to wherever your database exists +#DB_FILEPATH = "module1-introduction-to-sql/chinook.db" +#DB_FILEPATH = os.path.join("module1-introduction-to-sql", "chinook.db") +#DB_FILEPATH = os.path.join(os.path.dirname(__file__), ",,", "module2-0...", ""chinook.db") +DB_FILEPATH = os.path.join(os.path.dirname(__file__), "chinook.db") + +connection = sqlite3.connect(DB_FILEPATH) +print("CONNECTION:", connection) + +cursor = connection.cursor() +print("CURSOR", cursor) + +query = "SELECT * FROM customers;" + +#result = cursor.execute(query) +#print("RESULT", result) #> returns cursor object w/o results (need to fetch the results) + +result2 = cursor.execute(query).fetchall() +print("RESULT 2", result2) + +for row in result2: + print(type(row), row) + + + + + + +# many queries + +query = "SELECT count(distinct CustomerId) as customer_count FROM Customers" + +#result = cursor.execute(query) +#print("RESULT", result) #> returns cursor object w/o results (need to fetch the results) + +result3 = cursor.execute(query).fetchone() +print("RESULT 2", type(result3), result3) + +for row in result2: + print(type(row), row) \ No newline at end of file diff --git a/module2-sql-for-analysis/elephant_queries.py b/module2-sql-for-analysis/elephant_queries.py new file mode 100644 index 00000000..a886571c --- /dev/null +++ b/module2-sql-for-analysis/elephant_queries.py @@ -0,0 +1,64 @@ + +import os +from dotenv import load_dotenv +import json +import psycopg2 +from psycopg2.extras import execute_values + +load_dotenv() #> loads contents of the .env file into the script's environment + +DB_NAME = os.getenv("DB_NAME") +DB_USER = os.getenv("DB_USER") +DB_PASSWORD = os.getenv("DB_PASSWORD") +DB_HOST = os.getenv("DB_HOST") + +print(DB_NAME, DB_USER, DB_PASSWORD, DB_HOST) + +connection = psycopg2.connect(dbname=DB_NAME, user=DB_USER, password=DB_PASSWORD, host=DB_HOST) +print("CONNECTION:", connection) + +cursor = connection.cursor() +print("CURSOR:", cursor) + +# FETCH DATA +# FYI: in sqlite: result = cursor.execute("SELECT * from test_table;").fetchall() + +cursor.execute('SELECT * from test_table;') +result = cursor.fetchall() +print(result) + +# INSERT DATA + +# insertion_sql = """ +# INSERT INTO test_table (name, data) VALUES +# ('A row name',null), +# ('Another row, with JSON','{ "a": 1, "b": ["dog", "cat", 42], "c": true }'::JSONB); +# # """ +# cursor.execute(insertion_sql) + +my_dict = { "a": 1, "b": ["dog", "cat", 42], "c": 'true' } +# insertion_query = "INSERT INTO test_table (name, data) VALUES (%s, %s)" +# cursor.execute(insertion_query, +# ('A rowwwww', 'null') +# ) +# cursor.execute(insertion_query, +# ('Another row, with JSONNNNN', json.dumps(my_dict)) +# ) + + +insertion_query = "INSERT INTO test_table (name, data) VALUES %s" +execute_values(cursor, insertion_query, [ + ('A rowwwww', 'null'), + ('Another row, with JSONNNNN', json.dumps(my_dict)), + ('Third row', "3") +]) #data must be in a list of tuples!!!! + + + + + +# ACTUALLY SAVE THE TRANSACTIONS +connection.commit() + +cursor.close() +connection.close() \ No newline at end of file