Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions 02_activities/homework/homework_3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@
/* 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_rentals
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
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_id, c.customer_last_name, c.customer_first_name, SUM(p.quantity * p.cost_to_customer_per_qty) AS total_spent
FROM customer c
INNER JOIN customer_purchases p ON c.customer_id = p.customer_id
GROUP BY c.customer_id, c.customer_last_name, c.customer_first_name
HAVING total_spent > 2000
ORDER BY c.customer_last_name, c.customer_first_name;



Expand All @@ -23,6 +32,9 @@ When inserting the new vendor, you need to appropriately align the columns to be
-> To insert the new row use VALUES, specifying the value you want for each column:
VALUES(col1,col2,col3,col4,col5)
*/
CREATE TEMPORARY TABLE new_vendor AS
SELECT *
FROM vendor;



Expand All @@ -32,9 +44,16 @@ 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! */

CREATE TEMPORARY TABLE new_vendor AS
SELECT *
FROM vendor;


/* 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!! */
INSERT INTO new_vendor (vendor_id, vendor_name, vendor_type, vendor_owner_last_name, vendor_owner_first_name)
VALUES (10, 'Thomass Superfood Store', 'Fresh Focused', 'Rosenthal', 'Thomas');