diff --git a/02_activities/assignments/ER_Diagram.png b/02_activities/assignments/ER_Diagram.png new file mode 100644 index 000000000..1b2739d29 Binary files /dev/null and b/02_activities/assignments/ER_Diagram.png differ diff --git a/02_activities/assignments/assignment1.sql b/02_activities/assignments/assignment1.sql index 2e89fa7af..7c3a94f89 100644 --- a/02_activities/assignments/assignment1.sql +++ b/02_activities/assignments/assignment1.sql @@ -1,24 +1,23 @@ /* 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. */ -- option 1 - +select * from customer_purchases where product_id = 4 or product_id = 9; -- option 2 - +select * from customer_purchases where product_id in ( 4, 9); /*2. Write a query that returns all customer purchases and a new calculated column 'price' (quantity * cost_to_customer_per_qty), @@ -27,32 +26,45 @@ filtered by vendor 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 vendor_id >= 8 AND vendor_id <= 10; -- option 2 - +select * , (quantity * cost_to_customer_per_qty) as price from customer_purchases where vendor_id BETWEEN 8 and 10; --CASE /* 1. Products can be sold by the individual unit or by bulk measures like lbs. or oz. 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. */ - +SELECT PRODUCT_ID, PRODUCT_NAME, + CASE WHEN product_qty_type = 'unit' then 'unit' + else 'bulk' + end as prod_qty_type_condensed, + case when product_name like '%pepper%' then 1 + else 0 + end as pepper_flag + FROM PRODUCT; --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 v.vendor_id, v.vendor_name,v.vendor_type, v.vendor_owner_first_name, v.vendor_owner_last_name ,vb.booth_number +,vb.market_Date from vendor v inner join vendor_booth_assignments vb +on v.vendor_id = vb.vendor_id +order by v.vendor_name, vb.market_Date; /* SECTION 3 */ @@ -60,7 +72,10 @@ vendor_id field they both have in common, and sorts the result by vendor_name, t /* 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 v.vendor_id,v.vendor_name,count(vb.booth_number) +from vendor v inner join vendor_booth_assignments vb +on v.vendor_id = vb.vendor_id +group by v.vendor_id,v.vendor_name ; /* 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 @@ -68,7 +83,11 @@ 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 , sum(quantity * cost_to_customer_per_qty) as money_spent from customer c +inner join customer_purchases cp on c.customer_id = cp.customer_id +group by c.customer_first_name, c.customer_last_name +having money_spent > 2000 +order by c.customer_last_name, c.customer_first_name; --Temp Table /* 1. Insert the original vendor table into a temp.new_vendor and then add a 10th vendor: @@ -81,7 +100,13 @@ 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) */ +drop table if exists temp.new_vendor; +--make the TABLE +CREATE TEMP TABLE temp.new_vendor AS +--definition of the table +SELECT * FROM vendor; +insert into temp.new_vendor values (10, 'Thomas Superfood Store', 'Fresh Focused' ,'Thomas', 'Rosenthal'); -- Date @@ -90,7 +115,7 @@ 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', market_date) as month , strftime( '%Y', market_Date) as year , market_Date from customer_purchases; /* 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. @@ -98,3 +123,5 @@ 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', market_date) as month , strftime( '%Y', market_Date) as year , sum(quantity * cost_to_Customer_per_qty) as money_spent from customer_purchases +where month = '04' and year= '2022' group by customer_id;