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
177 changes: 166 additions & 11 deletions 02_activities/assignments/assignment2.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
/* ASSIGNMENT 2 */

/* SECTION 1 */
The store wants to keep customer addresses. Propose two architectures for the CUSTOMER_ADDRESS
table, one that will retain changes, and another that will overwrite. Which is type 1, which is type 2?

HINT: search type 1 vs type 2 slowly changing dimensions

#type 1
CREATE TABLE CUSTOMER_ADDRESS (
customer_id INT PRIMARY KEY,
street_address VARCHAR,
city VARCHAR,
province VARCHAR,
postal_code VARCHAR,
country VARCHAR
);

#type 2
CREATE TABLE CUSTOMER_ADDRESS_HISTORY (
customer_id INT,
street_address VARCHAR,
city VARCHAR,
province VARCHAR,
postal_code VARCHAR,
country VARCHAR,
effective_date DATE,
end_date DATE,
is_current BOOLEAN,
PRIMARY KEY (customer_id, effective_date)
);

/* SECTION 2 */

-- COALESCE
Expand All @@ -20,7 +51,27 @@ The `||` values concatenate the columns into strings.
Edit the appropriate columns -- you're making two edits -- and the NULL rows will be fixed.
All the other rows will remain the same.) */

SELECT
product_name || ', ' || product_size || ' (' || product_qty_type || ')'
FROM product;

SELECT
product_name,
product_size,
product_qty_type
FROM product
WHERE product_size IS NULL OR product_qty_type IS NULL or product_name is NULL;

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

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

--Windowed Functions
/* 1. Write a query that selects from the customer_purchases table and numbers each customer’s
Expand All @@ -32,18 +83,47 @@ each new market date for each customer, or select only the unique market dates p
(without purchase details) and number those visits.
HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK(). */


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

/* 2. Reverse the numbering of the query from a part so each customer’s most recent visit is labeled 1,
then write another query that uses this one as a subquery (or temp table) and filters the results to
only the customer’s most recent visit. */


SELECT
customer_id,
market_date
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 unique_visits
) AS ranked_visits
WHERE visit_number = 1;

/* 3. Using a COUNT() window function, include a value along with each row of the
customer_purchases table that indicates how many different times that customer has purchased that product_id. */


SELECT
customer_id,
product_id,
COUNT(*) OVER (
PARTITION BY customer_id, product_id
) AS product_purchase_count
FROM customer_purchases;

-- String manipulations
/* 1. Some product names in the product table have descriptions like "Jar" or "Organic".
Expand All @@ -58,10 +138,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. */



/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */


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;

-- UNION
/* 1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales.
Expand All @@ -73,7 +157,37 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling
3) Query the second temp table twice, once for the best day, once for the worst day,
with a UNION binding them. */

WITH sales_by_date AS (
SELECT
market_date,
SUM(quantity * cost_to_customer_per_qty) 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 best_day_rank,
RANK() OVER (ORDER BY total_sales ASC) AS worst_day_rank
FROM sales_by_date
)

SELECT
market_date,
total_sales,
'Best Day' AS label
FROM ranked_sales
WHERE best_day_rank = 1

UNION

SELECT
market_date,
total_sales,
'Worst Day' AS label
FROM ranked_sales
WHERE worst_day_rank = 1;


/* SECTION 3 */
Expand All @@ -89,27 +203,58 @@ Think a bit about the row counts: how many distinct vendors, product names are t
How many customers are there (y).
Before your final group by you should have the product of those two queries (x*y). */


SELECT
v.vendor_name,
p.product_name,
COUNT(c.customer_id) * 5 * vi.original_price AS total_revenue
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.original_price;

-- INSERT
/*1. Create a new table "product_units".
This table will contain only products where the `product_qty_type = 'unit'`.
It should use all of the columns from the product table, as well as a new column for the `CURRENT_TIMESTAMP`.
Name the timestamp column `snapshot_timestamp`. */


CREATE TABLE product_units AS
SELECT
*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_qty_type = 'unit';

/*2. Using `INSERT`, add a new row to the product_units table (with an updated timestamp).
This can be any product you desire (e.g. add another record for Apple Pie). */


INSERT INTO product_units (
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
snapshot_timestamp
)
VALUES (
11,
'pork chop',
'1 lb',
'6',
'lbs',
CURRENT_TIMESTAMP
);

-- DELETE
/* 1. Delete the older record for the whatever product you added.

HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/


DELETE FROM product_units
WHERE product_name = 'pork chop';

-- UPDATE
/* 1.We want to add the current_quantity to the product_units table.
Expand All @@ -129,5 +274,15 @@ Finally, make sure you have a WHERE statement to update the right row,
When you have all of these components, you can run the update statement. */


