diff --git a/02_activities/assignments/Assignment_1_Section_1.pdf b/02_activities/assignments/Assignment_1_Section_1.pdf
new file mode 100644
index 000000000..a1eb43748
Binary files /dev/null and b/02_activities/assignments/Assignment_1_Section_1.pdf differ
diff --git a/02_activities/assignments/assignment1.sql b/02_activities/assignments/assignment1.sql
index 2e89fa7af..1d012bfa2 100644
--- a/02_activities/assignments/assignment1.sql
+++ b/02_activities/assignments/assignment1.sql
@@ -4,34 +4,52 @@
--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),
filtered by vendor IDs between 8 and 10 (inclusive) using either:
1. two conditions using AND
2. one condition using BETWEEN
*/
--- option 1
+-- option 1 X
+--ALTER TABLE customer_purchases ADD price AS (quantity*cost_to_customer_per_qty);
+SELECT *
+FROM customer_purchases
+WHERE vendor_id >=8
+AND vendor_id <= 10;
+--GROUP BY customer_id, product_id, vendor_id
-- option 2
+SELECT
+customer_id,
+product_id,
+vendor_id,
+SUM(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.
@@ -39,20 +57,45 @@ 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.” */
+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 *
+FROM vendor INNER JOIN vendor_booth_assignments
+ON vendor.vendor_id = vendor_booth_assignments.vendor_id
+GROUP BY vendor_name, market_date;
/* SECTION 3 */
@@ -60,7 +103,9 @@ 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 vendor_id, COUNT (vendor_id) as num_of_rented
+FROM vendor_booth_assignments
+GROUP BY vendor_id;
/* 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 +113,19 @@ 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,
+ c.customer_id,
+ SUM (cp.quantity*cp.cost_to_customer_per_qty) AS total_spent
+FROM customer c
+LEFT JOIN customer_purchases cp
+ ON c.customer_id = cp.customer_id
+
+GROUP BY c.customer_first_name, c.customer_last_name, c.customer_id
+HAVING SUM (cp.quantity*cp.cost_to_customer_per_qty) > 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:
@@ -82,19 +139,41 @@ When inserting the new vendor, you need to appropriately align the columns to be
VALUES(col1,col2,col3,col4,col5)
*/
+CREATE TABLE temp.new_vendor AS
+
+SELECT * FROM vendor;
+INSERT INTO temp.new_vendor
+VALUES(10, 'THomass Superfood Store', 'Fresh Focused', 'Thomas','Rosenthal')
+
+SELECT * FROM temp.new_vendor;
-- Date
/*1. Get the customer_id, month, and year (in separate columns) of every purchase in the customer_purchases table.
HINT: you might need to search for strfrtime modifers sqlite on the web to know what the modifers for month
-and year are! */
-
+and year are! */ X
+SELECT
+ customer_id,
+ strftime('%m', market_date) AS month,
+ strftime('%Y', market_date) AS year
+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.
HINTS: you will need to AGGREGATE, GROUP BY, and filter...
but remember, STRFTIME returns a STRING for your WHERE statement!! */
+SELECT
+ customer_id,
+ SUM (quantity*cost_to_customer_per_qty) AS total_spent
+
+
+FROM customer_purchases
+WHERE strftime('%m', market_date) = '04'
+AND strftime('%Y', market_date) = '2022'
+
+GROUP BY customer_id
diff --git a/02_activities/assignments/assignment2.pdf b/02_activities/assignments/assignment2.pdf
new file mode 100644
index 000000000..2ccb84cdd
Binary files /dev/null and b/02_activities/assignments/assignment2.pdf differ
diff --git a/02_activities/assignments/assignment2.sql b/02_activities/assignments/assignment2.sql
index 5ad40748a..6abf560b6 100644
--- a/02_activities/assignments/assignment2.sql
+++ b/02_activities/assignments/assignment2.sql
@@ -20,7 +20,11 @@ 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 || ', ') AS product_name,
+ COALESCE(product_size, '') AS product_size,
+ ' (' || COALESCE(product_qty_type, 'unit') || ')' AS product_qty
+FROM product;
--Windowed Functions
/* 1. Write a query that selects from the customer_purchases table and numbers each customer’s
@@ -32,18 +36,38 @@ 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
+ORDER BY customer_id, market_date
/* 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 customer_purchases
+) 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,
+ market_date,
+ 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".
@@ -57,11 +81,26 @@ 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. */
-
+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
/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */
-
+SELECT
+ product_size,
+ CASE
+ WHEN INSTR(product_size, '-') > 0
+ THEN TRIM(substr(product_size, INSTR(product_size, '-') +1))
+ ELSE NULL
+ END AS description
+FROM product
+WHERE product_size REGEXP '[0-9]'
-- UNION
/* 1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales.
@@ -73,8 +112,36 @@ 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. */
+CREATE TABLE temp.sales_values_by_date AS
+SELECT
+ market_date,
+ SUM(original_price) as total_sales
+FROM vendor_inventory
+GROUP BY market_date;
+CREATE TABLE temp.best_worst_sales_values_by_date AS
+SELECT
+ market_date,
+ total_sales,
+rank() OVER (ORDER BY total_sales DESC) as [best],
+rank() OVER (ORDER BY total_sales ASC) as [worst]
+FROM sales_values_by_date;
+
+SELECT
+ market_date,
+ total_sales,
+ 'Best Day' AS sales_type
+FROM temp.best_worst_sales_values_by_date
+WHERE best = 1
+UNION
+
+SELECT
+ market_date,
+ total_sales,
+ 'Worst Day' AS sales_type
+FROM temp.best_worst_sales_values_by_date
+WHERE worst = 1
/* SECTION 3 */
@@ -89,7 +156,15 @@ 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 vendor_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".
@@ -97,19 +172,28 @@ 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_unit 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_unit (product_id, product_name, product_size, product_qty_type,snapshot_timestamp)
+VALUES (30, 'peach_pie', '500g','unit', 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_unit
+WHERE product_id = 7
-- UPDATE
/* 1.We want to add the current_quantity to the product_units table.
@@ -128,6 +212,19 @@ Finally, make sure you have a WHERE statement to update the right row,
you'll need to use product_units.product_id to refer to the correct row within the product_units table.
When you have all of these components, you can run the update statement. */
+ALTER TABLE temp.product_unit
+ADD current_quantity INT;
+SELECT *
+FROM temp.product_unit;
+
+UPDATE product_unit
+SET current_quantity = (
+ SELECT vi.quantity
+ FROM vendor_inventory vi
+ WHERE vi.product_id = product_unit.product_id
+ ORDER BY vi.market_date DESC
+ LIMIT 1
+);
diff --git a/05_src/sql/assignment1.db b/05_src/sql/assignment1.db
new file mode 100644
index 000000000..6ee8eeb2f
Binary files /dev/null and b/05_src/sql/assignment1.db differ
diff --git a/05_src/sql/assignment1.sqbpro b/05_src/sql/assignment1.sqbpro
new file mode 100644
index 000000000..e24598a8f
--- /dev/null
+++ b/05_src/sql/assignment1.sqbpro
@@ -0,0 +1,24 @@
+-- Reference to file "/Users/Hiro/sql/02_activities/assignments/assignment1.sql" (not supported by this version) --SELECT
+ c.customer_first_name,
+ c.customer_last_name,
+ c.customer_id,
+ SUM(cp.quantity * cp.cost_to_customer_per_qty) AS total_spent
+FROM customer c
+LEFT JOIN customer_purchases cp
+ ON c.customer_id = cp.customer_id
+GROUP BY c.customer_first_name, c.customer_last_name, c.customer_id
+HAVING SUM(cp.quantity * cp.cost_to_customer_per_qty) > 2000
+ORDER BY c.customer_last_name, c.customer_first_name;SELECT
+ c.customer_first_name,
+ c.customer_last_name,
+ c.customer_id,
+ SUM(cp.quantity * cp.cost_to_customer_per_qty) AS total_spent
+FROM customer c
+LEFT JOIN customer_purchases cp
+ ON c.customer_id = cp.customer_id
+GROUP BY c.customer_first_name, c.customer_last_name, c.customer_id
+HAVING SUM(cp.quantity * cp.cost_to_customer_per_qty) > 2000
+ORDER BY c.customer_last_name, c.customer_first_name;
+
+
+
diff --git a/05_src/sql/farmersmarket.db b/05_src/sql/farmersmarket.db
index 4720f2483..d8eb765c8 100644
Binary files a/05_src/sql/farmersmarket.db and b/05_src/sql/farmersmarket.db differ
diff --git a/05_src/sql/farmersmarket.sqbpro b/05_src/sql/farmersmarket.sqbpro
new file mode 100644
index 000000000..1ed7639a1
--- /dev/null
+++ b/05_src/sql/farmersmarket.sqbpro
@@ -0,0 +1,138 @@
+-- SELECT
+
+--selecting everything from customer
+SELECT *
+FROM customer;
+
+-- add a static value
+SELECT 2025 as this_year, 'April' as this_month, customer_id
+FROM customer;
+
+-- add an order by + limit
+SELECT *
+FROM customer
+ORDER BY customer_last_name
+LIMIT 10;
+
+--WHERE
+
+SELECT * FROM customer
+WHERE customer_id = 1
+AND customer_id = 2;
+
+-- IN
+SELECT * From customer_purchases
+WHERE customer_id IN (3,4,5) -- only custmoer 3, 4, 5
+AND vendor_id IN (3,4,5);
+
+-- LLIKE
+SELECT * FROM product
+WHERE product_name LIKE '%pepper%';
+
+-- customers with a last name starting with a
+SELECT * FROM customer
+WHERE customer_last_name LIKE 'a%';
+
+SELECT * FROM product
+WHERE product_size IS NULL
+OR product_size = '';
+
+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 LIKE '%pie%'
+ THEN 'Wednesday'
+ END as only_pie_day
+,CASE WHEN vendor_name = "Annie's Pies" -- double quote work, but not for all editors!
+ THEN 'annie is best'
+ END
+FROM vendor--DISTINCT
+
+-- Without distinct 4421 rows oof various customer_id
+SELECT customer_id FROM customer_purchases;
+
+SELECT DISTINCT customer_id FROM customer_purchases
+
+-- without distinctio, only we/sad 150 tiems OVER
+SELECT *
+FROM market_date_info;
+
+-- which vendor has sold product a customer
+SELECT DISTINCT vendor_id
+FROM customer_purchases; -- 3 rows, vendor id 7, 8 4
+
+-- which vendor has sold products to a customer
+SELECT DISTINCT vendor_id, product_id
+FROM customer_purchases;
+
+-- which vencor has sold products to a customer and which product was it and to whom it was sold?
+SELECT DISTINCT vendor_id, customer_id, product_id
+FROM customer_purchases
+
+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 LIKE '%pie%'
+ THEN 'Wednesday'
+ END as only_pie_day
+,CASE WHEN vendor_name = "Annie's Pies" -- double quote work, but not for all editors!
+ THEN 'annie is best'
+ END as annie_is_king
+FROM vendor
+
+-- INNER JOIN
+
+-- get prodcut names along customer_purchases
+SELECT
+product_name,
+market_date,
+customer_id,
+product_id,
+customer_purchases.product_id
+
+FROM customer_purchases
+INNER JOIN product
+ ON customer_purchases.product_id = product.product_id/* get product names alongside customer_purchases...only products that a customer has purchased will be present */
+SELECT
+product_name, -- coming from product
+vendor_id, -- coming from cp...below
+market_date,
+customer_id,
+customer_purchases.product_id
+
+FROM customer_purchases
+INNER JOIN product
+ 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,
+product_id,
+--customer_id -- this is dissatisfying...let's have a name!
+customer_first_name,
+customer_last_name
+
+FROM customer_purchases as cp
+INNER JOIN customer as c
+ ON cp.customer_id = c.customer_id-- LEFT JOIN
+
+-- there are produts being bought, but not have not?
+SELECT
+p.product_id, cp.product_id,
+cp.*--all colums in cp TABLE
+
+FROM product p
+LEFT JOIN customer_purchases cp
+ ON p.product_id = cp.product_id
+
+SELECT DISTINCT
+p.product_id, product_name,