diff --git a/02_activities/assignments/Cohort_8/Asignment_2_part_1.png b/02_activities/assignments/Cohort_8/Asignment_2_part_1.png new file mode 100644 index 000000000..f2aaf2050 Binary files /dev/null and b/02_activities/assignments/Cohort_8/Asignment_2_part_1.png differ diff --git a/02_activities/assignments/Cohort_8/Assignement_2_part_2.png b/02_activities/assignments/Cohort_8/Assignement_2_part_2.png new file mode 100644 index 000000000..1fe148d9b Binary files /dev/null and b/02_activities/assignments/Cohort_8/Assignement_2_part_2.png differ diff --git a/02_activities/assignments/Cohort_8/Logical_Model.png b/02_activities/assignments/Cohort_8/Logical_Model.png new file mode 100644 index 000000000..bcbc83263 Binary files /dev/null and b/02_activities/assignments/Cohort_8/Logical_Model.png differ diff --git a/02_activities/assignments/Cohort_8/assignment1.sql b/02_activities/assignments/Cohort_8/assignment1.sql index c992e3205..abb33935b 100644 --- a/02_activities/assignments/Cohort_8/assignment1.sql +++ b/02_activities/assignments/Cohort_8/assignment1.sql @@ -4,17 +4,24 @@ --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. */ +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), @@ -23,10 +30,19 @@ filtered by customer IDs between 8 and 10 (inclusive) using either: 2. one condition using BETWEEN */ -- option 1 - +SELECT + *, + quantity * cost_to_customer_per_qty AS price +FROM customer_purchases +WHERE customer_id >= 8 + AND customer_id <= 10; -- option 2 - +SELECT + *, + quantity * cost_to_customer_per_qty AS price +FROM customer_purchases +WHERE customer_id BETWEEN 8 AND 10; --CASE @@ -35,6 +51,15 @@ 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 LOWER(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. @@ -42,12 +67,37 @@ 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. */ +SELECT + product_id, + product_name, + CASE + WHEN LOWER(product_qty_type) = 'unit' THEN 'unit' + ELSE 'bulk' + END AS prod_qty_type_condensed, + CASE + WHEN LOWER(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 + v.vendor_id, + v.vendor_name, + v.vendor_type, + v.vendor_owner_first_name, + v.vendor_owner_last_name, + vba.booth_number, + vba.market_date +FROM vendor AS v +INNER JOIN vendor_booth_assignments AS vba + ON v.vendor_id = vba.vendor_id +ORDER BY v.vendor_name, vba.market_date; /* SECTION 3 */ @@ -57,6 +107,13 @@ vendor_id field they both have in common, and sorts the result by vendor_name, t at the farmer’s market by counting the vendor booth assignments per vendor_id. */ +SELECT + vendor_id, + COUNT(*) AS times_rented +FROM vendor_booth_assignments +GROUP BY vendor_id +ORDER BY times_rented DESC, 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 @@ -64,12 +121,25 @@ 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_id, + c.customer_first_name, + c.customer_last_name, + SUM(cp.quantity * cp.cost_to_customer_per_qty) AS total_spent +FROM customer AS c +JOIN customer_purchases AS 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_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: Thomass Superfood Store, a Fresh Focused store, owned by Thomas Rosenthal + + + HINT: This is two total queries -- first create the table from the original, then insert the new 10th vendor. When inserting the new vendor, you need to appropriately align the columns to be inserted (there are five columns to be inserted, I've given you the details, but not the syntax) @@ -78,6 +148,26 @@ When inserting the new vendor, you need to appropriately align the columns to be VALUES(col1,col2,col3,col4,col5) */ +DROP TABLE IF EXISTS temp.new_vendor; +CREATE TEMP TABLE new_vendor AS +SELECT * FROM vendor; + +INSERT INTO new_vendor ( + vendor_id, + vendor_name, + vendor_type, + vendor_owner_first_name, + vendor_owner_last_name +) +VALUES ( + 10, + 'Thomass Superfood Store', + 'Fresh Focused', + 'Thomas', + 'Rosenthal' +); + + -- Date @@ -87,6 +177,13 @@ HINT: you might need to search for strfrtime modifers sqlite on the web to know and year are! */ +SELECT + customer_id, + STRFTIME('%m', purchase_datetime) AS month, + STRFTIME('%Y', purchase_datetime) 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. @@ -94,3 +191,11 @@ 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_apr_2022 +FROM customer_purchases +WHERE STRFTIME('%Y', purchase_datetime) = '2022' + AND STRFTIME('%m', purchase_datetime) = '04' +GROUP BY customer_id +ORDER BY total_spent_apr_2022 DESC, customer_id; diff --git a/02_activities/assignments/Cohort_8/assignment2.sql b/02_activities/assignments/Cohort_8/assignment2.sql index c2743d3b7..46898965a 100644 --- a/02_activities/assignments/Cohort_8/assignment2.sql +++ b/02_activities/assignments/Cohort_8/assignment2.sql @@ -35,17 +35,60 @@ each new market date for each customer, or select only the unique market dates p 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, + ROW_NUMBER() OVER ( + PARTITION BY customer_id + ORDER BY market_date DESC + ) AS visit_rank +FROM customer_purchases; + + + +WITH ranked_visits AS ( + SELECT + customer_id, + market_date, + ROW_NUMBER() OVER ( + PARTITION BY customer_id + ORDER BY market_date DESC + ) AS visit_rank + FROM customer_purchases +) +SELECT * +FROM ranked_visits +WHERE visit_rank = 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, + purchase_date, + COUNT(*) OVER ( + PARTITION BY customer_id, product_id + ) AS purchase_count +FROM customer_purchases; + -- String manipulations /* 1. Some product names in the product table have descriptions like "Jar" or "Organic". @@ -60,10 +103,25 @@ 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_name, + product_size +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. @@ -75,7 +133,30 @@ 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 * price) 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_rank, + RANK() OVER (ORDER BY total_sales ASC) AS worst_rank + FROM sales_by_date +) +SELECT market_date, total_sales, 'Best Day' AS category +FROM ranked_sales +WHERE best_rank = 1 + +UNION + +SELECT market_date, total_sales, 'Worst Day' AS category +FROM ranked_sales +WHERE worst_rank = 1; /* SECTION 3 */ @@ -92,6 +173,28 @@ How many customers are there (y). Before your final group by you should have the product of those two queries (x*y). */ +WITH vendor_products AS ( + SELECT + v.vendor_id, + v.vendor_name, + p.product_id, + p.product_name, + vi.original_price + FROM vendor_inventory vi + JOIN vendor v ON vi.vendor_id = v.vendor_id + JOIN product p ON vi.product_id = p.product_id +), +customer_count AS ( + SELECT COUNT(*) AS total_customers FROM customer_purchases +) +SELECT + vp.vendor_name, + vp.product_name, + (cc.total_customers * 5 * vp.original_price) AS total_revenue +FROM vendor_products vp +CROSS JOIN customer_count cc +ORDER BY vp.vendor_name, vp.product_name; + -- INSERT /*1. Create a new table "product_units". @@ -99,12 +202,40 @@ 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 + product_id, + product_name, + product_size, + product_category_id, + product_qty_type, + 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 ( + 999, -- Example new product_id + 'Apple Pie', -- Product name + '9 inch', -- Product size + 3, -- Example category ID (adjust based on your schema) + 'unit', -- Quantity type + CURRENT_TIMESTAMP -- Updated timestamp +); + -- DELETE /* 1. Delete the older record for the whatever product you added. @@ -112,6 +243,11 @@ 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.*/ +DELETE FROM product_units +WHERE product_name = 'Apple Pie' +ORDER BY snapshot_timestamp ASC +LIMIT 1; + -- UPDATE /* 1.We want to add the current_quantity to the product_units table. @@ -120,6 +256,8 @@ First, add a new column, current_quantity to the table using the following synta ALTER TABLE product_units ADD current_quantity INT; + + Then, using UPDATE, change the current_quantity equal to the last quantity value from the vendor_inventory details. HINT: This one is pretty hard. @@ -131,5 +269,17 @@ 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. */ +UPDATE product_units pu +SET current_quantity = COALESCE(( + SELECT vi.quantity + FROM vendor_inventory vi + WHERE vi.product_id = pu.product_id + ORDER BY vi.market_date DESC + LIMIT 1 +), 0) +WHERE pu.product_id IN ( + SELECT DISTINCT product_id FROM vendor_inventory +); +