diff --git a/02_activities/homework/homework_3.sql b/02_activities/homework/homework_3.sql index 3fe2589a2..b3ea3ff50 100644 --- a/02_activities/homework/homework_3.sql +++ b/02_activities/homework/homework_3.sql @@ -2,7 +2,10 @@ /* 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. */ - +Answer: +SELECT vendor_id, count(vendor_id) +FROM vendor_booth_assignments +GROUP BY vendor_id /* 2. The Farmer’s Market Customer Appreciation Committee wants to give a bumper sticker to everyone who has ever spent more than $2000 at the market. Write a query that generates a list @@ -10,6 +13,16 @@ 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. */ +Answer: +SELECT c.customer_first_name, +c.customer_last_name, +SUM(cp.quantity * cp.cost_to_customer_per_qty) AS total_spent +FROM customer c +JOIN customer_purchases 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 ASC, c.customer_first_name ASC --Temp Table @@ -24,6 +37,16 @@ When inserting the new vendor, you need to appropriately align the columns to be VALUES(col1,col2,col3,col4,col5) */ +Answer: +DROP TABLE IF EXISTS new_vendor; + +CREATE TEMP TABLE new_vendor AS + +SELECT * +FROM vendor; + +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'); -- Date @@ -32,9 +55,27 @@ 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! */ +Answer: +SELECT customer_id, + strftime('%m', market_date) AS month, + strftime('%Y', market_date) AS year +FROM customer_purchases; + +I assume that the column of 'market_date' is the date of every purchase. + + /* 2. Using the previous query as a base, determine how much money each customer spent in April 2022. 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!! */ +Answer: + +SELECT customer_id, + SUM(quantity * cost_to_customer_per_qty) AS total_spent +FROM customer_purchases +WHERE strftime('%m', market_date) = '04' + AND strftime('%Y', market_date) = '2022' +GROUP BY customer_id; +