diff --git a/02_activities/assignments/DC_Cohort/assignment1.sql b/02_activities/assignments/DC_Cohort/assignment1.sql index c992e3205..d5b241a9d 100644 --- a/02_activities/assignments/DC_Cohort/assignment1.sql +++ b/02_activities/assignments/DC_Cohort/assignment1.sql @@ -1,20 +1,25 @@ /* ASSIGNMENT 1 */ /* SECTION 2 */ - --SELECT /* 1. Write a query that returns everything in the customer table. */ - - +SELECT * +FROM customer; /* 2. Write a query that displays all of the columns and 10 rows from the cus- tomer table, sorted by customer_last_name, then customer_first_ name. */ - +SELECT * +FROM customer +ORDER BY customer_last_name, customer_first_name +LIMIT 10; --WHERE /* 1. Write a query that returns all customer purchases of product IDs 4 and 9. */ - +SELECT * +FROM customer_purchases +WHERE product_id = 4 + OR product_id = 9; /*2. Write a query that returns all customer purchases and a new calculated column 'price' (quantity * cost_to_customer_per_qty), @@ -23,10 +28,19 @@ filtered by customer IDs between 8 and 10 (inclusive) using either: 2. one condition using BETWEEN */ -- option 1 - +SELECT +*, + (quantity * cost_to_customer_per_qty) AS PRICE +FROM customer_purchases +WHERE customer_id >= 8 + AND customer_id <=10; -- option 2 - +SELECT + *, + (quantity * cost_to_customer_per_qty) AS price +FROM customer_purchases +WHERE customer_id BETWEEN 8 AND 10; --CASE @@ -34,19 +48,44 @@ filtered by customer IDs between 8 and 10 (inclusive) using either: Using the product table, write a query that outputs the product_id and product_name columns and add a column called prod_qty_type_condensed that displays the word “unit” if the product_qty_type is “unit,” and otherwise displays the word “bulk.” */ - - +SELECT + product_id, + product_name, + CASE + WHEN product_qty_type = 'unit' THEN 'unit' + ELSE 'bulk' + END AS prod_qty_type_condensed +FROM product ; /* 2. We want to flag all of the different types of pepper products that are sold at the market. add a column to the previous query called pepper_flag that outputs a 1 if the product_name contains the word “pepper” (regardless of capitalization), and otherwise outputs 0. */ - - +WITH product_flagged AS ( + SELECT + product_id, + product_name, + CASE + WHEN product_qty_type = 'unit' THEN 'unit' + ELSE 'bulk' + END AS prod_qty_type_condensed + FROM product +) +SELECT + *, + CASE + WHEN LOWER(product_name) LIKE '%pepper%' THEN 1 + ELSE 0 + END AS pepper_flag +FROM product_flagged; --JOIN /* 1. Write a query that INNER JOINs the vendor table to the vendor_booth_assignments table on the vendor_id field they both have in common, and sorts the result by vendor_name, then market_date. */ - +SELECT * +FROM vendor AS v +INNER JOIN vendor_booth_assignments AS vba + ON v.vendor_id = vba.vendor_id +ORDER BY v.vendor_name, vba.market_date; @@ -55,7 +94,11 @@ vendor_id field they both have in common, and sorts the result by vendor_name, t -- AGGREGATE /* 1. Write a query that determines how many times each vendor has rented a booth at the farmer’s market by counting the vendor booth assignments per vendor_id. */ - +SELECT + vendor_id, + COUNT(*) AS booth_rental_count +FROM vendor_booth_assignments +GROUP BY vendor_id; /* 2. The Farmer’s Market Customer Appreciation Committee wants to give a bumper @@ -63,7 +106,15 @@ sticker to everyone who has ever spent more than $2000 at the market. Write a qu of customers for them to give stickers to, sorted by last name, then first name. HINT: This query requires you to join two tables, use an aggregate function, and use the HAVING keyword. */ - +SELECT + c.customer_first_name, + c.customer_last_name, +FROM customer AS c +INNER JOIN customer_purchases AS cp + ON c.customer_id = cp.customer_id + GROUP BY c.customer_id, c.customer_first_name, c.customer_last_name + HAVING SUM (cp.quantity * cp.cost_to_customer_per_qty) > 2000 + ORDER BY c.customer_last_name, c.customer_first_name ; --Temp Table @@ -78,7 +129,18 @@ When inserting the new vendor, you need to appropriately align the columns to be VALUES(col1,col2,col3,col4,col5) */ +CREATE TABLE temp.new_vendor AS +SELECT * +FROM vendor + +INSERT INTO temp.new_vendor +(vendor_id, vendor_name, vendor_type, cendor_owner_first_name, vendor_owner_last_name) +VALUES +(10,'Thomass Superfood Store', ' Fresh Focused store', 'Thomas', 'Rosenthal'); +SELECT * +FROM temp.new_vendor +ORDER BY vendor_id; -- Date /*1. Get the customer_id, month, and year (in separate columns) of every purchase in the customer_purchases table. @@ -86,6 +148,11 @@ VALUES(col1,col2,col3,col4,col5) HINT: you might need to search for strfrtime modifers sqlite on the web to know what the modifers for month and year are! */ +SELECT +customer_id, +strftime('%m', purchase_date) AS month, +strftime('%Y', purchase_date) AS year +FROM customer_purchases; /* 2. Using the previous query as a base, determine how much money each customer spent in April 2022. @@ -94,3 +161,16 @@ Remember that money spent is quantity*cost_to_customer_per_qty. HINTS: you will need to AGGREGATE, GROUP BY, and filter... but remember, STRFTIME returns a STRING for your WHERE statement!! */ +SELECT + customer_id, + strftime('%m', purchase_date) AS month, + strftime('%Y', purchase_date) AS year +FROM customer_purchases; + +SELECT +customer_id, +SUM(quantity * cost_to_customer_per_qty) AS total_spent +FROM customer_purchases +WHERE strftime('%m', purchase_date) = '04' +AND strftime('%Y',purchase_date) = '2022' +GROUP BY customer_id; diff --git a/02_activities/assignments/DC_Cohort/images/01_db_browser_for_sqlite.png b/02_activities/assignments/DC_Cohort/images/01_db_browser_for_sqlite.png index 005e2dd3d..f50842499 100644 Binary files a/02_activities/assignments/DC_Cohort/images/01_db_browser_for_sqlite.png and b/02_activities/assignments/DC_Cohort/images/01_db_browser_for_sqlite.png differ diff --git a/02_activities/assignments/DC_Cohort/images/01_db_browser_for_sqlite_choose_db.png b/02_activities/assignments/DC_Cohort/images/01_db_browser_for_sqlite_choose_db.png index 598cf160c..a617a85eb 100644 Binary files a/02_activities/assignments/DC_Cohort/images/01_db_browser_for_sqlite_choose_db.png and b/02_activities/assignments/DC_Cohort/images/01_db_browser_for_sqlite_choose_db.png differ diff --git a/02_activities/assignments/DC_Cohort/images/01_farmers_market_conceptual_model.png b/02_activities/assignments/DC_Cohort/images/01_farmers_market_conceptual_model.png index 8064eda53..60a48b007 100644 Binary files a/02_activities/assignments/DC_Cohort/images/01_farmers_market_conceptual_model.png and b/02_activities/assignments/DC_Cohort/images/01_farmers_market_conceptual_model.png differ diff --git a/02_activities/assignments/DC_Cohort/images/01_the_table_drop_down_at_the_top_left.png b/02_activities/assignments/DC_Cohort/images/01_the_table_drop_down_at_the_top_left.png index a3c56238a..bc9b1b228 100644 Binary files a/02_activities/assignments/DC_Cohort/images/01_the_table_drop_down_at_the_top_left.png and b/02_activities/assignments/DC_Cohort/images/01_the_table_drop_down_at_the_top_left.png differ diff --git a/03_instructional_team/sqbpro_originals/module_2.sqbpro b/03_instructional_team/sqbpro_originals/module_2.sqbpro index 73ace631b..55ab8821e 100644 --- a/03_instructional_team/sqbpro_originals/module_2.sqbpro +++ b/03_instructional_team/sqbpro_originals/module_2.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 2 */ +
/* MODULE 2 */ /* SELECT */ @@ -13,11 +13,11 @@ SELECT -/* 4. Select multiple specific columns * / +/* 4. Select multiple specific columns */ -/* 5. Add a static value in a column *//* MODULE 2 */ +/* 5. Add a static value in a column *//* MODULE 2 */ /* WHERE */ /* 1. Select only customer 1 from the customer table */ @@ -38,11 +38,11 @@ WHERE -/* 5. Nulls and Blanks* / +/* 5. Nulls and Blanks*/ -/* 6. BETWEEN x AND y *//* MODULE 2 */ +/* 6. BETWEEN x AND y *//* MODULE 2 */ /* CASE */ @@ -61,7 +61,7 @@ SELECT * /* 4. Experiment with selecting a different column instead of just a string value */ -FROM vendor/* MODULE 2 */ +FROM vendor/* MODULE 2 */ /* DISTINCT */ @@ -81,7 +81,7 @@ SELECT customer_id FROM customer_purchases -/* 4. Which vendor has sold products to a customer ... and which product was it * / +/* 4. Which vendor has sold products to a customer ... and which product was it */ @@ -89,7 +89,7 @@ SELECT customer_id FROM customer_purchases ... and which product was it? ... AND to whom was it sold*/ -/* MODULE 2 */ +/* MODULE 2 */ /* INNER JOIN */ @@ -109,7 +109,7 @@ SELECT customer_id FROM customer_purchases -- using table aliases -/* MODULE 2 */ +/* MODULE 2 */ /* LEFT JOIN */ @@ -141,7 +141,7 @@ LEFT JOIN product AS p ...Note how the row count changed from 24 to 23 */ -/* MODULE 2 */ +/* MODULE 2 */ /* Multiple Table JOINs */ @@ -157,4 +157,4 @@ LEFT JOIN product AS p Why do we have more rows now?*/ -
+
diff --git a/03_instructional_team/sqbpro_originals/module_3.sqbpro b/03_instructional_team/sqbpro_originals/module_3.sqbpro index 3d421003d..d3b452b67 100644 --- a/03_instructional_team/sqbpro_originals/module_3.sqbpro +++ b/03_instructional_team/sqbpro_originals/module_3.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 3 */ +
/* MODULE 3 */ /* COUNT */ @@ -18,7 +18,7 @@ 4. How many unique products were bought */ -/* MODULE 3 */ +/* MODULE 3 */ /* SUM & AVG */ @@ -29,7 +29,7 @@ /* 2. How much does each customer spend on average */ -/* MODULE 3 */ +/* MODULE 3 */ /* MIN & MAX */ @@ -54,7 +54,7 @@ ... not particularly useful? */ -/* MODULE 3 */ +/* MODULE 3 */ /* Arithmitic */ @@ -68,7 +68,7 @@ SELECT /* 3. What about every third? */ -/* MODULE 3 */ +/* MODULE 3 */ /* HAVING */ @@ -81,7 +81,7 @@ Filter to customer_id between 1 and 5 and total_cost > 50 /* 2. How many products were bought? Filter to number of purchases between 300 and 500 */ -/* MODULE 3 */ +/* MODULE 3 */ /* Subquery FROM */ @@ -94,7 +94,7 @@ Filter to number of purchases between 300 and 500 */ /* 2. What is the single item that has been bought in the greatest quantity?*/ -/* MODULE 3 */ +/* MODULE 3 */ /* Subquery WHERE */ @@ -105,7 +105,7 @@ Filter to number of purchases between 300 and 500 */ /* 2. What is the name of the vendor who sells pie */ -/* MODULE 3 */ +/* MODULE 3 */ /* Common Table Expression (CTE) */ @@ -118,7 +118,7 @@ SELECT /* ... re-aggregate the daily sales for each WEEK instead now */ -/* MODULE 3 */ +/* MODULE 3 */ /* Temp Tables */ @@ -142,7 +142,7 @@ CREATE TABLE temp.new_vendor_inventory AS /* 2. put the previous table into another temp table, e.g. as temp.new_new_vendor_inventory */ -/* MODULE 3 */ +/* MODULE 3 */ /* Date functions */ diff --git a/03_instructional_team/sqbpro_originals/module_4.sqbpro b/03_instructional_team/sqbpro_originals/module_4.sqbpro index e2d101fad..b1f922c2e 100644 --- a/03_instructional_team/sqbpro_originals/module_4.sqbpro +++ b/03_instructional_team/sqbpro_originals/module_4.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 4 */ +
/* MODULE 4 */ /* NULL Management */ @@ -18,7 +18,7 @@ finding values in the product_size column that are "blank" strings and filtering which rows are null or blank */ -/* MODULE 4 */ +/* MODULE 4 */ /* NULLIF Budget (example from the slides) */ /* The following example creates a budgets table to show a department (dept) @@ -59,26 +59,11 @@ FROM budgets https://learn.microsoft.com/en-us/sql/t-sql/language-elements/nullif-transact-sql?view=sql-server-ver17 */ -/* MODULE 4 */ +/* MODULE 4 */ /* Windowed functions: row_number */ /* 1. What product is the highest price per vendor */ -SELECT * - -FROM ( - SELECT - vendor_id - ,market_date - ,product_id - ,original_price - ,ROW_NUMBER() OVER (PARTITION BY vendor_id ORDER BY original_price DESC) as price_rn - - FROM vendor_inventory -) x - -WHERE x.price_rn = 1; - @@ -92,7 +77,7 @@ GROUP BY vendor_id--,product_id */ -/* MODULE 4 */ +/* MODULE 4 */ /* Windowed functions: dense_rank, rank, row_number */ @@ -120,7 +105,7 @@ VALUES (10, 100000); -/* MODULE 4 */ +/* MODULE 4 */ /* Windowed functions: NTILE */ @@ -142,7 +127,7 @@ VALUES JOIN vendor AS v ON v.vendor_id = cp.vendor_id - GROUP BY cp.market_date, v.vendor_id/* MODULE 4 */ + GROUP BY cp.market_date, v.vendor_id/* MODULE 4 */ /* String Manipulations */ @@ -162,7 +147,7 @@ SELECT /* 7. unicode, char */ -/* 8. REGEXP in a WHERE statement *//* MODULE 4 */ +/* 8. REGEXP in a WHERE statement *//* MODULE 4 */ /* Substring & instring together */ @@ -182,12 +167,12 @@ SELECT INSTR('FirstWord, SecondWord, ThirdWord',',')+1)) ,',') + INSTR('FirstWord, SecondWord, ThirdWord',',')+1) AS ThirdDelim -/* MODULE 4 */ +/* MODULE 4 */ /* UNION */ /* 1. Find the most and least expensive product by vendor with UNION (and row_number!) */ -/* MODULE 4 */ +/* MODULE 4 */ /* UNION */ /* 1. Emulate a FULL OUTER JOIN with a UNION */ @@ -214,7 +199,7 @@ quantity INT INSERT INTO temp.store2 VALUES("tiger",2), ("dancer",7), - ("superhero", 5);/* MODULE 4 */ + ("superhero", 5);/* MODULE 4 */ /* INTERSECT & EXCEPT */ /* 1. Find products that have been sold (e.g. are in customer purchases AND product) */ diff --git a/03_instructional_team/sqbpro_originals/module_5.sqbpro b/03_instructional_team/sqbpro_originals/module_5.sqbpro index 84bbfec3b..f608313e0 100644 --- a/03_instructional_team/sqbpro_originals/module_5.sqbpro +++ b/03_instructional_team/sqbpro_originals/module_5.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 5 */ +
/* MODULE 5 */ /* INSERT UPDATE DELETE */ @@ -16,7 +16,7 @@ CREATE TEMP TABLE product_expanded AS -/* 3. delete the newly added product *//* MODULE 5 */ +/* 3. delete the newly added product *//* MODULE 5 */ /* VIEW */ /* 1. Create a vendor daily sales view */ @@ -38,14 +38,14 @@ CREATE TEMP TABLE product_expanded AS GROUP BY cp.market_date, v.vendor_id; -/* MODULE 5 */ +/* MODULE 5 */ /* VIEW in another query */ /* 1. Transform the daily sales view into a sales by vendor per week result */ -/* MODULE 5 */ +/* MODULE 5 */ /* UPDATE statements for view */ @@ -67,7 +67,7 @@ VALUES('....','....','....','....','8:00 AM','2:00 PM','nothing interesting','Su */ -/* MODULE 5 */ +/* MODULE 5 */ /* DYNAMIC VIEW */ @@ -105,7 +105,7 @@ VALUES('....','....','....','....','8:00 AM','2:00 PM','nothing interesting','Su -/* MODULE 5 */ +/* MODULE 5 */ /* CROSS JOIN */ @@ -123,7 +123,7 @@ SELECT * FROM TEMP.sizes; -/* MODULE 5 */ +/* MODULE 5 */ /* SELF JOIN */ diff --git a/04_this_cohort/custom_slides/markdown/slides_02.Rmd b/04_this_cohort/custom_slides/markdown/slides_02.Rmd index cc97ceab1..d47451fb3 100644 --- a/04_this_cohort/custom_slides/markdown/slides_02.Rmd +++ b/04_this_cohort/custom_slides/markdown/slides_02.Rmd @@ -448,7 +448,7 @@ class: middle, center, inverse class: top, left, inverse # Putting Things Together with JOIN -- Joins are used to combine data stored in different tables into a single table +- Joins are used to combine data stored in different tables into a single result set -- diff --git a/04_this_cohort/live_code/Cohort_8/module_2/module_2.sqbpro b/04_this_cohort/live_code/Cohort_8/module_2/module_2.sqbpro index 73ace631b..55ab8821e 100644 --- a/04_this_cohort/live_code/Cohort_8/module_2/module_2.sqbpro +++ b/04_this_cohort/live_code/Cohort_8/module_2/module_2.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 2 */ +
/* MODULE 2 */ /* SELECT */ @@ -13,11 +13,11 @@ SELECT -/* 4. Select multiple specific columns * / +/* 4. Select multiple specific columns */ -/* 5. Add a static value in a column *//* MODULE 2 */ +/* 5. Add a static value in a column *//* MODULE 2 */ /* WHERE */ /* 1. Select only customer 1 from the customer table */ @@ -38,11 +38,11 @@ WHERE -/* 5. Nulls and Blanks* / +/* 5. Nulls and Blanks*/ -/* 6. BETWEEN x AND y *//* MODULE 2 */ +/* 6. BETWEEN x AND y *//* MODULE 2 */ /* CASE */ @@ -61,7 +61,7 @@ SELECT * /* 4. Experiment with selecting a different column instead of just a string value */ -FROM vendor/* MODULE 2 */ +FROM vendor/* MODULE 2 */ /* DISTINCT */ @@ -81,7 +81,7 @@ SELECT customer_id FROM customer_purchases -/* 4. Which vendor has sold products to a customer ... and which product was it * / +/* 4. Which vendor has sold products to a customer ... and which product was it */ @@ -89,7 +89,7 @@ SELECT customer_id FROM customer_purchases ... and which product was it? ... AND to whom was it sold*/ -/* MODULE 2 */ +/* MODULE 2 */ /* INNER JOIN */ @@ -109,7 +109,7 @@ SELECT customer_id FROM customer_purchases -- using table aliases -/* MODULE 2 */ +/* MODULE 2 */ /* LEFT JOIN */ @@ -141,7 +141,7 @@ LEFT JOIN product AS p ...Note how the row count changed from 24 to 23 */ -/* MODULE 2 */ +/* MODULE 2 */ /* Multiple Table JOINs */ @@ -157,4 +157,4 @@ LEFT JOIN product AS p Why do we have more rows now?*/ -
+
diff --git a/04_this_cohort/live_code/Cohort_8/module_3/module_3.sqbpro b/04_this_cohort/live_code/Cohort_8/module_3/module_3.sqbpro index 3d421003d..d3b452b67 100644 --- a/04_this_cohort/live_code/Cohort_8/module_3/module_3.sqbpro +++ b/04_this_cohort/live_code/Cohort_8/module_3/module_3.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 3 */ +
/* MODULE 3 */ /* COUNT */ @@ -18,7 +18,7 @@ 4. How many unique products were bought */ -/* MODULE 3 */ +/* MODULE 3 */ /* SUM & AVG */ @@ -29,7 +29,7 @@ /* 2. How much does each customer spend on average */ -/* MODULE 3 */ +/* MODULE 3 */ /* MIN & MAX */ @@ -54,7 +54,7 @@ ... not particularly useful? */ -/* MODULE 3 */ +/* MODULE 3 */ /* Arithmitic */ @@ -68,7 +68,7 @@ SELECT /* 3. What about every third? */ -/* MODULE 3 */ +/* MODULE 3 */ /* HAVING */ @@ -81,7 +81,7 @@ Filter to customer_id between 1 and 5 and total_cost > 50 /* 2. How many products were bought? Filter to number of purchases between 300 and 500 */ -/* MODULE 3 */ +/* MODULE 3 */ /* Subquery FROM */ @@ -94,7 +94,7 @@ Filter to number of purchases between 300 and 500 */ /* 2. What is the single item that has been bought in the greatest quantity?*/ -/* MODULE 3 */ +/* MODULE 3 */ /* Subquery WHERE */ @@ -105,7 +105,7 @@ Filter to number of purchases between 300 and 500 */ /* 2. What is the name of the vendor who sells pie */ -/* MODULE 3 */ +/* MODULE 3 */ /* Common Table Expression (CTE) */ @@ -118,7 +118,7 @@ SELECT /* ... re-aggregate the daily sales for each WEEK instead now */ -/* MODULE 3 */ +/* MODULE 3 */ /* Temp Tables */ @@ -142,7 +142,7 @@ CREATE TABLE temp.new_vendor_inventory AS /* 2. put the previous table into another temp table, e.g. as temp.new_new_vendor_inventory */ -/* MODULE 3 */ +/* MODULE 3 */ /* Date functions */ diff --git a/04_this_cohort/live_code/Cohort_8/module_4/module_4.sqbpro b/04_this_cohort/live_code/Cohort_8/module_4/module_4.sqbpro index e2d101fad..b1f922c2e 100644 --- a/04_this_cohort/live_code/Cohort_8/module_4/module_4.sqbpro +++ b/04_this_cohort/live_code/Cohort_8/module_4/module_4.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 4 */ +
/* MODULE 4 */ /* NULL Management */ @@ -18,7 +18,7 @@ finding values in the product_size column that are "blank" strings and filtering which rows are null or blank */ -/* MODULE 4 */ +/* MODULE 4 */ /* NULLIF Budget (example from the slides) */ /* The following example creates a budgets table to show a department (dept) @@ -59,26 +59,11 @@ FROM budgets https://learn.microsoft.com/en-us/sql/t-sql/language-elements/nullif-transact-sql?view=sql-server-ver17 */ -/* MODULE 4 */ +/* MODULE 4 */ /* Windowed functions: row_number */ /* 1. What product is the highest price per vendor */ -SELECT * - -FROM ( - SELECT - vendor_id - ,market_date - ,product_id - ,original_price - ,ROW_NUMBER() OVER (PARTITION BY vendor_id ORDER BY original_price DESC) as price_rn - - FROM vendor_inventory -) x - -WHERE x.price_rn = 1; - @@ -92,7 +77,7 @@ GROUP BY vendor_id--,product_id */ -/* MODULE 4 */ +/* MODULE 4 */ /* Windowed functions: dense_rank, rank, row_number */ @@ -120,7 +105,7 @@ VALUES (10, 100000); -/* MODULE 4 */ +/* MODULE 4 */ /* Windowed functions: NTILE */ @@ -142,7 +127,7 @@ VALUES JOIN vendor AS v ON v.vendor_id = cp.vendor_id - GROUP BY cp.market_date, v.vendor_id/* MODULE 4 */ + GROUP BY cp.market_date, v.vendor_id/* MODULE 4 */ /* String Manipulations */ @@ -162,7 +147,7 @@ SELECT /* 7. unicode, char */ -/* 8. REGEXP in a WHERE statement *//* MODULE 4 */ +/* 8. REGEXP in a WHERE statement *//* MODULE 4 */ /* Substring & instring together */ @@ -182,12 +167,12 @@ SELECT INSTR('FirstWord, SecondWord, ThirdWord',',')+1)) ,',') + INSTR('FirstWord, SecondWord, ThirdWord',',')+1) AS ThirdDelim -/* MODULE 4 */ +/* MODULE 4 */ /* UNION */ /* 1. Find the most and least expensive product by vendor with UNION (and row_number!) */ -/* MODULE 4 */ +/* MODULE 4 */ /* UNION */ /* 1. Emulate a FULL OUTER JOIN with a UNION */ @@ -214,7 +199,7 @@ quantity INT INSERT INTO temp.store2 VALUES("tiger",2), ("dancer",7), - ("superhero", 5);/* MODULE 4 */ + ("superhero", 5);/* MODULE 4 */ /* INTERSECT & EXCEPT */ /* 1. Find products that have been sold (e.g. are in customer purchases AND product) */ diff --git a/04_this_cohort/live_code/Cohort_8/module_5/module_5.sqbpro b/04_this_cohort/live_code/Cohort_8/module_5/module_5.sqbpro index 84bbfec3b..f608313e0 100644 --- a/04_this_cohort/live_code/Cohort_8/module_5/module_5.sqbpro +++ b/04_this_cohort/live_code/Cohort_8/module_5/module_5.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 5 */ +
/* MODULE 5 */ /* INSERT UPDATE DELETE */ @@ -16,7 +16,7 @@ CREATE TEMP TABLE product_expanded AS -/* 3. delete the newly added product *//* MODULE 5 */ +/* 3. delete the newly added product *//* MODULE 5 */ /* VIEW */ /* 1. Create a vendor daily sales view */ @@ -38,14 +38,14 @@ CREATE TEMP TABLE product_expanded AS GROUP BY cp.market_date, v.vendor_id; -/* MODULE 5 */ +/* MODULE 5 */ /* VIEW in another query */ /* 1. Transform the daily sales view into a sales by vendor per week result */ -/* MODULE 5 */ +/* MODULE 5 */ /* UPDATE statements for view */ @@ -67,7 +67,7 @@ VALUES('....','....','....','....','8:00 AM','2:00 PM','nothing interesting','Su */ -/* MODULE 5 */ +/* MODULE 5 */ /* DYNAMIC VIEW */ @@ -105,7 +105,7 @@ VALUES('....','....','....','....','8:00 AM','2:00 PM','nothing interesting','Su -/* MODULE 5 */ +/* MODULE 5 */ /* CROSS JOIN */ @@ -123,7 +123,7 @@ SELECT * FROM TEMP.sizes; -/* MODULE 5 */ +/* MODULE 5 */ /* SELF JOIN */ diff --git a/04_this_cohort/live_code/DC/module_2/CASE.sql b/04_this_cohort/live_code/DC/module_2/CASE.sql new file mode 100644 index 000000000..4213db036 --- /dev/null +++ b/04_this_cohort/live_code/DC/module_2/CASE.sql @@ -0,0 +1,40 @@ +/* MODULE 2 */ +/* CASE */ + + +SELECT * +/* 1. Add a CASE statement declaring which days vendors should come */ +,CASE WHEN vendor_type = 'Fresh Focused' THEN 'Wednesday' + WHEN vendor_type = 'Prepared Foods' THEN 'Thursday' + ELSE 'Saturday' +END as day_of_specialty + +/* 2. Add another CASE statement for Pie Day */ +,CASE WHEN vendor_name = "Annie's Pies" -- double quotes will work here! + THEN 'Annie is great' + END as pie_day + + +/* 3. Add another CASE statement with an ELSE clause to handle rows evaluating to False */ +,CASE WHEN vendor_name LIKE '%pie%' + THEN 'Wednesday' + ELSE 'Friday' + END as also_pie_day + + + +FROM vendor; + + +/* 4. Experiment with selecting a different column instead of just a string value */ +SELECT * +,CASE WHEN cost_to_customer_per_qty < '1.00' +THEN cost_to_customer_per_qty*5 +ELSE cost_to_customer_per_qty +END AS inflation + +FROM customer_purchases + + + + diff --git a/04_this_cohort/live_code/DC/module_2/DISTINCT.sql b/04_this_cohort/live_code/DC/module_2/DISTINCT.sql new file mode 100644 index 000000000..49706d216 --- /dev/null +++ b/04_this_cohort/live_code/DC/module_2/DISTINCT.sql @@ -0,0 +1,41 @@ +/* MODULE 2 */ +/* DISTINCT */ + + +/* 1. Compare how many customer_ids are the customer_purchases table, one select with distinct, one without */ + +-- 4221 rows +SELECT customer_id FROM customer_purchases; + +SELECT DISTINCT customer_id FROM customer_purchases; + + + +/* 2. Compare the difference between selecting market_day in market_date_info, with and without distinct: + what do these difference mean?*/ +-- market is open for 150 days + SELECT market_day + FROM market_date_info; + + -- market is only open on 2 days, wed and sat + SELECT DISTINCT market_day + FROM market_date_info; + + + +/* 3. Which vendor has sold products to a customer */ +-- 3 vendors have sold products +SELECT DISTINCT vendor_id +FROM customer_purchases; + + +/* 4. Which vendor has sold products to a customer ... and which product was it */ +SELECT DISTINCT vendor_id, product_id +FROM customer_purchases; + + +/* 5. Which vendor has sold products to a customer +... and which product was it? +... AND to whom was it sold*/ +SELECT DISTINCT vendor_id, product_id,customer_id +FROM customer_purchases diff --git a/04_this_cohort/live_code/DC/module_2/INNER_JOIN.sql b/04_this_cohort/live_code/DC/module_2/INNER_JOIN.sql new file mode 100644 index 000000000..af5590478 --- /dev/null +++ b/04_this_cohort/live_code/DC/module_2/INNER_JOIN.sql @@ -0,0 +1,49 @@ +/* MODULE 2 */ +/* INNER JOIN */ + + +/* 1. Get product names (from product table) alongside customer_purchases + ... use an INNER JOIN to see only products that have been purchased */ + +-- without table aliases +SELECT product_name, -- coming from the product TABLE +vendor_id, -- rest are coming from customer_purchases +market_date, +customer_id, +customer_purchases.product_id, +product.product_id + +FROM product +INNER JOIN customer_purchases + ON customer_purchases.product_id = product.product_id; + + + +/* 2. Using the Query #4 from DISTINCT earlier + (Which vendor has sold products to a customer AND which product was it AND to whom was it sold) + + Add customers' first and last names with an INNER JOIN */ + +-- using table aliases +SELECT DISTINCT +vendor_id, +product_id, +c.customer_id, +customer_first_name, -- coming from customer +customer_last_name -- coming from customer + + +FROM customer_purchases as cp +INNER JOIN customer as c + ON c.customer_id = cp.customer_id; + +SELECT DISTINCT product_name, -- coming from the product TABLE +customer_purchases.product_id, +product.product_id + +FROM product +INNER JOIN customer_purchases + ON customer_purchases.product_id = product.product_id; + + + diff --git a/04_this_cohort/live_code/DC/module_2/LEFT_JOIN.sql b/04_this_cohort/live_code/DC/module_2/LEFT_JOIN.sql new file mode 100644 index 000000000..d28fa1944 --- /dev/null +++ b/04_this_cohort/live_code/DC/module_2/LEFT_JOIN.sql @@ -0,0 +1,64 @@ +/* MODULE 2 */ +/* LEFT JOIN */ + + +/* 1. There are products that have been bought +... but are there products that have not been bought? +Use a LEFT JOIN to find out*/ +SELECT DISTINCT +p.product_id +,cp.product_id as [cp.product_id] +,product_name + +FROM product as p +LEFT JOIN customer_purchases as cp + ON p.product_id = cp.product_id; + + + +/* 2. Directions of LEFT JOINs matter ...*/ +-- only products that have been sold ... because there are no product ids in CP that ARENT in product +SELECT DISTINCT +p.product_id +,cp.product_id as [cp.product_id] +,product_name + +FROM customer_purchases as p +LEFT JOIN product as cp + ON p.product_id = cp.product_id + + +/* 3. As do which table's values you filter on ... */ +SELECT DISTINCT +p.product_id +,cp.product_id as [cp.product_id] +,product_name + +FROM product as p +LEFT JOIN customer_purchases as cp + ON p.product_id = cp.product_id + +WHERE p.product_id BETWEEN 1 AND 6 -- if we pick product, 6 rows (1-6), otherwise 5 rows because zinnias not been bought + + + +/* 4. Without using a RIGHT JOIN, make this query return the RIGHT JOIN result set +...**Hint, flip the order of the joins** ... + +SELECT * + +FROM product_category AS pc +LEFT JOIN product AS p + ON pc.product_category_id = p.product_category_id + ORDER by pc.product_category_id + +...Note how the row count changed from 24 to 23 +*/ + +SELECT * + +FROM product AS p +LEFT JOIN product_category AS pc + ON pc.product_category_id = p.product_category_id + ORDER by pc.product_category_id + diff --git a/04_this_cohort/live_code/DC/module_2/SELECT.sql b/04_this_cohort/live_code/DC/module_2/SELECT.sql new file mode 100644 index 000000000..bb75d8768 --- /dev/null +++ b/04_this_cohort/live_code/DC/module_2/SELECT.sql @@ -0,0 +1,28 @@ +/* MODULE 2 */ +/* SELECT */ + + +/* 1. Select everything in the customer table */ +SELECT * +FROM customer; + +/* 2. Use sql as a calculator */ +SELECT 1+1 as something, 10*5 as somethingelse, pi() as pi; + + +/* 3. Add order by and limit clauses */ +SELECT * +FROM customer +ORDER BY customer_first_name +LIMIT 10; + + +/* 4. Select multiple specific columns */ +SELECT customer_id, customer_first_name +FROM customer; + + + +/* 5. Add a static value in a column */ +SELECT 2025 as this_year, 'October' as this_month, customer_id +FROM customer diff --git a/04_this_cohort/live_code/DC/module_2/WHERE.sql b/04_this_cohort/live_code/DC/module_2/WHERE.sql new file mode 100644 index 000000000..01d9f8036 --- /dev/null +++ b/04_this_cohort/live_code/DC/module_2/WHERE.sql @@ -0,0 +1,50 @@ +/* MODULE 2 */ +/* WHERE */ + +/* 1. Select only customer 1 from the customer table */ +SELECT * +FROM customer +WHERE customer_id = 1; + + +/* 2. Differentiate between AND and OR */ +SELECT * +FROM customer +WHERE customer_id = 1 +OR customer_id = 2; + + +/* 3. IN */ +SELECT * +FROM customer +WHERE customer_id IN (3,4,5) +OR customer_postal_code IN ('M4M','M1L'); -- customers in these postal codes + + +/* 4. LIKE */ +-- all the peppers +SELECT * FROM product +WHERE product_name LIKE '%pepper%'; + +--- customer with a last name starting with a +SELECT * FROM customer +WHERE customer_last_name LIKE 'a%'; + +/* 5. Nulls and Blanks*/ + +SELECT * FROM product +WHERE product_size IS NULL -- null +OR product_size = ''; -- blank, two single quotes not one double quote -- different from NULL + + +/* 6. BETWEEN x AND y */ +SELECT * +FROM customer +WHERE customer_id BETWEEN 1 AND 20; +--dates + +SELECT market_date, market_day, market_year +FROM market_date_info + +WHERE market_date BETWEEN '2022-10-01' AND '2022-10-31' +AND market_date = 'Wednesday'; diff --git a/04_this_cohort/live_code/DC/module_2/module_2.sqbpro b/04_this_cohort/live_code/DC/module_2/module_2.sqbpro index 73ace631b..bdca500ba 100644 --- a/04_this_cohort/live_code/DC/module_2/module_2.sqbpro +++ b/04_this_cohort/live_code/DC/module_2/module_2.sqbpro @@ -1,94 +1,162 @@ -
/* MODULE 2 */ +
/* MODULE 2 */ /* SELECT */ /* 1. Select everything in the customer table */ -SELECT +SELECT * +FROM customer; /* 2. Use sql as a calculator */ - +SELECT 1+1 as something, 10*5 as somethingelse, pi() as pi; /* 3. Add order by and limit clauses */ +SELECT * +FROM customer +ORDER BY customer_first_name +LIMIT 10; - -/* 4. Select multiple specific columns * / +/* 4. Select multiple specific columns */ +SELECT customer_id, customer_first_name +FROM customer; -/* 5. Add a static value in a column *//* MODULE 2 */ +/* 5. Add a static value in a column */ +SELECT 2025 as this_year, 'October' as this_month, customer_id +FROM customer +/* MODULE 2 */ /* WHERE */ /* 1. Select only customer 1 from the customer table */ SELECT * FROM customer -WHERE +WHERE customer_id = 1; /* 2. Differentiate between AND and OR */ - +SELECT * +FROM customer +WHERE customer_id = 1 +OR customer_id = 2; /* 3. IN */ - +SELECT * +FROM customer +WHERE customer_id IN (3,4,5) +OR customer_postal_code IN ('M4M','M1L'); -- customers in these postal codes /* 4. LIKE */ +-- all the peppers +SELECT * FROM product +WHERE product_name LIKE '%pepper%'; +--- customer with a last name starting with a +SELECT * FROM customer +WHERE customer_last_name LIKE 'a%'; +/* 5. Nulls and Blanks*/ -/* 5. Nulls and Blanks* / +SELECT * FROM product +WHERE product_size IS NULL -- null +OR product_size = ''; -- blank, two single quotes not one double quote -- different from NULL +/* 6. BETWEEN x AND y */ +SELECT * +FROM customer +WHERE customer_id BETWEEN 1 AND 20; +--dates + +SELECT market_date, market_day, market_year +FROM market_date_info -/* 6. BETWEEN x AND y *//* MODULE 2 */ +WHERE market_date BETWEEN '2022-10-01' AND '2022-10-31' +AND market_date = 'Wednesday'; +/* MODULE 2 */ /* CASE */ SELECT * /* 1. Add a CASE statement declaring which days vendors should come */ - +,CASE WHEN vendor_type = 'Fresh Focused' THEN 'Wednesday' + WHEN vendor_type = 'Prepared Foods' THEN 'Thursday' + ELSE 'Saturday' +END as day_of_specialty /* 2. Add another CASE statement for Pie Day */ - +,CASE WHEN vendor_name = "Annie's Pies" -- double quotes will work here! + THEN 'Annie is great' + END as pie_day /* 3. Add another CASE statement with an ELSE clause to handle rows evaluating to False */ +,CASE WHEN vendor_name LIKE '%pie%' + THEN 'Wednesday' + ELSE 'Friday' + END as also_pie_day + +FROM vendor; + /* 4. Experiment with selecting a different column instead of just a string value */ +SELECT * +,CASE WHEN cost_to_customer_per_qty < '1.00' +THEN cost_to_customer_per_qty*5 +ELSE cost_to_customer_per_qty +END AS inflation + +FROM customer_purchases + + -FROM vendor/* MODULE 2 */ +/* MODULE 2 */ /* DISTINCT */ /* 1. Compare how many customer_ids are the customer_purchases table, one select with distinct, one without */ -- 4221 rows -SELECT customer_id FROM customer_purchases +SELECT customer_id FROM customer_purchases; + +SELECT DISTINCT customer_id FROM customer_purchases; /* 2. Compare the difference between selecting market_day in market_date_info, with and without distinct: what do these difference mean?*/ - +-- market is open for 150 days + SELECT market_day + FROM market_date_info; + + -- market is only open on 2 days, wed and sat + SELECT DISTINCT market_day + FROM market_date_info; + /* 3. Which vendor has sold products to a customer */ +-- 3 vendors have sold products +SELECT DISTINCT vendor_id +FROM customer_purchases; - -/* 4. Which vendor has sold products to a customer ... and which product was it * / - +/* 4. Which vendor has sold products to a customer ... and which product was it */ +SELECT DISTINCT vendor_id, product_id +FROM customer_purchases; /* 5. Which vendor has sold products to a customer ... and which product was it? ... AND to whom was it sold*/ - +SELECT DISTINCT vendor_id, product_id,customer_id +FROM customer_purchases /* MODULE 2 */ /* INNER JOIN */ @@ -97,7 +165,16 @@ SELECT customer_id FROM customer_purchases ... use an INNER JOIN to see only products that have been purchased */ -- without table aliases +SELECT product_name, -- coming from the product TABLE +vendor_id, -- rest are coming from customer_purchases +market_date, +customer_id, +customer_purchases.product_id, +product.product_id +FROM product +INNER JOIN customer_purchases + ON customer_purchases.product_id = product.product_id; @@ -107,6 +184,26 @@ SELECT customer_id FROM customer_purchases Add customers' first and last names with an INNER JOIN */ -- using table aliases +SELECT DISTINCT +vendor_id, +product_id, +c.customer_id, +customer_first_name, -- coming from customer +customer_last_name -- coming from customer + + +FROM customer_purchases as cp +INNER JOIN customer as c + ON c.customer_id = cp.customer_id; + +SELECT DISTINCT product_name, -- coming from the product TABLE +customer_purchases.product_id, +product.product_id + +FROM product +INNER JOIN customer_purchases + ON customer_purchases.product_id = product.product_id; + /* MODULE 2 */ @@ -116,15 +213,40 @@ SELECT customer_id FROM customer_purchases /* 1. There are products that have been bought ... but are there products that have not been bought? Use a LEFT JOIN to find out*/ +SELECT DISTINCT +p.product_id +,cp.product_id as [cp.product_id] +,product_name + +FROM product as p +LEFT JOIN customer_purchases as cp + ON p.product_id = cp.product_id; + /* 2. Directions of LEFT JOINs matter ...*/ +-- only products that have been sold ... because there are no product ids in CP that ARENT in product +SELECT DISTINCT +p.product_id +,cp.product_id as [cp.product_id] +,product_name +FROM customer_purchases as p +LEFT JOIN product as cp + ON p.product_id = cp.product_id +/* 3. As do which table's values you filter on ... */ +SELECT DISTINCT +p.product_id +,cp.product_id as [cp.product_id] +,product_name -/* 3. As do which values you filter on ... */ +FROM product as p +LEFT JOIN customer_purchases as cp + ON p.product_id = cp.product_id +WHERE p.product_id BETWEEN 1 AND 6 -- if we pick product, 6 rows (1-6), otherwise 5 rows because zinnias not been bought @@ -141,7 +263,14 @@ LEFT JOIN product AS p ...Note how the row count changed from 24 to 23 */ -/* MODULE 2 */ +SELECT * + +FROM product AS p +LEFT JOIN product_category AS pc + ON pc.product_category_id = p.product_category_id + ORDER by pc.product_category_id + +/* MODULE 2 */ /* Multiple Table JOINs */ @@ -149,12 +278,38 @@ LEFT JOIN product AS p (Which vendor has sold products to a customer AND which product was it AND to whom was it sold) Replace all the IDs (customer, vendor, and product) with the names instead*/ - - - -/* 2. Select product_category_name, everything from the product table, and then LEFT JOIN the customer_purchases table +SELECT DISTINCT +--v.vendor_id +vendor_name +--, product_id +,product_name +--,customer_id -- first/last name +,customer_first_name +,customer_last_name + +FROM customer_purchases as cp +INNER JOIN customer as c + ON c.customer_id = cp.customer_id +INNER JOIN vendor as v + ON v.vendor_id = cp.vendor_id +INNER JOIN product as p + ON p.product_id = cp.product_id; + + +/* 2. Select product_category_name, everything from the product table, +and then LEFT JOIN the customer_purchases table ... how does this LEFT JOIN affect the number of rows? Why do we have more rows now?*/ -
+SELECT product_category_name, p.*, cp.product_id as productid_in_purchases_table + +FROM product_category as pc +INNER JOIN product as p + ON p.product_category_id = pc.product_category_id +LEFT JOIN customer_purchases as cp + ON cp.product_id = p.product_id + +ORDER BY cp.product_id + +
diff --git a/04_this_cohort/live_code/DC/module_2/multiple_table_joins.sql b/04_this_cohort/live_code/DC/module_2/multiple_table_joins.sql new file mode 100644 index 000000000..9afdefafe --- /dev/null +++ b/04_this_cohort/live_code/DC/module_2/multiple_table_joins.sql @@ -0,0 +1,43 @@ +/* MODULE 2 */ +/* Multiple Table JOINs */ + + +/* 1. Using the Query #4 from DISTINCT earlier + (Which vendor has sold products to a customer AND which product was it AND to whom was it sold) + + Replace all the IDs (customer, vendor, and product) with the names instead*/ +SELECT DISTINCT +--v.vendor_id +vendor_name +--, product_id +,product_name +--,customer_id -- first/last name +,customer_first_name +,customer_last_name + +FROM customer_purchases as cp +INNER JOIN customer as c + ON c.customer_id = cp.customer_id +INNER JOIN vendor as v + ON v.vendor_id = cp.vendor_id +INNER JOIN product as p + ON p.product_id = cp.product_id; + + +/* 2. Select product_category_name, everything from the product table, +and then LEFT JOIN the customer_purchases table +... how does this LEFT JOIN affect the number of rows? + +Why do we have more rows now?*/ + +SELECT product_category_name, p.*, cp.product_id as productid_in_purchases_table + +FROM product_category as pc +INNER JOIN product as p + ON p.product_category_id = pc.product_category_id +LEFT JOIN customer_purchases as cp + ON cp.product_id = p.product_id + +ORDER BY cp.product_id + + \ No newline at end of file diff --git a/04_this_cohort/live_code/DC/module_3/COUNT.sql b/04_this_cohort/live_code/DC/module_3/COUNT.sql new file mode 100644 index 000000000..c12508adc --- /dev/null +++ b/04_this_cohort/live_code/DC/module_3/COUNT.sql @@ -0,0 +1,28 @@ +/* MODULE 3 */ +/* COUNT */ + + +/* 1. Count the number of products */ + SELECT COUNT(product_id) as num_of_product + FROM product; + + +/* 2. How many products per product_qty_type */ +SELECT product_qty_type, COUNT(product_id) as num_of_product +FROM product +GROUP BY product_qty_type; + +/* 3. How many products per product_qty_type and per their product_size */ +SELECT product_size +,product_qty_type +, COUNT(product_id) as num_of_product +FROM product +GROUP BY product_size, product_qty_type + +ORDER BY product_qty_type; + +/* COUNT DISTINCT + 4. How many unique products were bought */ +SELECT COUNT(DISTINCT product_id) as bought_prods +FROM customer_purchases + diff --git a/04_this_cohort/live_code/DC/module_3/CTEs.sql b/04_this_cohort/live_code/DC/module_3/CTEs.sql new file mode 100644 index 000000000..fd6b2f2ed --- /dev/null +++ b/04_this_cohort/live_code/DC/module_3/CTEs.sql @@ -0,0 +1,38 @@ +/* MODULE 3 */ +/* Common Table Expression (CTE) */ + + +/* 1. Calculate sales per vendor per day */ +WITH vendor_daily_sales AS ( + SELECT + md.market_date + ,market_day + ,market_week + ,market_year + ,vendor_name + ,SUM(quantity*cost_to_customer_per_qty) as sales + + FROM customer_purchases cp + INNER JOIN vendor v -- we want the vendor_name + ON v.vendor_id = cp.vendor_id + INNER JOIN market_date_info md -- all the date columns + ON cp.market_date = md.market_date + + GROUP BY md.market_date, v.vendor_id + +) + + +/* ... re-aggregate the daily sales for each WEEK instead now */ + +SELECT +market_year +,market_week +,vendor_name +,SUM(sales) as sales + +FROM vendor_daily_sales +GROUP BY market_year,market_week, vendor_name + + + diff --git a/04_this_cohort/live_code/DC/module_3/HAVING.sql b/04_this_cohort/live_code/DC/module_3/HAVING.sql new file mode 100644 index 000000000..87b974030 --- /dev/null +++ b/04_this_cohort/live_code/DC/module_3/HAVING.sql @@ -0,0 +1,26 @@ +/* MODULE 3 */ +/* HAVING */ + + +/* 1. How much did a customer spend on each day? +Filter to customer_id between 1 and 5 and total_spend > 50 +... What order of execution occurs?*/ +SELECT +market_date +,customer_id +,SUM(quantity*cost_to_customer_per_qty) as total_spend + +FROM customer_purchases +WHERE customer_id BETWEEN 1 AND 5 + +GROUP BY market_date, customer_id +HAVING total_spend > 50; + +/* 2. How many products were bought? +Filter to number of purchases between 300 and 500 */ +SELECT count(product_id) as num_of_prod, product_id +FROM customer_purchases +GROUP BY product_id +HAVING count(product_id) BETWEEN 300 AND 500 + + diff --git a/04_this_cohort/live_code/DC/module_3/MIN_MAX.sql b/04_this_cohort/live_code/DC/module_3/MIN_MAX.sql new file mode 100644 index 000000000..6b766e99c --- /dev/null +++ b/04_this_cohort/live_code/DC/module_3/MIN_MAX.sql @@ -0,0 +1,50 @@ +/* MODULE 3 */ +/* MIN & MAX */ + + +/* 1. What is the most expensive product +...pay attention to how it doesn't handle ties very well +*/ +SELECT product_name, max(original_price) as most_expensive + +FROM vendor_inventory as vi +INNER JOIN product as p + ON p.product_id = vi.product_id; + + +/* 2. Prove that max is working */ +SELECT DISTINCT +product_name, +original_price + +FROM vendor_inventory as vi +INNER JOIN product as p + ON p.product_id = vi.product_id; + + +/* 3. Find the minimum price per each product_qty_type */ +SELECT product_name +,product_qty_type +,min(original_price) + +FROM vendor_inventory as vi +INNER JOIN product as p + ON p.product_id = vi.product_id + +GROUP BY product_qty_type; + +/* 4. Prove that min is working */ +SELECT DISTINCT product_name +,product_qty_type +--,min(original_price) +,original_price + +FROM vendor_inventory as vi +INNER JOIN product as p + ON p.product_id = vi.product_id; + +/* 5. Min/max on a string +... not particularly useful? */ +SELECT max(product_name) +FROM product + diff --git a/04_this_cohort/live_code/DC/module_3/SUM_AVG.sql b/04_this_cohort/live_code/DC/module_3/SUM_AVG.sql new file mode 100644 index 000000000..3bdc76052 --- /dev/null +++ b/04_this_cohort/live_code/DC/module_3/SUM_AVG.sql @@ -0,0 +1,27 @@ +/* MODULE 3 */ +/* SUM & AVG */ + + +/* 1. How much did customers spend each (per) day */ +SELECT +market_date +,customer_id +,SUM(quantity*cost_to_customer_per_qty) as total_spend + +FROM customer_purchases +GROUP BY market_date,customer_id; + + +/* 2. How much does each customer spend on average */ +SELECT +customer_first_name +,customer_last_name +,ROUND(AVG(quantity*cost_to_customer_per_qty),2) as avg_spend + +FROM customer_purchases as cp +INNER JOIN customer as c + ON c.customer_id = cp.customer_id + +GROUP BY c.customer_id + + diff --git a/04_this_cohort/live_code/DC/module_3/arithmitic.sql b/04_this_cohort/live_code/DC/module_3/arithmitic.sql new file mode 100644 index 000000000..eb056cf1e --- /dev/null +++ b/04_this_cohort/live_code/DC/module_3/arithmitic.sql @@ -0,0 +1,18 @@ +/* MODULE 3 */ +/* Arithmitic */ + + +/* 1. power, pi(), ceiling, division, integer division, etc */ +SELECT power(4,2), pi(); + +SELECT 10.0 / 3.0 as division, +CAST(10.0 as INT) / CAST(3.0 as INT) as integer_division; + +/* 2. Every even vendor_id with modulo */ +SELECT * FROM vendor +WHERE vendor_id % 2 = 0; + +/* 3. What about every third? */ +SELECT * FROM vendor +WHERE vendor_id % 3 = 0; + diff --git a/04_this_cohort/live_code/DC/module_3/module_3.sqbpro b/04_this_cohort/live_code/DC/module_3/module_3.sqbpro index 3d421003d..bb625b930 100644 --- a/04_this_cohort/live_code/DC/module_3/module_3.sqbpro +++ b/04_this_cohort/live_code/DC/module_3/module_3.sqbpro @@ -1,124 +1,226 @@ -
/* MODULE 3 */ +
/* MODULE 3 */ /* COUNT */ /* 1. Count the number of products */ - + SELECT COUNT(product_id) as num_of_product + FROM product; /* 2. How many products per product_qty_type */ - - +SELECT product_qty_type, COUNT(product_id) as num_of_product +FROM product +GROUP BY product_qty_type; /* 3. How many products per product_qty_type and per their product_size */ +SELECT product_size +,product_qty_type +, COUNT(product_id) as num_of_product +FROM product +GROUP BY product_size, product_qty_type - +ORDER BY product_qty_type; /* COUNT DISTINCT 4. How many unique products were bought */ +SELECT COUNT(DISTINCT product_id) as bought_prods +FROM customer_purchases - -/* MODULE 3 */ +/* MODULE 3 */ /* SUM & AVG */ -/* 1. How much did customers spend each day */ +/* 1. How much did customers spend each (per) day */ +SELECT +market_date +,customer_id +,SUM(quantity*cost_to_customer_per_qty) as total_spend +FROM customer_purchases +GROUP BY market_date,customer_id; /* 2. How much does each customer spend on average */ +SELECT +customer_first_name +,customer_last_name +,ROUND(AVG(quantity*cost_to_customer_per_qty),2) as avg_spend +FROM customer_purchases as cp +INNER JOIN customer as c + ON c.customer_id = cp.customer_id -/* MODULE 3 */ +GROUP BY c.customer_id + + +/* MODULE 3 */ /* MIN & MAX */ /* 1. What is the most expensive product ...pay attention to how it doesn't handle ties very well */ +SELECT product_name, max(original_price) as most_expensive + +FROM vendor_inventory as vi +INNER JOIN product as p + ON p.product_id = vi.product_id; /* 2. Prove that max is working */ +SELECT DISTINCT +product_name, +original_price +FROM vendor_inventory as vi +INNER JOIN product as p + ON p.product_id = vi.product_id; /* 3. Find the minimum price per each product_qty_type */ +SELECT product_name +,product_qty_type +,min(original_price) +FROM vendor_inventory as vi +INNER JOIN product as p + ON p.product_id = vi.product_id +GROUP BY product_qty_type; /* 4. Prove that min is working */ +SELECT DISTINCT product_name +,product_qty_type +--,min(original_price) +,original_price - +FROM vendor_inventory as vi +INNER JOIN product as p + ON p.product_id = vi.product_id; /* 5. Min/max on a string ... not particularly useful? */ +SELECT max(product_name) +FROM product - -/* MODULE 3 */ +/* MODULE 3 */ /* Arithmitic */ /* 1. power, pi(), ceiling, division, integer division, etc */ -SELECT +SELECT power(4,2), pi(); +SELECT 10.0 / 3.0 as division, +CAST(10.0 as INT) / CAST(3.0 as INT) as integer_division; /* 2. Every even vendor_id with modulo */ - - +SELECT * FROM vendor +WHERE vendor_id % 2 = 0; /* 3. What about every third? */ +SELECT * FROM vendor +WHERE vendor_id % 3 = 0; -/* MODULE 3 */ +/* MODULE 3 */ /* HAVING */ /* 1. How much did a customer spend on each day? -Filter to customer_id between 1 and 5 and total_cost > 50 +Filter to customer_id between 1 and 5 and total_spend > 50 ... What order of execution occurs?*/ - +SELECT +market_date +,customer_id +,SUM(quantity*cost_to_customer_per_qty) as total_spend + +FROM customer_purchases +WHERE customer_id BETWEEN 1 AND 5 +GROUP BY market_date, customer_id +HAVING total_spend > 50; /* 2. How many products were bought? Filter to number of purchases between 300 and 500 */ +SELECT count(product_id) as num_of_prod, product_id +FROM customer_purchases +GROUP BY product_id +HAVING count(product_id) BETWEEN 300 AND 500 -/* MODULE 3 */ + +/* MODULE 3 */ /* Subquery FROM */ /*1. Simple subquery in a FROM statement, e.g. for inflation ...we could imagine joining this to a more complex query perhaps */ +SELECT DISTINCT +product_id, inflation - +FROM ( + SELECT product_id, cost_to_customer_per_qty, + CASE WHEN cost_to_customer_per_qty < '1.00' THEN cost_to_customer_per_qty*5 + ELSE cost_to_customer_per_qty END AS inflation + + FROM customer_purchases +); /* 2. What is the single item that has been bought in the greatest quantity?*/ +--outer query +SELECT product_name, MAX(quantity_purchased) + +FROM product AS p +INNER JOIN ( +-- inner query + SELECT product_id + ,count(quantity) as quantity_purchased + FROM customer_purchases + GROUP BY product_id +) AS x ON p.product_id = x.product_id -/* MODULE 3 */ +/* MODULE 3 */ /* Subquery WHERE */ /* 1. How much did each customer spend at each vendor for each day at the market WHEN IT RAINS */ +SELECT +market_date, +customer_id, +vendor_id, +SUM(quantity*cost_to_customer_per_qty) as total_spend +FROM customer_purchases +-- filter by rain_flag +-- "what dates was it raining" +WHERE market_date IN ( + SELECT market_date + FROM market_date_info + WHERE market_rain_flag = 1 +) -/* 2. What is the name of the vendor who sells pie */ - -/* MODULE 3 */ -/* Common Table Expression (CTE) */ - +GROUP BY market_date, vendor_id, customer_id; -/* 1. Calculate sales per vendor per day */ -SELECT +/* 2. What is the name of the vendor who sells pie */ +SELECT DISTINCT vendor_name +FROM customer_purchases as cp +INNER JOIN vendor as v + ON cp.vendor_id = v.vendor_id + +WHERE product_id IN ( + SELECT product_id + FROM product + WHERE product_name LIKE '%pie%' +) -/* ... re-aggregate the daily sales for each WEEK instead now */ -/* MODULE 3 */ +/* MODULE 3 */ /* Temp Tables */ @@ -134,15 +236,58 @@ DROP TABLE IF EXISTS temp.new_vendor_inventory; CREATE TABLE temp.new_vendor_inventory AS -- definition of the table +SELECT *, +original_price*5 as inflation +FROM vendor_inventory; +/* 2. put the previous table into another temp table, e.g. as temp.new_new_vendor_inventory */ +DROP TABLE IF EXISTS temp.new_new_vendor_inventory; +CREATE TABLE temp.new_new_vendor_inventory AS +SELECT * +,inflation*2 as super_inflation +FROM temp.new_vendor_inventory +/* MODULE 3 */ +/* Common Table Expression (CTE) */ + + +/* 1. Calculate sales per vendor per day */ +WITH vendor_daily_sales AS ( + SELECT + md.market_date + ,market_day + ,market_week + ,market_year + ,vendor_name + ,SUM(quantity*cost_to_customer_per_qty) as sales + + FROM customer_purchases cp + INNER JOIN vendor v -- we want the vendor_name + ON v.vendor_id = cp.vendor_id + INNER JOIN market_date_info md -- all the date columns + ON cp.market_date = md.market_date + + GROUP BY md.market_date, v.vendor_id + +) + + +/* ... re-aggregate the daily sales for each WEEK instead now */ + +SELECT +market_year +,market_week +,vendor_name +,SUM(sales) as sales + +FROM vendor_daily_sales +GROUP BY market_year,market_week, vendor_name -/* 2. put the previous table into another temp table, e.g. as temp.new_new_vendor_inventory */ -/* MODULE 3 */ +/* MODULE 3 */ /* Date functions */ diff --git a/04_this_cohort/live_code/DC/module_3/subquery_from.sql b/04_this_cohort/live_code/DC/module_3/subquery_from.sql new file mode 100644 index 000000000..c5d07d78b --- /dev/null +++ b/04_this_cohort/live_code/DC/module_3/subquery_from.sql @@ -0,0 +1,32 @@ +/* MODULE 3 */ +/* Subquery FROM */ + + +/*1. Simple subquery in a FROM statement, e.g. for inflation +...we could imagine joining this to a more complex query perhaps */ +SELECT DISTINCT +product_id, inflation + +FROM ( + SELECT product_id, cost_to_customer_per_qty, + CASE WHEN cost_to_customer_per_qty < '1.00' THEN cost_to_customer_per_qty*5 + ELSE cost_to_customer_per_qty END AS inflation + + FROM customer_purchases +); + + +/* 2. What is the single item that has been bought in the greatest quantity?*/ +--outer query +SELECT product_name, MAX(quantity_purchased) + +FROM product AS p +INNER JOIN ( +-- inner query + SELECT product_id + ,count(quantity) as quantity_purchased + + FROM customer_purchases + GROUP BY product_id +) AS x ON p.product_id = x.product_id + diff --git a/04_this_cohort/live_code/DC/module_3/subquery_where.sql b/04_this_cohort/live_code/DC/module_3/subquery_where.sql new file mode 100644 index 000000000..c24313b2b --- /dev/null +++ b/04_this_cohort/live_code/DC/module_3/subquery_where.sql @@ -0,0 +1,41 @@ +/* MODULE 3 */ +/* Subquery WHERE */ + + +/* 1. How much did each customer spend at each vendor for each day at the market WHEN IT RAINS */ + +SELECT +market_date, +customer_id, +vendor_id, +SUM(quantity*cost_to_customer_per_qty) as total_spend + +FROM customer_purchases + +-- filter by rain_flag +-- "what dates was it raining" +WHERE market_date IN ( + SELECT market_date + FROM market_date_info + WHERE market_rain_flag = 1 +) + +GROUP BY market_date, vendor_id, customer_id; + + + +/* 2. What is the name of the vendor who sells pie */ + +SELECT DISTINCT vendor_name + +FROM customer_purchases as cp +INNER JOIN vendor as v + ON cp.vendor_id = v.vendor_id + +WHERE product_id IN ( + SELECT product_id + FROM product + WHERE product_name LIKE '%pie%' +) + + diff --git a/04_this_cohort/live_code/DC/module_3/temp_tables.sql b/04_this_cohort/live_code/DC/module_3/temp_tables.sql new file mode 100644 index 000000000..c701bae81 --- /dev/null +++ b/04_this_cohort/live_code/DC/module_3/temp_tables.sql @@ -0,0 +1,29 @@ +/* MODULE 3 */ +/* Temp Tables */ + + +/* 1. Put our inflation query into a temp table, e.g. as temp.new_vendor_inventory*/ + +/* some structural code */ +/* ...heads up, sometimes this query can be finnicky -- it's good to try highlighting different sections to help it succeed...*/ + +-- if a table named new_vendor_inventory exists, delete it, other do NOTHING +DROP TABLE IF EXISTS temp.new_vendor_inventory; + +--make the table +CREATE TABLE temp.new_vendor_inventory AS + +-- definition of the table +SELECT *, +original_price*5 as inflation +FROM vendor_inventory; + + +/* 2. put the previous table into another temp table, e.g. as temp.new_new_vendor_inventory */ +DROP TABLE IF EXISTS temp.new_new_vendor_inventory; +CREATE TABLE temp.new_new_vendor_inventory AS + +SELECT * +,inflation*2 as super_inflation +FROM temp.new_vendor_inventory + diff --git a/04_this_cohort/live_code/DC/module_4/module_4.sqbpro b/04_this_cohort/live_code/DC/module_4/module_4.sqbpro index e2d101fad..b1f922c2e 100644 --- a/04_this_cohort/live_code/DC/module_4/module_4.sqbpro +++ b/04_this_cohort/live_code/DC/module_4/module_4.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 4 */ +
/* MODULE 4 */ /* NULL Management */ @@ -18,7 +18,7 @@ finding values in the product_size column that are "blank" strings and filtering which rows are null or blank */ -/* MODULE 4 */ +/* MODULE 4 */ /* NULLIF Budget (example from the slides) */ /* The following example creates a budgets table to show a department (dept) @@ -59,26 +59,11 @@ FROM budgets https://learn.microsoft.com/en-us/sql/t-sql/language-elements/nullif-transact-sql?view=sql-server-ver17 */ -/* MODULE 4 */ +/* MODULE 4 */ /* Windowed functions: row_number */ /* 1. What product is the highest price per vendor */ -SELECT * - -FROM ( - SELECT - vendor_id - ,market_date - ,product_id - ,original_price - ,ROW_NUMBER() OVER (PARTITION BY vendor_id ORDER BY original_price DESC) as price_rn - - FROM vendor_inventory -) x - -WHERE x.price_rn = 1; - @@ -92,7 +77,7 @@ GROUP BY vendor_id--,product_id */ -/* MODULE 4 */ +/* MODULE 4 */ /* Windowed functions: dense_rank, rank, row_number */ @@ -120,7 +105,7 @@ VALUES (10, 100000); -/* MODULE 4 */ +/* MODULE 4 */ /* Windowed functions: NTILE */ @@ -142,7 +127,7 @@ VALUES JOIN vendor AS v ON v.vendor_id = cp.vendor_id - GROUP BY cp.market_date, v.vendor_id/* MODULE 4 */ + GROUP BY cp.market_date, v.vendor_id/* MODULE 4 */ /* String Manipulations */ @@ -162,7 +147,7 @@ SELECT /* 7. unicode, char */ -/* 8. REGEXP in a WHERE statement *//* MODULE 4 */ +/* 8. REGEXP in a WHERE statement *//* MODULE 4 */ /* Substring & instring together */ @@ -182,12 +167,12 @@ SELECT INSTR('FirstWord, SecondWord, ThirdWord',',')+1)) ,',') + INSTR('FirstWord, SecondWord, ThirdWord',',')+1) AS ThirdDelim -/* MODULE 4 */ +/* MODULE 4 */ /* UNION */ /* 1. Find the most and least expensive product by vendor with UNION (and row_number!) */ -/* MODULE 4 */ +/* MODULE 4 */ /* UNION */ /* 1. Emulate a FULL OUTER JOIN with a UNION */ @@ -214,7 +199,7 @@ quantity INT INSERT INTO temp.store2 VALUES("tiger",2), ("dancer",7), - ("superhero", 5);/* MODULE 4 */ + ("superhero", 5);/* MODULE 4 */ /* INTERSECT & EXCEPT */ /* 1. Find products that have been sold (e.g. are in customer purchases AND product) */ diff --git a/04_this_cohort/live_code/DC/module_5/module_5.sqbpro b/04_this_cohort/live_code/DC/module_5/module_5.sqbpro index 84bbfec3b..f608313e0 100644 --- a/04_this_cohort/live_code/DC/module_5/module_5.sqbpro +++ b/04_this_cohort/live_code/DC/module_5/module_5.sqbpro @@ -1,4 +1,4 @@ -
/* MODULE 5 */ +
/* MODULE 5 */ /* INSERT UPDATE DELETE */ @@ -16,7 +16,7 @@ CREATE TEMP TABLE product_expanded AS -/* 3. delete the newly added product *//* MODULE 5 */ +/* 3. delete the newly added product *//* MODULE 5 */ /* VIEW */ /* 1. Create a vendor daily sales view */ @@ -38,14 +38,14 @@ CREATE TEMP TABLE product_expanded AS GROUP BY cp.market_date, v.vendor_id; -/* MODULE 5 */ +/* MODULE 5 */ /* VIEW in another query */ /* 1. Transform the daily sales view into a sales by vendor per week result */ -/* MODULE 5 */ +/* MODULE 5 */ /* UPDATE statements for view */ @@ -67,7 +67,7 @@ VALUES('....','....','....','....','8:00 AM','2:00 PM','nothing interesting','Su */ -/* MODULE 5 */ +/* MODULE 5 */ /* DYNAMIC VIEW */ @@ -105,7 +105,7 @@ VALUES('....','....','....','....','8:00 AM','2:00 PM','nothing interesting','Su -/* MODULE 5 */ +/* MODULE 5 */ /* CROSS JOIN */ @@ -123,7 +123,7 @@ SELECT * FROM TEMP.sizes; -/* MODULE 5 */ +/* MODULE 5 */ /* SELF JOIN */