diff --git a/02_activities/assignments/Microcredential_Cohort/assignment1.sql b/02_activities/assignments/Microcredential_Cohort/assignment1.sql index 2ec561e2a..0ef027ead 100644 --- a/02_activities/assignments/Microcredential_Cohort/assignment1.sql +++ b/02_activities/assignments/Microcredential_Cohort/assignment1.sql @@ -6,8 +6,8 @@ --SELECT /* 1. Write a query that returns everything in the customer table. */ --QUERY 1 - - +SELECT * +FROM customer; --END QUERY @@ -16,9 +16,10 @@ /* 2. Write a query that displays all of the columns and 10 rows from the customer table, sorted by customer_last_name, then customer_first_ name. */ --QUERY 2 - - - +SELECT * +FROM customer +ORDER by customer_last_name, customer_first_name +LIMIT 10; --END QUERY @@ -28,8 +29,13 @@ sorted by customer_last_name, then customer_first_ name. */ Limit to 25 rows of output. */ --QUERY 3 - - +SELECT cp.*,p.product_name +FROM customer_purchases As cp +JOIN Product as p +ON cp.product_id = p.product_id +WHERE cp.product_id IN (4,9) +ORDER By cp.product_id DESC +LIMIT 25; --END QUERY @@ -42,9 +48,15 @@ filtered by customer IDs between 8 and 10 (inclusive) using either: Limit to 25 rows of output. */ --QUERY 4 - - - +SELECT cp.*, +p.product_name, +(quantity * cost_to_customer_per_qty) AS price +FROM customer_purchases AS cp +JOIN Product as p +ON cp.product_id = p.product_id +WHERE cp.product_id BETWEEN 8 AND 10 +ORDER By cp.product_id DESC +LIMIT 25; --END QUERY @@ -56,9 +68,13 @@ columns and add a column called prod_qty_type_condensed that displays the word if the product_qty_type is “unit,” and otherwise displays the word “bulk.” */ --QUERY 5 - - - +SELECT product_id, product_name, +CASE +WHEN product_qty_type = "UNIT" THEN "UNIT" +ELSE "bulk" +END AS prod_qty_type +FROM product; + --END QUERY @@ -66,9 +82,16 @@ if the product_qty_type is “unit,” and otherwise displays the word “bulk. 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. */ --QUERY 6 - - - +SELECT product_id, product_name, +CASE +WHEN product_qty_type = "UNIT" THEN "UNIT" +ELSE "bulk" +END AS prod_qty_type, +CASE +WHEN product_name like '%pepper%' THEN 1 +ELSE 0 +END AS pepper_flag +FROM product; --END QUERY @@ -79,8 +102,12 @@ vendor_id field they both have in common, and sorts the result by market_date, t Limit to 24 rows of output. */ --QUERY 7 - - +SELECT v.*, vba.* +FROM vendor AS v +INNER JOIN vendor_booth_assignments AS vba +ON v.vendor_id = vba.vendor_id +ORDER BY vba.market_date ASC , v.vendor_name ASC +Limit 24; --END QUERY @@ -93,8 +120,15 @@ Limit to 24 rows of output. */ at the farmer’s market by counting the vendor booth assignments per vendor_id. */ --QUERY 8 - - +SELECT + v.vendor_id, + v.vendor_name, + COUNT(vba.vendor_id) AS count_vendor_booth_assignments +FROM vendor AS v +INNER JOIN vendor_booth_assignments AS vba + ON v.vendor_id = vba.vendor_id +GROUP BY v.vendor_id, v.vendor_name +ORDER BY v.vendor_name ASC; --END QUERY @@ -106,8 +140,14 @@ 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. */ --QUERY 9 +SELECT c.customer_id, c.customer_first_name,c.customer_last_name, sum(cp.quantity * cp.cost_to_customer_per_qty) As total_customer_purchases - +FROM customer AS c +INNER JOIN customer_purchases AS cp +ON c.customer_id = cp.customer_id +GROUP BY c.customer_id,c.customer_last_name, c.customer_first_name +HAVING total_customer_purchases > 2000 +ORDER BY c.customer_last_name, c.customer_first_name; --END QUERY @@ -125,11 +165,19 @@ VALUES(col1,col2,col3,col4,col5) */ --QUERY 10 +-- creating temp table +CREATE TABLE TEMP.new_vendor AS +SELECT * FROM vendor; +-- inserting row +INSERT INTO temp.new_vendor (vendor_id, vendor_name, vendor_type, vendor_owner_first_name, vendor_owner_last_name) +VALUES (10, 'Thomass Superfood Store', 'Fresh Focused', 'Thomas', 'Rosenthal'); ---END QUERY +-- Checking temp table +SELECT * FROM temp.new_vendor; +--END QUERY -- Date /*1. Get the customer_id, month, and year (in separate columns) of every purchase in the customer_purchases table. @@ -139,8 +187,11 @@ and year are! Limit to 25 rows of output. */ --QUERY 11 - - +SELECT cp.customer_id, cp.product_id, p.product_name, strftime('%m', cp.market_date) AS purchase_month, strftime('%Y', cp.market_date) AS purchase_year +From customer_purchases as cp +INNER JOIN product as p +ON cp.product_id = p.product_id +LIMIT 25; --END QUERY @@ -152,8 +203,17 @@ HINTS: you will need to AGGREGATE, GROUP BY, and filter... but remember, STRFTIME returns a STRING for your WHERE statement... AND be sure you remove the LIMIT from the previous query before aggregating!! */ --QUERY 12 - - - +SELECT cp.customer_id, c.customer_first_name, c.customer_last_name, p.product_name, +strftime('%m', cp.market_date) AS purchase_month, +strftime('%Y', cp.market_date) AS purchase_year, +sum(cp.quantity * cp.cost_to_customer_per_qty) AS total_purchases +From customer_purchases as cp +INNER JOIN customer AS c +ON c.customer_id = cp.customer_id +INNER JOIN product as p +ON cp.customer_id = c.customer_id +WHERE purchase_month='04' AND purchase_year='2022' +Group by cp.customer_id, c.customer_first_name,c.customer_last_name, p.product_name +ORDER by cp.customer_id; --END QUERY diff --git a/02_activities/assignments/Microcredential_Cohort/assignment_1.jpg b/02_activities/assignments/Microcredential_Cohort/assignment_1.jpg new file mode 100644 index 000000000..469f74d10 Binary files /dev/null and b/02_activities/assignments/Microcredential_Cohort/assignment_1.jpg differ