diff --git a/02_activities/assignments/Cohort_8/Assignment1.md b/02_activities/assignments/Cohort_8/Assignment1.md index 2d1ba5e1c..ee0e980ef 100644 --- a/02_activities/assignments/Cohort_8/Assignment1.md +++ b/02_activities/assignments/Cohort_8/Assignment1.md @@ -120,28 +120,93 @@ Steps to complete this part of the assignment: #### 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 customer 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), filtered by customer IDs between 8 and 10 (inclusive) using either: 1. two conditions using AND +SELECT *, +(quantity*cost_to_customer_per_qty) as price +FROM customer_purchases +WHERE customer_id >= 8 +AND customer_id <= 10; + 2. one condition using BETWEEN
-
+SELECT *, +(quantity*cost_to_customer_per_qty) as price +FROM customer_purchases + +WHERE customer_id BETWEEN '8' AND '10'; + #### CASE 1. Products can be sold by the individual unit or by bulk measures like lbs. or oz. Using the product table, write a query that outputs the `product_id` and `product_name` 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' +WHEN product_qty_type IS NULL THEN 'NULL' +ELSE 'bulk' +END as product_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' +WHEN product_qty_type IS NULL THEN 'NULL' +ELSE 'bulk' +END AS product_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 JOIN`s 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 +ORDER BY vendor_name, market_date; + +--or if we want only one vendor_id column: +SELECT + vendor.vendor_id, + vendor.vendor_name, + vendor_booth_assignments.market_date, + vendor_booth_assignments.booth_number +FROM vendor +INNER JOIN vendor_booth_assignments + ON vendor.vendor_id = vendor_booth_assignments.vendor_id +ORDER BY vendor.vendor_name, vendor_booth_assignments.market_date; + *** ## Section 3: @@ -157,12 +222,40 @@ Steps to complete this part of the assignment: #### AGGREGATE 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(booth_number) as num_booths + 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 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, +ROUND(SUM (cp.quantity*cp.cost_to_customer_per_qty),0) as total_spend + +FROM customer_purchases AS cp +INNER JOIN customer AS c +ON c.customer_id = cp.customer_id + +GROUP BY +c.customer_id, +c.customer_last_name, +c.customer_first_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 @@ -173,6 +266,26 @@ To insert the new row use VALUES, specifying the value you want for each column:
-
+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, + 'Thomass Superfood Store', + 'Fresh Focused', + 'Thomas', + 'Rosenthal' + ); + + #### Date 1. Get the customer_id, month, and year (in separate columns) of every purchase in the customer_purchases table. diff --git a/02_activities/assignments/Cohort_8/Assignment1_section1_ItzelPolin_Lopez.pdf b/02_activities/assignments/Cohort_8/Assignment1_section1_ItzelPolin_Lopez.pdf new file mode 100644 index 000000000..e733fd135 Binary files /dev/null and b/02_activities/assignments/Cohort_8/Assignment1_section1_ItzelPolin_Lopez.pdf differ diff --git a/02_activities/assignments/Cohort_8/assignment-two/Prompt 1.pdf b/02_activities/assignments/Cohort_8/assignment-two/Prompt 1.pdf new file mode 100644 index 000000000..03d899ac4 Binary files /dev/null and b/02_activities/assignments/Cohort_8/assignment-two/Prompt 1.pdf differ diff --git a/02_activities/assignments/Cohort_8/assignment-two/Prompt 2.pdf b/02_activities/assignments/Cohort_8/assignment-two/Prompt 2.pdf new file mode 100644 index 000000000..ccd899a2a Binary files /dev/null and b/02_activities/assignments/Cohort_8/assignment-two/Prompt 2.pdf differ diff --git a/02_activities/assignments/Cohort_8/assignment-two/Prompt 3.pdf b/02_activities/assignments/Cohort_8/assignment-two/Prompt 3.pdf new file mode 100644 index 000000000..2602dff1b Binary files /dev/null and b/02_activities/assignments/Cohort_8/assignment-two/Prompt 3.pdf differ diff --git a/02_activities/assignments/Cohort_8/assignment-two/assignment2.sql b/02_activities/assignments/Cohort_8/assignment-two/assignment2.sql new file mode 100644 index 000000000..65a692f47 --- /dev/null +++ b/02_activities/assignments/Cohort_8/assignment-two/assignment2.sql @@ -0,0 +1,407 @@ +/* ASSIGNMENT 2 */ +/* SECTION 2 */ + +-- COALESCE +/* 1. Our favourite manager wants a detailed long list of products, but is afraid of tables! +We tell them, no problem! We can produce a list with all of the appropriate details. + +Using the following syntax you create our super cool and not at all needy manager a list: + +SELECT +product_name || ', ' || product_size|| ' (' || product_qty_type || ')' +FROM product; + +But wait! The product table has some bad data (a few NULL values). +Find the NULLs and then using COALESCE, replace the NULL with a +blank for the first problem, and 'unit' for the second problem. + +SELECT + IFNULL(product_name, '') || ', ' || + IFNULL(product_size, '') || ' (' || + COALESCE(product_qty_type, 'unit') || ')' + AS product_label +FROM product; + +SELECT + COALESCE(product_name, '') || ', ' || + COALESCE(product_size, '') || ' (' || + COALESCE(product_qty_type, 'unit') || ')' + AS product_label +FROM product; + +HINT: keep the syntax the same, but edited the correct components with the string. +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.) */ + + + +--Windowed Functions +/* 1. Write a query that selects from the customer_purchases table and numbers each customer’s +visits to the farmer’s market (labeling each market date with a different number). +Each customer’s first visit is labeled 1, second visit is labeled 2, etc. + +You can either display all rows in the customer_purchases table, with the counter changing on +each new market date for each customer, or select only the unique market dates per customer +(without purchase details) and number those visits. +HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK(). */ + +--Option 1:Show every purchase row and number visits But ONLY if each customer has one purchase per visit. +SELECT + customer_id, + market_date, + product_id, + ROW_NUMBER() OVER ( + PARTITION BY customer_id + ORDER BY market_date + ) AS visit_number +FROM customer_purchases; +--This will label each row in order of the market visits. + +--Option 2: Show only distinct visits (one per date) dense_rank +SELECT + customer_id, + market_date, + DENSE_RANK() OVER ( + PARTITION BY customer_id + ORDER BY market_date + ) AS visit_number +FROM ( + SELECT DISTINCT customer_id, market_date + 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. */ + +--with row_number: +SELECT + customer_id, + market_date, + ROW_NUMBER() OVER ( + PARTITION BY customer_id + ORDER BY market_date DESC + ) AS visit_number_desc +FROM customer_purchases; + +--with dense_rank: +SELECT + customer_id, + market_date, + DENSE_RANK() OVER ( + PARTITION BY customer_id + ORDER BY market_date DESC + ) AS visit_number_desc +FROM ( + SELECT DISTINCT customer_id, market_date + FROM customer_purchases; + + --subquery with row number + +SELECT * +FROM ( + SELECT + customer_id, + market_date, + product_id, + quantity, + ROW_NUMBER() OVER ( + PARTITION BY customer_id + ORDER BY market_date DESC + ) AS visit_number_desc + FROM customer_purchases +) AS visits +WHERE visit_number_desc = 1; + + +--subquery with dense_rank + + +SELECT * +FROM ( + SELECT + customer_id, + market_date, + DENSE_RANK() OVER ( + PARTITION BY customer_id + ORDER BY market_date DESC + ) AS visit_number_desc + FROM ( + SELECT DISTINCT customer_id, market_date + FROM customer_purchases + ) +) AS visits +WHERE visit_number_desc = 1; --must recent visit is visit 1 because is desc + +/* 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, + quantity, + COUNT(*) OVER ( + PARTITION BY customer_id, product_id --matching pairs only + ) AS times_purchased_by_customer +FROM customer_purchases; + +-- String manipulations +/* 1. Some product names in the product table have descriptions like "Jar" or "Organic". +These are separated from the product name with a hyphen. +Create a column using SUBSTR (and a couple of other commands) that captures these, but is otherwise NULL. +Remove any trailing or leading whitespaces. Don't just use a case statement for each product! + +| product_name | description | +|----------------------------|-------------| +| Habanero Peppers - Organic | Organic | + +Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR will help split the column. */ + +--INSTR(product_name,'-') → finds the position of the hyphen + +--SUBSTR() → extracts text + +--TRIM() → removes extra whitespace + +--NULLIF() → turns empty strings into NULL + +SELECT + product_name, + NULLIF( + TRIM( + SUBSTR( + product_name, + INSTR(product_name,'-') + 1 -- start after hyphen + ) + ), + '' + ) AS description +FROM product; + +--INSTR() finds the hyphen location. +--SUBSTR(..., INSTR(...) + 1) grabs everything after it. +--TRIM() removes spaces like " Organic" → "Organic". +--NULLIF(..., '') converts empty strings to NULL, which happens when a product has no hyphen. + + +/* 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]'; --[0-9] means: match any digit + + +-- UNION +/* 1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales. + +HINT: There are a possibly a few ways to do this query, but if you're struggling, try the following: +1) Create a CTE/Temp Table to find sales values grouped dates; + +WITH sales_by_date AS ( + SELECT + market_date, + SUM(quantity * cost_to_customer_per_qty) AS total_revenue + FROM customer_purchases + GROUP BY market_date +), +2) Create another CTE/Temp table with a rank windowed function on the previous query to create +"best day" and "worst day"; + +ranked_days AS ( + SELECT + market_date, + total_revenue, + DENSE_RANK() OVER (ORDER BY total_revenue DESC) AS best_rank, + DENSE_RANK() OVER (ORDER BY total_revenue ASC) AS worst_rank + FROM sales_by_date +) + +All query at once then: + + +WITH sales_by_date AS ( + SELECT + market_date, + SUM(quantity * cost_to_customer_per_qty) AS total_revenue + FROM customer_purchases + GROUP BY market_date +), + +ranked_days AS ( + SELECT + market_date, + total_revenue, + DENSE_RANK() OVER (ORDER BY total_revenue DESC) AS best_rank, + DENSE_RANK() OVER (ORDER BY total_revenue ASC) AS worst_rank + FROM sales_by_date +) + +SELECT + market_date, + total_revenue, + 'best day' AS label +FROM ranked_days +WHERE best_rank = 1 + +UNION + +SELECT + market_date, + total_revenue, + 'worst day' AS label +FROM ranked_days +WHERE worst_rank = 1; + + +3) Query the second temp table twice, once for the best day, once for the worst day, +with a UNION binding them. */ + +SELECT + market_date, + total_revenue, + 'best day' AS label +FROM ranked_days +WHERE best_rank = 1 + +UNION + +SELECT + market_date, + total_revenue, + 'worst day' AS label +FROM ranked_days +WHERE worst_rank = 1; + + + +/* SECTION 3 */ + +-- Cross Join +/*1. Suppose every vendor in the `vendor_inventory` table had 5 of each of their products to sell to **every** +customer on record. How much money would each vendor make per product? +Show this by vendor_name and product name, rather than using the IDs. + +HINT: Be sure you select only relevant columns and rows. +Remember, CROSS JOIN will explode your table rows, so CROSS JOIN should likely be a subquery. +Think a bit about the row counts: how many distinct vendors, product names are there (x)? +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_name, + p.product_name, + vi.original_price + FROM vendor_inventory AS vi + JOIN vendor AS v USING (vendor_id) + JOIN product AS p USING (product_id) +), + +vp_customers AS ( + SELECT + vp.vendor_name, + vp.product_name, + (5 * vp.original_price) AS revenue_per_customer + FROM vendor_products AS vp + CROSS JOIN customer +) + +SELECT + vendor_name, + product_name, + SUM(revenue_per_customer) AS total_revenue +FROM vp_customers +GROUP BY vendor_name, product_name +ORDER BY vendor_name, product_name; + +-- 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`. */ + + + +/*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). */ +CREATE TABLE product_units AS +SELECT + *, + CURRENT_TIMESTAMP AS snapshot_timestamp +FROM product +WHERE product_qty_type = 'unit'; + + +-- 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_id = 5 + AND snapshot_timestamp = ( + SELECT MIN(snapshot_timestamp) + FROM product_units + WHERE product_id = 5 + ); + + +-- UPDATE +/* 1.We want to add the current_quantity to the product_units table. +First, add a new column, current_quantity to the table using the following syntax. + +ALTER TABLE product_units +ADD current_quantity INT; + + +--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. +First, determine how to get the "last" quantity per product. + +Second, coalesce null values to 0 (if you don't have null values, figure out how to rearrange your query so you do.) +Third, SET current_quantity = (...your select statement...), remembering that WHERE can only accommodate one column. +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. */ + +/* STEP 2: Update current_quantity with the LAST quantity from vendor_inventory + - "Last" = most recent (max) market_date + - If there is no matching row or the quantity is NULL → use 0 +*/ + +ALTER TABLE product_units +ADD current_quantity INT; + + + +UPDATE product_units +SET current_quantity = COALESCE( + ( + SELECT vi.quantity + FROM vendor_inventory AS vi + WHERE vi.product_id = product_units.product_id + ORDER BY vi.market_date DESC -- most recent first + LIMIT 1 + ), + 0 -- if the subquery returns NULL (no row), set to 0 +); + + +/* STEP 3: Check the results */ + +SELECT + product_id, + product_name, + current_quantity +FROM product_units +ORDER BY product_id; + + diff --git a/02_activities/assignments/Cohort_8/assignment1.sql b/02_activities/assignments/Cohort_8/assignment1.sql index c992e3205..139bbd1d7 100644 --- a/02_activities/assignments/Cohort_8/assignment1.sql +++ b/02_activities/assignments/Cohort_8/assignment1.sql @@ -4,18 +4,23 @@ --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); -- only product ID 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 customer IDs between 8 and 10 (inclusive) using either: @@ -23,10 +28,18 @@ 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,11 +48,27 @@ 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' +WHEN product_qty_type IS NULL THEN 'NULL' +ELSE 'bulk' +END as product_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' +WHEN product_qty_type IS NULL THEN 'NULL' +ELSE 'bulk' +END AS product_qty_type_condensed, +CASE +WHEN product_name LIKE '%pepper%' THEN 1 +ELSE 0 +END AS pepper_flag +FROM product; @@ -47,16 +76,33 @@ contains the word “pepper” (regardless of capitalization), and otherwise out /* 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 +ORDER BY vendor_name, market_date; + +--or if we want only one vendor_id column: +SELECT + vendor.vendor_id, + vendor.vendor_name, + vendor_booth_assignments.market_date, + vendor_booth_assignments.booth_number +FROM vendor +INNER JOIN vendor_booth_assignments + ON vendor.vendor_id = vendor_booth_assignments.vendor_id +ORDER BY vendor.vendor_name, vendor_booth_assignments.market_date; /* SECTION 3 */ -- AGGREGATE /* 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(booth_number) as num_booths + 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 @@ -64,7 +110,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_first_name, +c.customer_last_name, +ROUND(SUM (cp.quantity*cp.cost_to_customer_per_qty),0) as total_spend +FROM customer_purchases AS cp +INNER JOIN customer AS c +ON c.customer_id = cp.customer_id + +GROUP BY +c.customer_id, +c.customer_last_name, +c.customer_first_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: @@ -78,7 +142,24 @@ 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 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, + 'Thomass Superfood Store', + 'Fresh Focused', + 'Thomas', + 'Rosenthal' + ); -- Date /*1. Get the customer_id, month, and year (in separate columns) of every purchase in the customer_purchases table. diff --git a/02_activities/assignments/Cohort_8/assignment2.sql b/02_activities/assignments/Cohort_8/assignment2.sql deleted file mode 100644 index 5ad40748a..000000000 --- a/02_activities/assignments/Cohort_8/assignment2.sql +++ /dev/null @@ -1,133 +0,0 @@ -/* ASSIGNMENT 2 */ -/* SECTION 2 */ - --- COALESCE -/* 1. Our favourite manager wants a detailed long list of products, but is afraid of tables! -We tell them, no problem! We can produce a list with all of the appropriate details. - -Using the following syntax you create our super cool and not at all needy manager a list: - -SELECT -product_name || ', ' || product_size|| ' (' || product_qty_type || ')' -FROM product - -But wait! The product table has some bad data (a few NULL values). -Find the NULLs and then using COALESCE, replace the NULL with a -blank for the first problem, and 'unit' for the second problem. - -HINT: keep the syntax the same, but edited the correct components with the string. -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.) */ - - - ---Windowed Functions -/* 1. Write a query that selects from the customer_purchases table and numbers each customer’s -visits to the farmer’s market (labeling each market date with a different number). -Each customer’s first visit is labeled 1, second visit is labeled 2, etc. - -You can either display all rows in the customer_purchases table, with the counter changing on -each new market date for each customer, or select only the unique market dates per customer -(without purchase details) and number those visits. -HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK(). */ - - - -/* 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. */ - - - -/* 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. */ - - - --- String manipulations -/* 1. Some product names in the product table have descriptions like "Jar" or "Organic". -These are separated from the product name with a hyphen. -Create a column using SUBSTR (and a couple of other commands) that captures these, but is otherwise NULL. -Remove any trailing or leading whitespaces. Don't just use a case statement for each product! - -| product_name | description | -|----------------------------|-------------| -| Habanero Peppers - Organic | Organic | - -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. */ - - - --- UNION -/* 1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales. - -HINT: There are a possibly a few ways to do this query, but if you're struggling, try the following: -1) Create a CTE/Temp Table to find sales values grouped dates; -2) Create another CTE/Temp table with a rank windowed function on the previous query to create -"best day" and "worst day"; -3) Query the second temp table twice, once for the best day, once for the worst day, -with a UNION binding them. */ - - - - -/* SECTION 3 */ - --- Cross Join -/*1. Suppose every vendor in the `vendor_inventory` table had 5 of each of their products to sell to **every** -customer on record. How much money would each vendor make per product? -Show this by vendor_name and product name, rather than using the IDs. - -HINT: Be sure you select only relevant columns and rows. -Remember, CROSS JOIN will explode your table rows, so CROSS JOIN should likely be a subquery. -Think a bit about the row counts: how many distinct vendors, product names are there (x)? -How many customers are there (y). -Before your final group by you should have the product of those two queries (x*y). */ - - - --- 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`. */ - - - -/*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). */ - - - --- 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.*/ - - - --- UPDATE -/* 1.We want to add the current_quantity to the product_units table. -First, add a new column, current_quantity to the table using the following syntax. - -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. -First, determine how to get the "last" quantity per product. -Second, coalesce null values to 0 (if you don't have null values, figure out how to rearrange your query so you do.) -Third, SET current_quantity = (...your select statement...), remembering that WHERE can only accommodate one column. -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. */ - - - -