Skip to content
Open
Show file tree
Hide file tree
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 69 additions & 14 deletions 02_activities/assignments/Microcredential_Cohort/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/* 1. Write a query that returns everything in the customer table. */
--QUERY 1


SELECT * FROM customer;


--END QUERY
Expand All @@ -17,8 +17,7 @@
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

Expand All @@ -28,6 +27,7 @@ sorted by customer_last_name, then customer_first_ name. */
Limit to 25 rows of output. */
--QUERY 3

SELECT * FROM customer_purchases WHERE product_id IN (4,9) LIMIT 25;



Expand All @@ -43,7 +43,8 @@ Limit to 25 rows of output.
*/
--QUERY 4


SELECT *, (quantity*cost_to_customer_per_qty) AS price FROM customer_purchases
WHERE customer_id BETWEEN 8 and 10 LIMIT 25;


--END QUERY
Expand All @@ -56,7 +57,11 @@ 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_condensed
FROM product;


--END QUERY
Expand All @@ -67,7 +72,14 @@ add a column to the previous query called pepper_flag that outputs a 1 if the pr
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_condensed,
CASE WHEN product_name like '%pepper%' THEN 1
ELSE 0
END AS pepper_flag
FROM product;


--END QUERY
Expand All @@ -79,6 +91,10 @@ 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 * FROM
vendor ven INNER JOIN vendor_booth_assignments venba
ON ven.vendor_id = venba.vendor_id
ORDER BY market_date, vendor_name LIMIT 24;



Expand All @@ -93,7 +109,10 @@ Limit to 24 rows of output. */
at the farmer’s market by counting the vendor booth assignments per vendor_id. */
--QUERY 8


SELECT ven.vendor_name, count(venba.vendor_id) AS 'No of Times Booth rented' FROM
vendor ven INNER JOIN vendor_booth_assignments venba
ON ven.vendor_id = venba.vendor_id
GROUP BY vendor_name;


--END QUERY
Expand All @@ -106,8 +125,15 @@ 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 cust.customer_last_name,cust.customer_first_name FROM customer cust
JOIN
(
SELECT cpurchases.customer_id
FROM customer_purchases cpurchases
GROUP BY cpurchases.customer_id HAVING sum(quantity * cost_to_customer_per_qty) > 2000
) spent
ON cust.customer_id = spent.customer_id
ORDER BY cust.customer_last_name,cust.customer_first_name;

--END QUERY

Expand All @@ -124,10 +150,14 @@ When inserting the new vendor, you need to appropriately align the columns to be
VALUES(col1,col2,col3,col4,col5)
*/
--QUERY 10
DROP TABLE IF EXISTS new_vendor;
CREATE TEMP TABLE temp.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 store','Thomas','Rosenthal');



SELECT * FROM new_vendor;
--END QUERY


Expand All @@ -139,8 +169,23 @@ and year are!
Limit to 25 rows of output. */
--QUERY 11



SELECT customer_id,
CASE strftime('%m', market_date)
WHEN '01' THEN 'January'
WHEN '02' THEN 'February'
WHEN '03' THEN 'March'
WHEN '04' THEN 'April'
WHEN '05' THEN 'May'
WHEN '06' THEN 'June'
WHEN '07' THEN 'July'
WHEN '08' THEN 'August'
WHEN '09' THEN 'September'
WHEN '10' THEN 'October'
WHEN '11' THEN 'November'
WHEN '12' THEN 'December'
END AS 'month',
strftime('%Y', market_date) AS 'year'
FROM customer_purchases LIMIT 25;

--END QUERY

Expand All @@ -153,7 +198,17 @@ 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 customer_first_name, customer_last_name, spent.customer_spent
FROM customer cust
INNER JOIN
(
SELECT customer_id,
SUM(quantity*cost_to_customer_per_qty) AS customer_spent
FROM customer_purchases
WHERE strftime('%Y', market_date) = '2022' and strftime('%m', market_date) = '04'
GROUP BY customer_id
) spent
ON cust.customer_id = spent.customer_id;


--END QUERY