ALTER TABLE product_units
ADD current_quantity INT;

UPDATE product_units
SET current_quantity = COALESCE((
SELECT vi.quantity
FROM vendor_inventory vi
WHERE vi.product_id = product_units.product_id
ORDER BY vi.market_date DESC
LIMIT 1
), 0);

27 changes: 27 additions & 0 deletions 04_this_cohort/live_code/module_3/DATES.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- dates

-- now

SELECT DISTINCT
DATE('now') as [now]
,DATETIME() as [right_now]

--strftime
,strftime('%Y/%m','now') as this_year_month
,strftime('%Y-%m-%d', '2025-08-10', '+50 days') as the_future
,market_date
,strftime('%m-%d-%Y',market_date, '+50 days', '-1 year') as the_past

--dateadd
--last date of the month
,DATE(market_date,'start of month','-1 day','start of month') as start_of_prev_month
,DATE(market_date,'start of month','-1 day') as end_of_prev_month


-- datediff "equiv"
,market_date
,julianday('now') - julianday(market_date) as now_md_dd-- number of days between now and each market_date
,(julianday('now') - julianday(market_date)) / 365.25 as now_md_dd_yrs -- number of YEARS between now and market_date
,(julianday('now') - julianday(market_date)) * 24 as now_md_dd_hours -- number of HOURS bewtween now and market_date

FROM market_date_info
40 changes: 40 additions & 0 deletions 04_this_cohort/live_code/module_4/FULL_OUTER_JOIN_UNION.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--FULL OUTER JOIN WITH A UNION
-- two stores, determine which costumes they have in stock
DROP TABLE IF EXISTS temp.store1;
CREATE TEMP TABLE IF NOT EXISTS temp.store1
(
costume TEXT,
quantity INT
);

INSERT INTO temp.store1
VALUES("tiger",6),
("elephant",2),
("princess", 4);


DROP TABLE IF EXISTS temp.store2;
CREATE TEMP TABLE IF NOT EXISTS temp.store2
(
costume TEXT,
quantity INT
);

INSERT INTO temp.store2
VALUES("tiger",2),
("dancer",7),
("superhero", 5);



SELECT s1.costume, s1.quantity as store1_quantity, s2.quantity as store2_quantity, 'top query' as location
FROM store1 s1
LEFT JOIN store2 s2 on s1.costume = s2.costume

UNION ALL

SELECT s2.costume, s1.quantity, s2.quantity, 'bottom query'
FROM store2 as s2
LEFT JOIN store1 s1 on s1.costume = s2.costume
WHERE s1.costume IS NULL

24 changes: 24 additions & 0 deletions 04_this_cohort/live_code/module_4/IFNULL_NULLIF.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--IFNULL and coalesce & NULLIF

SELECT *
,IFNULL(product_size,'Unknown')

--replace with another COLUMN
,IFNULL(product_size, product_qty_type)
,coalesce(product_size, product_qty_type)
,coalesce(product_size,product_qty_type,'missing') -- if the first value is null, then the second value, if that is null, then the third value (missing)

,IFNULL(IFNULL(product_size, product_qty_type),'missing') -- same as above but with two ifnulls

FROM product;

SELECT *
,coalesce(product_size,'Unknown') -- we aren't successfully handling the blank value
--nullif
,NULLIF(product_size,'') -- find the values in product_size that "blanks" and set them to null
,coalesce(NULLIF(product_size,''),'Unknown')
,coalesce(NULLIF(TRIM(product_size),''),'Unknown') -- a trimmed blank so all white space becomes blank ' ' = ''

FROM product

WHERE NULLIF(product_size,'') IS NULL -- capturing BOTH nulls and blanks at the same time!
29 changes: 29 additions & 0 deletions 04_this_cohort/live_code/module_4/INTERSECT_EXCEPT.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- INTERSECT / EXCEPT

-- products that have been sold (e.g. are in the customer_purchases and product)

SELECT product_id
FROM customer_purchases
INTERSECT
SELECT product_id
FROM product;

-- products that have NOT been sold (e.g. are NOT in customer_purchases even though they are in product)
SELECT product_name, x.product_id
FROM
(
SELECT product_id
FROM product
EXCEPT
SELECT product_id
FROM customer_purchases
) x
JOIN product p on x.product_id = p.product_id;

-- sold products that are not in the products table ... not possible
-- NOTHING
SELECT product_id
FROM customer_purchases
EXCEPT
SELECT product_id
FROM product
Loading