Skip to content
1 change: 1 addition & 0 deletions 02_activities/assignments/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ product_name,
END AS description
FROM product;


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

SELECT
Expand Down
21 changes: 21 additions & 0 deletions 04_this_cohort/live_code/module_2/CASE.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--CASE

-- add some logic to determine which vendors come on which days
SELECT * ,
CASE WHEN vendor_type = 'Fresh Focused' THEN 'Wednesday'
WHEN vendor_type = 'Prepared Foods' THEN 'Thursday'
ELSE 'Saturday'
END as day_of_specialty
-- pie day, otherwise nothing
,CASE WHEN vendor_name = "Annie's Pies" --- double quotes will work just this once!
THEN 'annie is the best'
END as annie_is_the_king
,CASE WHEN vendor_name LIKE '%pie%'
THEN 'Wednesday'
ELSE 'Friday' -- with the else, we get values for FALSE statements
END as pie_day
-- nonsense, but not a string, instead a different COLUMN
,CASE WHEN vendor_type = 'Fresh Focused' THEN vendor_owner_first_name
WHEN vendor_type = 'Prepared Foods' THEN vendor_owner_last_name
END as first_or_last
FROM vendor
32 changes: 32 additions & 0 deletions 04_this_cohort/live_code/module_2/DISTINCT.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- DISTINCT

--without distinct 4221 rows of various cust_ids
SELECT customer_id FROM customer_purchases;

-- with distinct 26 rows of various cust_ids
SELECT DISTINCT customer_id FROM customer_purchases;

--150 days the market was open
SELECT market_day
FROM market_date_info;

-- market is only open wed and sat
SELECT DISTINCT market_day
FROM market_date_info;

/* which vendor has sold products to a customer */ -- 3 rows
SELECT DISTINCT vendor_id
FROM customer_purchases;

/* which vendor has sold products to a customer ... and which product was it? */ -- 8 rows
SELECT DISTINCT vendor_id, product_id
FROM customer_purchases;

/* which vendor has sold products to a customer
... and which product was it?
... AND to whom was it sold*/ -- 200 rows
SELECT DISTINCT vendor_id, customer_id, product_id
FROM customer_purchases
ORDER BY customer_id ASC, product_id DESC


29 changes: 29 additions & 0 deletions 04_this_cohort/live_code/module_2/INNER_JOIN.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- INNER JOIN
-- INNER JOIN without an alias

--get product names alongside customer_purchases ... only products that a customer has purchased
SELECT
product_name, -- come from product table
vendor_id, -- coming from the customer_purchases table ... below
market_date,
customer_id,
customer_purchases.product_id


FROM product
INNER JOIN customer_purchases
ON customer_purchases.product_id = product.product_id;

-- which vendor has sold products to a customer AND which product was it AND to whom was it sold
SELECT DISTINCT vendor_id,
c.customer_id,
customer_first_name,
customer_last_name, -- go and get this name
product_id
FROM customer_purchases as cp
INNER JOIN customer as c
ON c.customer_id = cp.customer_id




25 changes: 25 additions & 0 deletions 04_this_cohort/live_code/module_2/LEFT_JOIN.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- LEFT JOIN

-- there are products that have been bought, but are there products that have not been bought?

SELECT DISTINCT
p.product_id
,cp.product_id as [cp.product_id]
,product_name

FROM product as p
LEFT JOIN customer_purchases as cp
ON p.product_id = cp.product_id

WHERE cp.product_id IS NULL; -- only show product ids that have not been sold

-- directions matter
-- this shows ONLY producst that have been sold...because there are no products id in cp that ARENT in product
SELECT DISTINCT
p.product_id
,cp.product_id as [cp.product_id]
,product_name

FROM customer_purchases as cp
LEFT JOIN product as p
ON p.product_id = cp.product_id
23 changes: 23 additions & 0 deletions 04_this_cohort/live_code/module_2/SELECT.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
-- SELECT

-- select everything from the customer TABLE
SELECT *
FROM customer;

-- use sql as a calculator
SELECT 1+1, 10*5, pi();

--add a static value
SELECT 2025 as this_year, 'August' as this_month, customer_id
FROM customer;

-- add an order by and limit
SELECT *
FROM customer
ORDER BY customer_first_name
LIMIT 10;

-- select multiple columns
SELECT customer_id, customer_first_name
FROM customer;

33 changes: 33 additions & 0 deletions 04_this_cohort/live_code/module_2/WHERE.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- WHERE

SELECT * FROM customer
WHERE customer_id = 1
OR customer_id = 2; -- 1 or 2
--AND customer_id = 2 -- returns nothing

--IN
SELECT * FROM customer
WHERE customer_id IN (3,4,5) -- only customers 3,4,5
OR customer_postal_code IN ('M4H','M1L'); -- customers in these postal codes

--LIKE
-- all the peppers
SELECT * FROM product
WHERE product_name LIKE '%pepper%';

