{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Guided Project: Sales Performance Analysis with SQL\n",
"## Learn to combine SQL's skills we've learned to perform practical data analysis\n",
"## Dataquest"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/plain": [
"[]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%load_ext sql\n",
"%sql sqlite://\n",
"%sql attach \"sales.db\" as sales"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Guided Project Introduction"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"
\n",
" \n",
" id | \n",
" name | \n",
" description | \n",
"
\n",
" \n",
" 1 | \n",
" products | \n",
" Items in the stores | \n",
"
\n",
" \n",
" 2 | \n",
" regions | \n",
" Geographic areas where stores exist | \n",
"
\n",
" \n",
" 3 | \n",
" sales | \n",
" Transactions in the store alongside with product, region, and time dimensions | \n",
"
\n",
" \n",
" 4 | \n",
" sales_details | \n",
" Transactions in the stores with several details | \n",
"
\n",
" \n",
" 5 | \n",
" table_name | \n",
" Tables in the database | \n",
"
\n",
" \n",
" 6 | \n",
" time | \n",
" Dates | \n",
"
\n",
"
"
],
"text/plain": [
"[(1, 'products', 'Items in the stores'),\n",
" (2, 'regions', 'Geographic areas where stores exist'),\n",
" (3, 'sales', 'Transactions in the store alongside with product, region, and time dimensions'),\n",
" (4, 'sales_details', 'Transactions in the stores with several details'),\n",
" (5, 'table_name', 'Tables in the database'),\n",
" (6, 'time', 'Dates')]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"-- Title: Sales Performance Analysis\n",
"-- Description: we are trying to analyze the sales.db databases to find meanningful trends using SQL.\n",
"-- Goal: Our goal is to find the share of sales of the top three regions, the frequently bought products in these top three countrie and the most suitable days for intense marketing and promotion?\n",
"\n",
"-- table_name table's content.\n",
"SELECT *\n",
" FROM table_name;\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Getting to know the database"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" product_id | \n",
" product_name | \n",
" product_description | \n",
"
\n",
" \n",
" 1 | \n",
" Puff Pastry - Slab | \n",
" None | \n",
"
\n",
" \n",
" 2 | \n",
" Lamb - Loin Chops | \n",
" None | \n",
"
\n",
" \n",
" 3 | \n",
" Flour - Fast / Rapid | \n",
" None | \n",
"
\n",
"
"
],
"text/plain": [
"[(1, 'Puff Pastry - Slab', None),\n",
" (2, 'Lamb - Loin Chops', None),\n",
" (3, 'Flour - Fast / Rapid', None)]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"-- the first three rows of each table from our database\n",
"\n",
"SELECT *\n",
" FROM products\n",
" LIMIT 3;"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" region_id | \n",
" region_name | \n",
" region_description | \n",
"
\n",
" \n",
" 1 | \n",
" Indonesia | \n",
" None | \n",
"
\n",
" \n",
" 2 | \n",
" Portugal | \n",
" None | \n",
"
\n",
" \n",
" 3 | \n",
" Netherlands | \n",
" None | \n",
"
\n",
"
"
],
"text/plain": [
"[(1, 'Indonesia', None), (2, 'Portugal', None), (3, 'Netherlands', None)]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT *\n",
" FROM regions\n",
" LIMIT 3;"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" sales_id | \n",
" time_id | \n",
" product_id | \n",
" region_id | \n",
" transaction_amount | \n",
"
\n",
" \n",
" 4 | \n",
" 868 | \n",
" 227 | \n",
" 154 | \n",
" -39864 | \n",
"
\n",
" \n",
" 5 | \n",
" 436 | \n",
" 684 | \n",
" 724 | \n",
" 79272 | \n",
"
\n",
" \n",
" 6 | \n",
" 529 | \n",
" 176 | \n",
" 478 | \n",
" -33632 | \n",
"
\n",
"
"
],
"text/plain": [
"[(4, 868, 227, 154, -39864),\n",
" (5, 436, 684, 724, 79272),\n",
" (6, 529, 176, 478, -33632)]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT *\n",
" FROM sales\n",
" LIMIT 3;"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" time_id | \n",
" time_datetime | \n",
" time_year | \n",
" time_month | \n",
" time_day | \n",
" time_hour | \n",
" time_minute | \n",
" time_second | \n",
" time_day_of_week | \n",
" product_name | \n",
" region_name | \n",
" transaction_amount | \n",
"
\n",
" \n",
" 868 | \n",
" 2013-06-26 13:50:36 | \n",
" 2013 | \n",
" 6 | \n",
" 26 | \n",
" 13 | \n",
" 50 | \n",
" 36 | \n",
" 4 | \n",
" Chickensplit Half | \n",
" Pakistan | \n",
" -39864 | \n",
"
\n",
" \n",
" 436 | \n",
" 2012-06-14 09:34:24 | \n",
" 2012 | \n",
" 6 | \n",
" 14 | \n",
" 9 | \n",
" 34 | \n",
" 24 | \n",
" 5 | \n",
" Soup - Beef Conomme, Dry | \n",
" Poland | \n",
" 79272 | \n",
"
\n",
" \n",
" 529 | \n",
" 2015-08-12 02:58:16 | \n",
" 2015 | \n",
" 8 | \n",
" 12 | \n",
" 2 | \n",
" 58 | \n",
" 16 | \n",
" 4 | \n",
" Tea - Earl Grey | \n",
" China | \n",
" -33632 | \n",
"
\n",
"
"
],
"text/plain": [
"[(868, '2013-06-26 13:50:36', 2013, 6, 26, 13, 50, 36, 4, 'Chickensplit Half', 'Pakistan', -39864),\n",
" (436, '2012-06-14 09:34:24', 2012, 6, 14, 9, 34, 24, 5, 'Soup - Beef Conomme, Dry', 'Poland', 79272),\n",
" (529, '2015-08-12 02:58:16', 2015, 8, 12, 2, 58, 16, 4, 'Tea - Earl Grey', 'China', -33632)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT *\n",
" FROM sales_details\n",
" LIMIT 3;"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" time_id | \n",
" time_datetime | \n",
" time_year | \n",
" time_month | \n",
" time_day | \n",
" time_hour | \n",
" time_minute | \n",
" time_second | \n",
" time_day_of_week | \n",
"
\n",
" \n",
" 1 | \n",
" 2017-10-11 19:52:57 | \n",
" 2017 | \n",
" 10 | \n",
" 11 | \n",
" 19 | \n",
" 52 | \n",
" 57 | \n",
" 4 | \n",
"
\n",
" \n",
" 2 | \n",
" 2018-01-24 19:15:24 | \n",
" 2018 | \n",
" 1 | \n",
" 24 | \n",
" 19 | \n",
" 15 | \n",
" 24 | \n",
" 4 | \n",
"
\n",
" \n",
" 3 | \n",
" 2017-12-06 19:45:58 | \n",
" 2017 | \n",
" 12 | \n",
" 6 | \n",
" 19 | \n",
" 45 | \n",
" 58 | \n",
" 4 | \n",
"
\n",
"
"
],
"text/plain": [
"[(1, '2017-10-11 19:52:57', 2017, 10, 11, 19, 52, 57, 4),\n",
" (2, '2018-01-24 19:15:24', 2018, 1, 24, 19, 15, 24, 4),\n",
" (3, '2017-12-06 19:45:58', 2017, 12, 6, 19, 45, 58, 4)]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"SELECT *\n",
" FROM time\n",
" LIMIT 3;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Identifying the Tables We Need"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" selected_table | \n",
"
\n",
" \n",
" sales_details | \n",
"
\n",
"
"
],
"text/plain": [
"[('sales_details',)]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"-- Selecting the identified table.\n",
"\n",
"SELECT 'sales_details' AS selected_table;\n"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" num_rows | \n",
"
\n",
" \n",
" 1000 | \n",
"
\n",
"
"
],
"text/plain": [
"[(1000,)]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"-- Counting the rows of the selected table.\n",
"\n",
"SELECT COUNT(*) AS num_rows\n",
" FROM sales_details;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Filtering interesting rows\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" time_id | \n",
" time_datetime | \n",
" time_year | \n",
" time_month | \n",
" time_day | \n",
" time_hour | \n",
" time_minute | \n",
" time_second | \n",
" time_day_of_week | \n",
" product_name | \n",
" region_name | \n",
" transaction_amount | \n",
"
\n",
" \n",
" 436 | \n",
" 2012-06-14 09:34:24 | \n",
" 2012 | \n",
" 6 | \n",
" 14 | \n",
" 9 | \n",
" 34 | \n",
" 24 | \n",
" 5 | \n",
" Soup - Beef Conomme, Dry | \n",
" Poland | \n",
" 79272 | \n",
"
\n",
" \n",
" 146 | \n",
" 2015-02-06 12:18:18 | \n",
" 2015 | \n",
" 2 | \n",
" 6 | \n",
" 12 | \n",
" 18 | \n",
" 18 | \n",
" 6 | \n",
" Wine - Spumante Bambino White | \n",
" France | \n",
" 87806 | \n",
"
\n",
" \n",
" 483 | \n",
" 2012-12-01 23:08:12 | \n",
" 2012 | \n",
" 12 | \n",
" 1 | \n",
" 23 | \n",
" 8 | \n",
" 12 | \n",
" 7 | \n",
" Ketchup - Tomato | \n",
" Kazakhstan | \n",
" 98243 | \n",
"
\n",
"
"
],
"text/plain": [
"[(436, '2012-06-14 09:34:24', 2012, 6, 14, 9, 34, 24, 5, 'Soup - Beef Conomme, Dry', 'Poland', 79272),\n",
" (146, '2015-02-06 12:18:18', 2015, 2, 6, 12, 18, 18, 6, 'Wine - Spumante Bambino White', 'France', 87806),\n",
" (483, '2012-12-01 23:08:12', 2012, 12, 1, 23, 8, 12, 7, 'Ketchup - Tomato', 'Kazakhstan', 98243)]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"-- Filtering the rows related to positive values from the `transaction_amount` column.\n",
"\n",
"SELECT *\n",
" FROM sales_details\n",
" WHERE transaction_amount >= 0 \n",
" LIMIT 3;"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" num_positive_transactions | \n",
" num_rows | \n",
"
\n",
" \n",
" 494 | \n",
" 494 | \n",
"
\n",
"
"
],
"text/plain": [
"[(494, 494)]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"-- Counting the positive values from the `transaction_amount` column compared to the number of rows in the filtered table.\n",
"\n",
"SELECT COUNT(transaction_amount) AS num_positive_transactions, COUNT(*) AS num_rows\n",
" FROM sales_details\n",
" WHERE transaction_amount >= 0;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Since the `num_positive_transactions` and `num_rows` are the same, we can conclude that there is no missing values in the `transaction_amount` column."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Total Transaction Amount for Each Region\n"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" region_name | \n",
" total_transaction_amount | \n",
"
\n",
" \n",
" Afghanistan | \n",
" 131159 | \n",
"
\n",
" \n",
" Albania | \n",
" 235171 | \n",
"
\n",
" \n",
" Argentina | \n",
" 340583 | \n",
"
\n",
" \n",
" Armenia | \n",
" 77199 | \n",
"
\n",
" \n",
" Australia | \n",
" 187963 | \n",
"
\n",
" \n",
" Azerbaijan | \n",
" 159200 | \n",
"
\n",
" \n",
" Bahamas | \n",
" 148728 | \n",
"
\n",
" \n",
" Bangladesh | \n",
" 154916 | \n",
"
\n",
" \n",
" Belarus | \n",
" 25315 | \n",
"
\n",
" \n",
" Benin | \n",
" 34352 | \n",
"
\n",
" \n",
" Bolivia | \n",
" 10961 | \n",
"
\n",
" \n",
" Brazil | \n",
" 488294 | \n",
"
\n",
" \n",
" Burkina Faso | \n",
" 38329 | \n",
"
\n",
" \n",
" Cameroon | \n",
" 44091 | \n",
"
\n",
" \n",
" Canada | \n",
" 125266 | \n",
"
\n",
" \n",
" China | \n",
" 3587812 | \n",
"
\n",
" \n",
" Colombia | \n",
" 356547 | \n",
"
\n",
" \n",
" Croatia | \n",
" 140920 | \n",
"
\n",
" \n",
" Cuba | \n",
" 196686 | \n",
"
\n",
" \n",
" Cyprus | \n",
" 577 | \n",
"
\n",
" \n",
" Czech Republic | \n",
" 94021 | \n",
"
\n",
" \n",
" Denmark | \n",
" 66339 | \n",
"
\n",
" \n",
" Dominican Republic | \n",
" 126001 | \n",
"
\n",
" \n",
" Ecuador | \n",
" 26411 | \n",
"
\n",
" \n",
" Egypt | \n",
" 59730 | \n",
"
\n",
" \n",
" Estonia | \n",
" 32547 | \n",
"
\n",
" \n",
" Ethiopia | \n",
" 276368 | \n",
"
\n",
" \n",
" Finland | \n",
" 196962 | \n",
"
\n",
" \n",
" France | \n",
" 871335 | \n",
"
\n",
" \n",
" Georgia | \n",
" 90044 | \n",
"
\n",
" \n",
" Germany | \n",
" 74158 | \n",
"
\n",
" \n",
" Greece | \n",
" 346326 | \n",
"
\n",
" \n",
" Guatemala | \n",
" 9567 | \n",
"
\n",
" \n",
" Guinea | \n",
" 49896 | \n",
"
\n",
" \n",
" Guyana | \n",
" 124443 | \n",
"
\n",
" \n",
" Honduras | \n",
" 169520 | \n",
"
\n",
" \n",
" Hungary | \n",
" 80988 | \n",
"
\n",
" \n",
" Indonesia | \n",
" 2565442 | \n",
"
\n",
" \n",
" Iran | \n",
" 353566 | \n",
"
\n",
" \n",
" Iraq | \n",
" 96055 | \n",
"
\n",
" \n",
" Ireland | \n",
" 89760 | \n",
"
\n",
" \n",
" Israel | \n",
" 81269 | \n",
"
\n",
" \n",
" Ivory Coast | \n",
" 142272 | \n",
"
\n",
" \n",
" Japan | \n",
" 138545 | \n",
"
\n",
" \n",
" Kazakhstan | \n",
" 121368 | \n",
"
\n",
" \n",
" Kyrgyzstan | \n",
" 95798 | \n",
"
\n",
" \n",
" Laos | \n",
" 119385 | \n",
"
\n",
" \n",
" Latvia | \n",
" 74817 | \n",
"
\n",
" \n",
" Libya | \n",
" 81214 | \n",
"
\n",
" \n",
" Luxembourg | \n",
" 70793 | \n",
"
\n",
" \n",
" Macedonia | \n",
" 46793 | \n",
"
\n",
" \n",
" Madagascar | \n",
" 92723 | \n",
"
\n",
" \n",
" Malawi | \n",
" 122046 | \n",
"
\n",
" \n",
" Mali | \n",
" 85633 | \n",
"
\n",
" \n",
" Mauritius | \n",
" 42660 | \n",
"
\n",
" \n",
" Mexico | \n",
" 338072 | \n",
"
\n",
" \n",
" Mongolia | \n",
" 6955 | \n",
"
\n",
" \n",
" Morocco | \n",
" 97271 | \n",
"
\n",
" \n",
" Netherlands | \n",
" 87392 | \n",
"
\n",
" \n",
" New Caledonia | \n",
" 31718 | \n",
"
\n",
" \n",
" Nicaragua | \n",
" 54865 | \n",
"
\n",
" \n",
" Nigeria | \n",
" 150886 | \n",
"
\n",
" \n",
" Norway | \n",
" 214806 | \n",
"
\n",
" \n",
" Pakistan | \n",
" 121607 | \n",
"
\n",
" \n",
" Palestinian Territory | \n",
" 291565 | \n",
"
\n",
" \n",
" Peru | \n",
" 190533 | \n",
"
\n",
" \n",
" Philippines | \n",
" 1097122 | \n",
"
\n",
" \n",
" Poland | \n",
" 1342022 | \n",
"
\n",
" \n",
" Portugal | \n",
" 559054 | \n",
"
\n",
" \n",
" Russia | \n",
" 1053683 | \n",
"
\n",
" \n",
" Saudi Arabia | \n",
" 28475 | \n",
"
\n",
" \n",
" Serbia | \n",
" 95519 | \n",
"
\n",
" \n",
" Sierra Leone | \n",
" 41685 | \n",
"
\n",
" \n",
" Slovenia | \n",
" 47833 | \n",
"
\n",
" \n",
" Somalia | \n",
" 66332 | \n",
"
\n",
" \n",
" South Korea | \n",
" 229805 | \n",
"
\n",
" \n",
" Spain | \n",
" 355871 | \n",
"
\n",
" \n",
" Sweden | \n",
" 777801 | \n",
"
\n",
" \n",
" Switzerland | \n",
" 131534 | \n",
"
\n",
" \n",
" Syria | \n",
" 123207 | \n",
"
\n",
" \n",
" Tajikistan | \n",
" 58541 | \n",
"
\n",
" \n",
" Tanzania | \n",
" 245208 | \n",
"
\n",
" \n",
" Thailand | \n",
" 413431 | \n",
"
\n",
" \n",
" Togo | \n",
" 201465 | \n",
"
\n",
" \n",
" Turkmenistan | \n",
" 47280 | \n",
"
\n",
" \n",
" Uganda | \n",
" 190927 | \n",
"
\n",
" \n",
" Ukraine | \n",
" 228561 | \n",
"
\n",
" \n",
" United Kingdom | \n",
" 74921 | \n",
"
\n",
" \n",
" United States | \n",
" 1005656 | \n",
"
\n",
" \n",
" Uzbekistan | \n",
" 68969 | \n",
"
\n",
" \n",
" Venezuela | \n",
" 112740 | \n",
"
\n",
" \n",
" Vietnam | \n",
" 259546 | \n",
"
\n",
" \n",
" Yemen | \n",
" 22777 | \n",
"
\n",
"
"
],
"text/plain": [
"[('Afghanistan', 131159),\n",
" ('Albania', 235171),\n",
" ('Argentina', 340583),\n",
" ('Armenia', 77199),\n",
" ('Australia', 187963),\n",
" ('Azerbaijan', 159200),\n",
" ('Bahamas', 148728),\n",
" ('Bangladesh', 154916),\n",
" ('Belarus', 25315),\n",
" ('Benin', 34352),\n",
" ('Bolivia', 10961),\n",
" ('Brazil', 488294),\n",
" ('Burkina Faso', 38329),\n",
" ('Cameroon', 44091),\n",
" ('Canada', 125266),\n",
" ('China', 3587812),\n",
" ('Colombia', 356547),\n",
" ('Croatia', 140920),\n",
" ('Cuba', 196686),\n",
" ('Cyprus', 577),\n",
" ('Czech Republic', 94021),\n",
" ('Denmark', 66339),\n",
" ('Dominican Republic', 126001),\n",
" ('Ecuador', 26411),\n",
" ('Egypt', 59730),\n",
" ('Estonia', 32547),\n",
" ('Ethiopia', 276368),\n",
" ('Finland', 196962),\n",
" ('France', 871335),\n",
" ('Georgia', 90044),\n",
" ('Germany', 74158),\n",
" ('Greece', 346326),\n",
" ('Guatemala', 9567),\n",
" ('Guinea', 49896),\n",
" ('Guyana', 124443),\n",
" ('Honduras', 169520),\n",
" ('Hungary', 80988),\n",
" ('Indonesia', 2565442),\n",
" ('Iran', 353566),\n",
" ('Iraq', 96055),\n",
" ('Ireland', 89760),\n",
" ('Israel', 81269),\n",
" ('Ivory Coast', 142272),\n",
" ('Japan', 138545),\n",
" ('Kazakhstan', 121368),\n",
" ('Kyrgyzstan', 95798),\n",
" ('Laos', 119385),\n",
" ('Latvia', 74817),\n",
" ('Libya', 81214),\n",
" ('Luxembourg', 70793),\n",
" ('Macedonia', 46793),\n",
" ('Madagascar', 92723),\n",
" ('Malawi', 122046),\n",
" ('Mali', 85633),\n",
" ('Mauritius', 42660),\n",
" ('Mexico', 338072),\n",
" ('Mongolia', 6955),\n",
" ('Morocco', 97271),\n",
" ('Netherlands', 87392),\n",
" ('New Caledonia', 31718),\n",
" ('Nicaragua', 54865),\n",
" ('Nigeria', 150886),\n",
" ('Norway', 214806),\n",
" ('Pakistan', 121607),\n",
" ('Palestinian Territory', 291565),\n",
" ('Peru', 190533),\n",
" ('Philippines', 1097122),\n",
" ('Poland', 1342022),\n",
" ('Portugal', 559054),\n",
" ('Russia', 1053683),\n",
" ('Saudi Arabia', 28475),\n",
" ('Serbia', 95519),\n",
" ('Sierra Leone', 41685),\n",
" ('Slovenia', 47833),\n",
" ('Somalia', 66332),\n",
" ('South Korea', 229805),\n",
" ('Spain', 355871),\n",
" ('Sweden', 777801),\n",
" ('Switzerland', 131534),\n",
" ('Syria', 123207),\n",
" ('Tajikistan', 58541),\n",
" ('Tanzania', 245208),\n",
" ('Thailand', 413431),\n",
" ('Togo', 201465),\n",
" ('Turkmenistan', 47280),\n",
" ('Uganda', 190927),\n",
" ('Ukraine', 228561),\n",
" ('United Kingdom', 74921),\n",
" ('United States', 1005656),\n",
" ('Uzbekistan', 68969),\n",
" ('Venezuela', 112740),\n",
" ('Vietnam', 259546),\n",
" ('Yemen', 22777)]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"-- Computing the total positive transaction amount for each region using the `GROUP BY` clause.\n",
"\n",
"SELECT region_name, SUM(transaction_amount) AS total_transaction_amount\n",
" FROM sales_details\n",
" WHERE transaction_amount >= 0\n",
" GROUP BY region_name;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Top Three Regions\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" region_name | \n",
" total_transaction_amount | \n",
"
\n",
" \n",
" China | \n",
" 3587812 | \n",
"
\n",
" \n",
" Indonesia | \n",
" 2565442 | \n",
"
\n",
" \n",
" Poland | \n",
" 1342022 | \n",
"
\n",
"
"
],
"text/plain": [
"[('China', 3587812), ('Indonesia', 2565442), ('Poland', 1342022)]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"\n",
"-- Finding the top three regions in relation to the total positive transaction amount.\n",
"\n",
"SELECT region_name, SUM(transaction_amount) AS total_transaction_amount\n",
" FROM sales_details\n",
" WHERE transaction_amount >= 0\n",
" GROUP BY region_name\n",
" ORDER BY total_transaction_amount DESC\n",
" LIMIT 3;\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Share of Sales\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" region_name | \n",
" total_transaction_amount_props | \n",
"
\n",
" \n",
" China | \n",
" 14.909148950077872 | \n",
"
\n",
" \n",
" Indonesia | \n",
" 10.660691502449314 | \n",
"
\n",
" \n",
" Poland | \n",
" 5.576770993653348 | \n",
"
\n",
"
"
],
"text/plain": [
"[('China', 14.909148950077872),\n",
" ('Indonesia', 10.660691502449314),\n",
" ('Poland', 5.576770993653348)]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"-- Computes the share of sales of the top three regions in relation to the total positive transaction amount.\n",
"\n",
"SELECT region_name, SUM(transaction_amount) * 100.0 / 24064499 AS total_transaction_amount_props\n",
" FROM sales_details\n",
" WHERE transaction_amount >= 0\n",
" GROUP BY region_name\n",
" ORDER BY total_transaction_amount_props DESC\n",
" LIMIT 3;\n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" top_3_share_market | \n",
"
\n",
" \n",
" 31.15 | \n",
"
\n",
"
"
],
"text/plain": [
"[(31.15,)]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"-- Summing the share of sales\n",
"SELECT ROUND(14.909148950077872, 2) + ROUND(10.660691502449314, 2) + ROUND(5.576770993653348, 2) AS top_3_share_market"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. The highest sales in our store come from Asian countries. Our customers are probably many in this part of the world."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Finding Frequently Bought Products\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" region_name | \n",
" product_name | \n",
" num_transactions | \n",
" avg_transaction_amount | \n",
"
\n",
" \n",
" Indonesia | \n",
" Beef - Rib Eye Aaa | \n",
" 3 | \n",
" 60284.333333333336 | \n",
"
\n",
" \n",
" China | \n",
" Coke - Diet, 355 Ml | \n",
" 2 | \n",
" 46720.0 | \n",
"
\n",
" \n",
" Indonesia | \n",
" Cheese - La Sauvagine | \n",
" 2 | \n",
" 54260.5 | \n",
"
\n",
" \n",
" Indonesia | \n",
" Fennel - Seeds | \n",
" 2 | \n",
" 36930.0 | \n",
"
\n",
" \n",
" Indonesia | \n",
" Oneshot Automatic Soap System | \n",
" 2 | \n",
" 58257.0 | \n",
"
\n",
" \n",
" Indonesia | \n",
" Sprouts - Baby Pea Tendrils | \n",
" 2 | \n",
" 6054.5 | \n",
"
\n",
"
"
],
"text/plain": [
"[('Indonesia', 'Beef - Rib Eye Aaa', 3, 60284.333333333336),\n",
" ('China', 'Coke - Diet, 355 Ml', 2, 46720.0),\n",
" ('Indonesia', 'Cheese - La Sauvagine', 2, 54260.5),\n",
" ('Indonesia', 'Fennel - Seeds', 2, 36930.0),\n",
" ('Indonesia', 'Oneshot Automatic Soap System', 2, 58257.0),\n",
" ('Indonesia', 'Sprouts - Baby Pea Tendrils', 2, 6054.5)]"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"\n",
"-- Finding the frequently bought products in China, Indonesia, and Poland.\n",
"\n",
"SELECT region_name, product_name, COUNT(*) AS num_transactions, AVG(transaction_amount) AS avg_transaction_amount\n",
" FROM sales_details\n",
" WHERE transaction_amount >= 0 AND (region_name = 'China' OR region_name = 'Indonesia' OR region_name = 'Poland')\n",
" GROUP BY region_name, product_name\n",
"HAVING COUNT(*) > 1\n",
" ORDER BY num_transactions DESC, region_name, product_name;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. For example, in China, we always have to ensure to have the `Coke - Diet, 355 Ml` in stock and in Indonesia, `Beef - Rib Eye Aaa`. Notice that, Poland is not in the top 3 sales because some products are bought very frequently. Maybe the products there are just expensive."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"# Finding Promotion Days\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" * sqlite://\n",
"Done.\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" region_name | \n",
" time_day_of_week | \n",
" num_transactions | \n",
" total_transaction_amount | \n",
"
\n",
" \n",
" China | \n",
" 3 | \n",
" 14 | \n",
" 687399 | \n",
"
\n",
" \n",
" China | \n",
" 4 | \n",
" 14 | \n",
" 578406 | \n",
"
\n",
" \n",
" China | \n",
" 5 | \n",
" 13 | \n",
" 607495 | \n",
"
\n",
" \n",
" China | \n",
" 7 | \n",
" 14 | \n",
" 702672 | \n",
"
\n",
" \n",
" Indonesia | \n",
" 5 | \n",
" 13 | \n",
" 717903 | \n",
"
\n",
"
"
],
"text/plain": [
"[('China', 3, 14, 687399),\n",
" ('China', 4, 14, 578406),\n",
" ('China', 5, 13, 607495),\n",
" ('China', 7, 14, 702672),\n",
" ('Indonesia', 5, 13, 717903)]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"%%sql\n",
"\n",
"-- Finding the days of the week where the number of transactions is greater or equal to `10` in China, Indonesia, and Poland.\n",
" \n",
"SELECT region_name, time_day_of_week, COUNT(*) AS num_transactions, SUM( transaction_amount) as total_transaction_amount\n",
" FROM sales_details\n",
" WHERE transaction_amount >= 0 AND (region_name = 'China' OR region_name = 'Indonesia' OR region_name = 'Poland' )\n",
" GROUP BY region_name, time_day_of_week\n",
"HAVING COUNT(*) >= 10\n",
" ORDER BY region_name, time_day_of_week;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. In China most of the days are interesting while in Indonesia it is only on Friday. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}