|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Practice with SQLite 3 with a given prompt" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "You are given the below tables, showing Store, Product, and Sales information for a chain of grocery stores. The columns are labeled in such a way that you should be able to interpret what each field is showing." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "First up, we need to create the tables..." |
| 22 | + ] |
| 23 | + }, |
| 24 | + { |
| 25 | + "cell_type": "code", |
| 26 | + "execution_count": 1, |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "import sqlite3" |
| 31 | + ] |
| 32 | + }, |
| 33 | + { |
| 34 | + "cell_type": "code", |
| 35 | + "execution_count": 2, |
| 36 | + "metadata": {}, |
| 37 | + "outputs": [], |
| 38 | + "source": [ |
| 39 | + "# Create DB\n", |
| 40 | + "connection = sqlite3.connect(\"test_DB.db\")" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "code", |
| 45 | + "execution_count": 3, |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "# Define SQL Tables\n", |
| 50 | + "sql_table1 = \"\"\"\n", |
| 51 | + "CREATE TABLE store ( \n", |
| 52 | + "store_number INTEGER PRIMARY KEY, \n", |
| 53 | + "location text);\"\"\"\n", |
| 54 | + "\n", |
| 55 | + "sql_table2 = \"\"\"\n", |
| 56 | + "CREATE TABLE product ( \n", |
| 57 | + "product_id INTEGER PRIMARY KEY, \n", |
| 58 | + "product_name text,\n", |
| 59 | + "price_usd real);\"\"\"\n", |
| 60 | + "\n", |
| 61 | + "sql_table3 = \"\"\"\n", |
| 62 | + "CREATE TABLE sales ( \n", |
| 63 | + "sale_id INTEGER,\n", |
| 64 | + "product_id integer,\n", |
| 65 | + "store_id integer,\n", |
| 66 | + "date DATE);\"\"\"" |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": 4, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "# Establish a Connection to the Database\n", |
| 76 | + "cursor = connection.cursor()" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": 5, |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [ |
| 84 | + { |
| 85 | + "data": { |
| 86 | + "text/plain": [ |
| 87 | + "<sqlite3.Cursor at 0x111cdef10>" |
| 88 | + ] |
| 89 | + }, |
| 90 | + "execution_count": 5, |
| 91 | + "metadata": {}, |
| 92 | + "output_type": "execute_result" |
| 93 | + } |
| 94 | + ], |
| 95 | + "source": [ |
| 96 | + "# delete \n", |
| 97 | + "#cursor.execute(\"\"\"DROP TABLE store;\"\"\")\n", |
| 98 | + "#cursor.execute(\"\"\"DROP TABLE product;\"\"\")\n", |
| 99 | + "#cursor.execute(\"\"\"DROP TABLE sales;\"\"\")" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": 6, |
| 105 | + "metadata": {}, |
| 106 | + "outputs": [ |
| 107 | + { |
| 108 | + "data": { |
| 109 | + "text/plain": [ |
| 110 | + "<sqlite3.Cursor at 0x111cdef10>" |
| 111 | + ] |
| 112 | + }, |
| 113 | + "execution_count": 6, |
| 114 | + "metadata": {}, |
| 115 | + "output_type": "execute_result" |
| 116 | + } |
| 117 | + ], |
| 118 | + "source": [ |
| 119 | + "# Initialize the tables\n", |
| 120 | + "cursor.execute(sql_table1)\n", |
| 121 | + "cursor.execute(sql_table2)\n", |
| 122 | + "cursor.execute(sql_table3)" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "code", |
| 127 | + "execution_count": 7, |
| 128 | + "metadata": {}, |
| 129 | + "outputs": [], |
| 130 | + "source": [ |
| 131 | + "# Populate the DB's\n", |
| 132 | + "store_data = [ (\"91110\", \"New York\"),\n", |
| 133 | + " (\"99525\", \"Los Angeles\"),\n", |
| 134 | + " (\"37340\", \"Tokyo\"),\n", |
| 135 | + " (\"32016\", \"Detroit\"),\n", |
| 136 | + " (\"57507\", \"London\")]\n", |
| 137 | + "\n", |
| 138 | + "product_data = [ (\"31331\", \"Apples\", \"2\"),\n", |
| 139 | + " (\"34611\", \"Lettuce\", \"3\"),\n", |
| 140 | + " (\"49760\", \"Chicken\", \"5\"),\n", |
| 141 | + " (\"26583\", \"Lemons\", \"1\"),\n", |
| 142 | + " (\"20267\", \"Bread\", \"2\")]\n", |
| 143 | + "\n", |
| 144 | + "\n", |
| 145 | + "sales_data = [ ( \"1\",\"31331\", \"91110\", \"02/20/2020\"),\n", |
| 146 | + " ( \"1\",\"31331\", \"91110\", \"02/20/2020\"),\n", |
| 147 | + " ( \"2\",\"34611\", \"57507\", \"02/20/2020\"),\n", |
| 148 | + " ( \"3\",\"26583\", \"37340\", \"02/20/2020\"),\n", |
| 149 | + " ( \"3\",\"34611\", \"32016\", \"02/20/2020\"),\n", |
| 150 | + " ( \"3\",\"20267\", \"99525\", \"02/21/2020\"),\n", |
| 151 | + " ( \"4\",\"31331\", \"99525\", \"02/21/2020\"),\n", |
| 152 | + " ( \"5\",\"49760\", \"99525\", \"02/21/2020\"),\n", |
| 153 | + " ( \"6\",\"34611\", \"97507\", \"02/21/2020\"),\n", |
| 154 | + " ( \"7\",\"31331\", \"91110\", \"02/21/2020\")]" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": 8, |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [ |
| 163 | + "# Insert the data\n", |
| 164 | + "for p in store_data:\n", |
| 165 | + " format_str1 = \"\"\"INSERT INTO store (store_number, location)\n", |
| 166 | + " VALUES (\"{store_number}\", \"{location}\");\"\"\"\n", |
| 167 | + " \n", |
| 168 | + " sql_command1 = format_str1.format(store_number=p[0], location=p[1])\n", |
| 169 | + " cursor.execute(sql_command1)\n", |
| 170 | + " \n", |
| 171 | + "for l in product_data:\n", |
| 172 | + " format_str2 = \"\"\"INSERT INTO product (product_id, product_name, price_usd)\n", |
| 173 | + " VALUES (\"{product_id}\", \"{product_name}\", \"{price_usd}\");\"\"\"\n", |
| 174 | + " \n", |
| 175 | + " sql_command2 = format_str2.format(product_id=l[0], product_name=l[1], price_usd=l[2])\n", |
| 176 | + " cursor.execute(sql_command2)\n", |
| 177 | + " \n", |
| 178 | + "for k in sales_data:\n", |
| 179 | + " format_str3 = \"\"\"INSERT INTO sales (sale_id, product_id, store_id, date)\n", |
| 180 | + " VALUES (\"{sale_id}\", \"{product_id}\", \"{store_id}\", \"{date}\");\"\"\"\n", |
| 181 | + " \n", |
| 182 | + " sql_command3 = format_str3.format(sale_id=k[0], product_id=k[1], store_id=k[2], date = k[3])\n", |
| 183 | + " cursor.execute(sql_command3)" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "code", |
| 188 | + "execution_count": 9, |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [], |
| 191 | + "source": [ |
| 192 | + "# Never forget this, if you want the changes to be saved:\n", |
| 193 | + "connection.commit()" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": 10, |
| 199 | + "metadata": {}, |
| 200 | + "outputs": [], |
| 201 | + "source": [ |
| 202 | + "# Close the connection\n", |
| 203 | + "connection.close()" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "markdown", |
| 208 | + "metadata": {}, |
| 209 | + "source": [ |
| 210 | + "## Now, query the data to get the desired answer\n", |
| 211 | + "\n", |
| 212 | + "Using the tables above, write a SQL query to return the number of sales as well as the average sale price (in dollars) for a given location.\n", |
| 213 | + "\n", |
| 214 | + "Your output should return the following columns:\n", |
| 215 | + "\n", |
| 216 | + "location\t, number_sales\t, avg_sale_price\n", |
| 217 | + "\n" |
| 218 | + ] |
| 219 | + }, |
| 220 | + { |
| 221 | + "cell_type": "code", |
| 222 | + "execution_count": 11, |
| 223 | + "metadata": {}, |
| 224 | + "outputs": [], |
| 225 | + "source": [ |
| 226 | + "connection = sqlite3.connect(\"test_DB.db\")\n", |
| 227 | + "\n", |
| 228 | + "cursor = connection.cursor()" |
| 229 | + ] |
| 230 | + }, |
| 231 | + { |
| 232 | + "cell_type": "code", |
| 233 | + "execution_count": 12, |
| 234 | + "metadata": {}, |
| 235 | + "outputs": [ |
| 236 | + { |
| 237 | + "name": "stdout", |
| 238 | + "output_type": "stream", |
| 239 | + "text": [ |
| 240 | + "fetchall:\n", |
| 241 | + "(32016, 'Detroit')\n", |
| 242 | + "(37340, 'Tokyo')\n", |
| 243 | + "(57507, 'London')\n", |
| 244 | + "(91110, 'New York')\n", |
| 245 | + "(99525, 'Los Angeles')\n" |
| 246 | + ] |
| 247 | + } |
| 248 | + ], |
| 249 | + "source": [ |
| 250 | + "# What does the data look like? Does our table pull right?\n", |
| 251 | + "\n", |
| 252 | + "# Make the query\n", |
| 253 | + "cursor.execute(\"SELECT * FROM store\") \n", |
| 254 | + "\n", |
| 255 | + "# Now, print the query\n", |
| 256 | + "print(\"fetchall:\")\n", |
| 257 | + "result = cursor.fetchall() \n", |
| 258 | + "for r in result:\n", |
| 259 | + " print(r)" |
| 260 | + ] |
| 261 | + }, |
| 262 | + { |
| 263 | + "cell_type": "code", |
| 264 | + "execution_count": 17, |
| 265 | + "metadata": {}, |
| 266 | + "outputs": [ |
| 267 | + { |
| 268 | + "name": "stdout", |
| 269 | + "output_type": "stream", |
| 270 | + "text": [ |
| 271 | + "\n", |
| 272 | + "fetch one:\n", |
| 273 | + "(32016, 'Detroit')\n" |
| 274 | + ] |
| 275 | + } |
| 276 | + ], |
| 277 | + "source": [ |
| 278 | + "## Similar, but instead do it one at a time\n", |
| 279 | + "cursor.execute(\"SELECT * FROM store\") \n", |
| 280 | + "\n", |
| 281 | + "# Print\n", |
| 282 | + "print(\"\\nfetch one:\")\n", |
| 283 | + "res = cursor.fetchone() \n", |
| 284 | + "print(res)" |
| 285 | + ] |
| 286 | + }, |
| 287 | + { |
| 288 | + "cell_type": "markdown", |
| 289 | + "metadata": {}, |
| 290 | + "source": [ |
| 291 | + "Now again, our problem: \n", |
| 292 | + "- Using the tables above, write a SQL query to return the number of sales as well as the average sale price (in dollars) for a given location." |
| 293 | + ] |
| 294 | + }, |
| 295 | + { |
| 296 | + "cell_type": "markdown", |
| 297 | + "metadata": {}, |
| 298 | + "source": [ |
| 299 | + "## The Query, which I put into SQLite\n", |
| 300 | + "\n", |
| 301 | + "SELECT \n", |
| 302 | + "\n", |
| 303 | + " location AS Location,\n", |
| 304 | + "\n", |
| 305 | + " count(sale_id) AS num_sales, \n", |
| 306 | + " \n", |
| 307 | + " avg(price_usd) AS avg_price\n", |
| 308 | + "\n", |
| 309 | + "\n", |
| 310 | + "FROM \n", |
| 311 | + " \n", |
| 312 | + " test_db.sales\n", |
| 313 | + " \n", |
| 314 | + " INNER JOIN test_db.store ON sales.store_id = store.store_number\n", |
| 315 | + " \n", |
| 316 | + " INNER JOIN test_db.product ON sales.product_id = product.product_id\n", |
| 317 | + " \n", |
| 318 | + "\n", |
| 319 | + "GROUP BY \n", |
| 320 | + "\n", |
| 321 | + " sales.store_id\n", |
| 322 | + "\n", |
| 323 | + "ORDER BY\n", |
| 324 | + "\n", |
| 325 | + " avg_price DESC;" |
| 326 | + ] |
| 327 | + }, |
| 328 | + { |
| 329 | + "cell_type": "markdown", |
| 330 | + "metadata": {}, |
| 331 | + "source": [ |
| 332 | + "## Our Results" |
| 333 | + ] |
| 334 | + }, |
| 335 | + { |
| 336 | + "cell_type": "markdown", |
| 337 | + "metadata": {}, |
| 338 | + "source": [ |
| 339 | + "| Location | NUM_SALES | AVG_PRICE |\n", |
| 340 | + "|----------|-----------|-----------|\n", |
| 341 | + "|New York | 3 | 2 |\n", |
| 342 | + "|Los Angeles | 3 | 3 |\n", |
| 343 | + "|Detroit | 1 | 3 |\n", |
| 344 | + "|Tokyo | 1 | 1 |\n", |
| 345 | + "|London | 1 | 3 |" |
| 346 | + ] |
| 347 | + }, |
| 348 | + { |
| 349 | + "cell_type": "markdown", |
| 350 | + "metadata": {}, |
| 351 | + "source": [ |
| 352 | + "We see that New York and LA have the highest number of sales but LA seems to be making more money per sale, on average." |
| 353 | + ] |
| 354 | + }, |
| 355 | + { |
| 356 | + "cell_type": "markdown", |
| 357 | + "metadata": {}, |
| 358 | + "source": [ |
| 359 | + "This was a fun way to dive a little bit further into sqlite3 on Python and then connect to the DB in SQLite and answer the question." |
| 360 | + ] |
| 361 | + }, |
| 362 | + { |
| 363 | + "cell_type": "markdown", |
| 364 | + "metadata": {}, |
| 365 | + "source": [] |
| 366 | + } |
| 367 | + ], |
| 368 | + "metadata": { |
| 369 | + "kernelspec": { |
| 370 | + "display_name": "Python 3", |
| 371 | + "language": "python", |
| 372 | + "name": "python3" |
| 373 | + }, |
| 374 | + "language_info": { |
| 375 | + "codemirror_mode": { |
| 376 | + "name": "ipython", |
| 377 | + "version": 3 |
| 378 | + }, |
| 379 | + "file_extension": ".py", |
| 380 | + "mimetype": "text/x-python", |
| 381 | + "name": "python", |
| 382 | + "nbconvert_exporter": "python", |
| 383 | + "pygments_lexer": "ipython3", |
| 384 | + "version": "3.7.4" |
| 385 | + } |
| 386 | + }, |
| 387 | + "nbformat": 4, |
| 388 | + "nbformat_minor": 2 |
| 389 | +} |
0 commit comments