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
2 changes: 2 additions & 0 deletions 02_activities/assignments/DC_Cohort/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ The store wants to keep customer addresses. Propose two architectures for the CU
Your answer...
```



***

## Section 2:
Expand Down
97 changes: 71 additions & 26 deletions 02_activities/assignments/DC_Cohort/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
--SELECT
/* 1. Write a query that returns everything in the customer table. */
--QUERY 1



SELECT*
FROM customer;

--END QUERY


/* 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

Expand All @@ -27,9 +27,10 @@ sorted by customer_last_name, then customer_first_ name. */
/* 1. Write a query that returns all customer purchases of product IDs 4 and 9.
Limit to 25 rows of output. */
--QUERY 3



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

--END QUERY

Expand All @@ -42,9 +43,10 @@ filtered by customer IDs between 8 and 10 (inclusive) using either:
Limit to 25 rows of output.
*/
--QUERY 4



SELECT quantity*cost_per_quantity AS price
FROM customer_purchases
WHERE customer_id BETWEEN 8 AND 10
LIMIT 25;

--END QUERY

Expand All @@ -55,9 +57,14 @@ Using the product table, write a query that outputs the product_id and product_n
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.” */
--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 @@ -66,7 +73,18 @@ 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_condensed,
CASE
WHEN product_name LIKE '%pepper%' THEN 1
ELSE 0
END AS pepper_flag
FROM product;



Expand All @@ -78,9 +96,16 @@ contains the word “pepper” (regardless of capitalization), and otherwise out
vendor_id field they both have in common, and sorts the result by market_date, then vendor_name.
Limit to 24 rows of output. */
--QUERY 7



SELECT
v.*,
vba.*
FROM vendor v
INNER JOIN vendor_booth_assignments vba
ON v.vendor_id = vba.vendor_id
ORDER BY
vba.market_date,
v.vendor_name
LIMIT 24;

--END QUERY

Expand All @@ -92,9 +117,12 @@ Limit to 24 rows of output. */
/* 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. */
--QUERY 8



SELECT
vendor_id,
COUNT(*) AS booth_rental_count
FROM vendor_booth_assignments
GROUP BY vendor_id
ORDER BY vendor_id;

--END QUERY

Expand All @@ -105,9 +133,22 @@ 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_per_quantity) AS total_spent
FROM customer c
INNER 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_per_quantity) > 2000
ORDER BY
c.customer_last_name,
c.customer_first_name;

--END QUERY

Expand All @@ -124,8 +165,12 @@ 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 temp.new_vendor;
CREATE 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, 'Thomas Superfood Store', 'Fresh Focused', 'Thomas', 'Rosenthal')


--END QUERY
Expand Down
138 changes: 131 additions & 7 deletions 02_activities/assignments/DC_Cohort/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Edit the appropriate columns -- you're making two edits -- and the NULL rows wil
All the other rows will remain the same. */
--QUERY 1

SELECT
COALESCE(product_name, '') || ', ' || COALESCE(product_size, 'unit') || ' (' || product_qty_type || ')' AS FINAL_PRODUCT_LIST
FROM product;



Expand All @@ -41,6 +44,20 @@ HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK().
Filter the visits to dates before April 29, 2022. */
--QUERY 2

SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY market_date
) AS visit_number
FROM (
SELECT DISTINCT
customer_id,
market_date
FROM customer_purchases
WHERE market_date < '2022-04-29'
) AS visits;



Expand All @@ -53,6 +70,37 @@ only the customer’s most recent visit.
HINT: Do not use the previous visit dates filter. */
--QUERY 3

SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM (
SELECT DISTINCT
customer_id,
market_date
FROM customer_purchases
) AS visits;

SELECT *
FROM (
SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (
PARTITION BY customer_id
ORDER BY market_date DESC
) AS visit_number
FROM (
SELECT DISTINCT
customer_id,
market_date
FROM customer_purchases
) AS visits
) AS numbered_visits
WHERE visit_number = 1;



Expand All @@ -66,7 +114,15 @@ You can make this a running count by including an ORDER BY within the PARTITION
Filter the visits to dates before April 29, 2022. */
--QUERY 4


SELECT
customer_id,
product_id,
market_date,
COUNT(*) OVER (
PARTITION BY customer_id, product_id
) AS product_purchase_count
FROM customer_purchases
WHERE market_date < '2022-04-29';


--END QUERY
Expand All @@ -85,7 +141,14 @@ Remove any trailing or leading whitespaces. Don't just use a case statement for
Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR will help split the column. */
--QUERY 5


SELECT
product_name,
CASE
WHEN INSTR(product_name, '-') > 0
THEN TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1))
ELSE NULL
END AS description
FROM product;


--END QUERY
Expand All @@ -94,6 +157,9 @@ Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR w
/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */
--QUERY 6

SELECT *
FROM product
WHERE product_size REGEXP '[0-9]';



Expand All @@ -111,6 +177,36 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling
with a UNION binding them. */
--QUERY 7

WITH daily_sales AS (
SELECT
market_date,
SUM(quantity * cost_per_quantity) AS total_sales
FROM customer_purchases
GROUP BY market_date
),
ranked_sales AS (
SELECT
market_date,
total_sales,
RANK() OVER (ORDER BY total_sales DESC) AS sales_rank
FROM daily_sales
)

SELECT
market_date,
total_sales,
'best day' AS day_type
FROM ranked_sales
WHERE sales_rank = 1

UNION

SELECT
market_date,
total_sales,
'worst day' AS day_type
FROM ranked_sales
WHERE sales_rank = (SELECT MAX(sales_rank) FROM ranked_sales);



Expand All @@ -132,7 +228,20 @@ How many customers are there (y).
Before your final group by you should have the product of those two queries (x*y). */
--QUERY 8


SELECT
v.vendor_name,
p.product_name,
5 * COUNT(DISTINCT c.customer_id) * vi.quantity*original_price AS total_sales
FROM vendor_inventory vi
JOIN vendor v
ON vi.vendor_id = v.vendor_id
JOIN product p
ON vi.product_id = p.product_id
CROSS JOIN customer c
GROUP BY
v.vendor_name,
p.product_name,
vi.quantity*original_price;


--END QUERY
Expand All @@ -145,8 +254,13 @@ It should use all of the columns from the product table, as well as a new column
Name the timestamp column `snapshot_timestamp`. */
--QUERY 9



DROP TABLE IF EXISTS temp.product_units;
CREATE TEMP TABLE product_units AS
SELECT
product.*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_qty_type = 'unit';

--END QUERY

Expand All @@ -155,7 +269,12 @@ Name the timestamp column `snapshot_timestamp`. */
This can be any product you desire (e.g. add another record for Apple Pie). */
--QUERY 10


INSERT INTO product_units
SELECT
product.*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_name = 'Apple Pie';


--END QUERY
Expand All @@ -167,7 +286,12 @@ This can be any product you desire (e.g. add another record for Apple Pie). */
HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/
--QUERY 11


DELETE FROM product_units
WHERE product_name = 'Apple Pie'
AND snapshot_timestamp < (
SELECT MAX(snapshot_timestamp)
FROM product_units
WHERE product_name = 'Apple Pie');


--END QUERY
Expand Down
Binary file not shown.
Binary file modified 05_src/sql/farmersmarket.db
Binary file not shown.
Loading