--customer with a last name starting with a
SELECT * FROM customer
WHERE customer_last_name LIKE 'a%';

--NULLS and Blanks
SELECT * FROM product
WHERE product_size IS NULL
OR product_size = ''; -- two single quotes, "blanks", different from nulls

-- between another option
SELECT *
FROM customer
WHERE customer_id BETWEEN 1 AND 20



42 changes: 42 additions & 0 deletions 04_this_cohort/live_code/module_2/multiple_table_joins.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
-- multiple_table_joins

/* which vendor has sold products to a customer
... and which product was it?
... AND to whom was it sold*/
SELECT DISTINCT
--customer_id,
customer_first_name,
customer_last_name,
--vendor_id,
vendor_name,
--product_id
product_name

FROM customer_purchases as cp
INNER JOIN customer as c
ON c.customer_id = cp.customer_id
INNER JOIN vendor as v
ON v.vendor_id = cp.vendor_id
INNER JOIN product as p
ON p.product_id = cp.product_id;


/* what if we add the dates they were purchased ? */
SELECT DISTINCT
market_date,
--customer_id,
customer_first_name,
customer_last_name,
--vendor_id,
vendor_name,
--product_id
product_name

FROM customer_purchases as cp
INNER JOIN customer as c
ON c.customer_id = cp.customer_id
INNER JOIN vendor as v
ON v.vendor_id = cp.vendor_id
INNER JOIN product as p
ON p.product_id = cp.product_id

27 changes: 27 additions & 0 deletions 04_this_cohort/live_code/module_3/COUNT.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- count
-- count the number of products

SELECT COUNT(product_id) as num_of_prods
FROM product;

--how many products per product_qty_type
SELECT product_qty_type,
COUNT(product_id) as num_of_prods
FROM product
GROUP BY product_qty_type;

--how many products per product_qty_type and per their product_size
SELECT
product_size,
product_qty_type,
COUNT(product_id) as num_of_prods

FROM product

GROUP BY product_size,product_qty_type;

--count DISTINCT
--how many unique products were bought

SELECT count(DISTINCT product_id) as bought_products
FROM customer_purchases
38 changes: 38 additions & 0 deletions 04_this_cohort/live_code/module_3/CTE.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--CTE

--calculate sales per vendor per day

WITH vendor_daily_sales AS (
SELECT
md.market_date,
market_day,
market_week,
market_year,
vendor_name,
SUM(quantity*cost_to_customer_per_qty) as sales


FROM customer_purchases cp
INNER JOIN market_date_info md -- get the market_day, market_week, market_year
ON cp.market_date = md.market_date
INNER JOIN vendor v
ON v.vendor_id = cp.vendor_id

GROUP BY md.market_date, v.vendor_id

) , -- if we want another CTE, add a comma, but not another with

new_customer AS
(
SELECT * FROM customer
),

-- re-aggregate the daily sales for each WEEK instead
SELECT
market_year,
market_week,
vendor_name,
SUM(sales) as weekly_sales

FROM vendor_daily_sales
GROUP by market_year, market_week, vendor_name
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
25 changes: 25 additions & 0 deletions 04_this_cohort/live_code/module_3/HAVING.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--HAVING

-- how much did a customer spend on each day
SELECT --fifth
market_date,
customer_id,
SUM(quantity*cost_to_customer_per_qty) as total_cost

FROM customer_purchases -- first
WHERE customer_id BETWEEN 1 AND 5 -- filtering the non-aggregated values -- second


GROUP BY market_date, customer_id -- third
HAVING total_cost > 50; -- filtering the aggregated values -- fourth

-- how many products were bought?

SELECT
count(product_id) as number_of_products,
product_id

FROM customer_purchases
WHERE product_id <= 8
GROUP BY product_id
HAVING count(product_id) BETWEEN 300 AND 500
47 changes: 47 additions & 0 deletions 04_this_cohort/live_code/module_3/MIN_MAX.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- MIN & max

-- what is the most expensive product

SELECT
product_name,
max(original_price) as most_expensive -- doesn't handle ties well

FROM product p
INNER JOIN vendor_inventory vi
ON p.product_id = vi.product_id;

--prove it
SELECT DISTINCT
product_name,
original_price

FROM product p
INNER JOIN vendor_inventory vi
ON p.product_id = vi.product_id
ORDER BY original_price DESC;

--minimum price per each product_qty_type
SELECT
product_name,
product_qty_type,
min(original_price) as least_expensive

FROM product p
INNER JOIN vendor_inventory vi
ON p.product_id = vi.product_id
GROUP BY product_qty_type

order by product_qty_type ASC, original_price ASC;

--prove it
SELECT DISTINCT
product_name,
product_qty_type,
original_price

FROM product p
INNER JOIN vendor_inventory vi
ON p.product_id = vi.product_id
ORDER BY product_qty_type, original_price


Loading