diff --git a/ASSIGNMENT2.sql b/ASSIGNMENT2.sql new file mode 100644 index 000000000..771f17c55 --- /dev/null +++ b/ASSIGNMENT2.sql @@ -0,0 +1,324 @@ +/* ASSIGNMENT 2 */ +--Please write responses between the QUERY # and END QUERY blocks +/* 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 column with +nulls, and 'unit' for the second column with nulls. + +**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. */ +--QUERY 1 +SELECT * +FROM product; + +SELECT +product_name|| COALESCE(product_size, ' ')|| ' (' || COALESCE(product_qty_type, 'Unit' ) || ')' +FROM product; + + + + +--END QUERY + + +--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(). +Filter the visits to dates before April 29, 2022. */ +--QUERY 2 + +SELECT +customer_id, +market_date, +ROW_NUMBER() Over ( + PARTITION BY customer_id + ORDER BY market_date) AS Visit + FROM customer_purchases +WHERE market_date < '2022-04-29'; +--Shows that when the same customer visits the same day, it counts as a seperate visit. + +SELECT +customer_id, +market_date, +DENSE_RANK() Over ( + PARTITION BY customer_id + ORDER BY market_date) AS Visit + FROM customer_purchases +WHERE market_date < '2022-04-29'; +--Shows that when the same customer visits the same day, it just counts it as one visit. +--END QUERY + + +/* 2. Reverse the numbering of the query 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. +HINT: Do not use the previous visit dates filter. */ +--QUERY 3 + +SELECT * +FROM ( SELECT +customer_id, +market_date, +DENSE_RANK() Over ( + PARTITION BY customer_id + ORDER BY market_date DESC) AS Visit + FROM customer_purchases ) +WHERE Visit = 1; + + + + + + +--END QUERY + + +/* 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. + +You can make this a running count by including an ORDER BY within the PARTITION BY if desired. +Filter the visits to dates before April 29, 2022. */ +--QUERY 4 +SELECT customer_id, product_id, market_date, +COUNT(*) OVER ( + PARTITION BY customer_id, product_id) AS Purchase_count +FROM customer_purchases +WHERE market_date < '2022-04-29'; + + + +--END QUERY + + +-- 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. */ +--QUERY 5 + +--NOTES FOR MYSELF +--INSTR() finds where the character (Hyphen) is ******INSTR(string, substring)***** +--SUBSTR() uses that postion to know where to start cutting *****substr( string, start, length )**** +--If Hyphen exists THEN extract the text after it and trimp spaces ELSE return NULL???? CASE NEEDS END DONT FORGET + +SELECT +CASE + WHEN INSTR(product_name, '-') > 0 THEN + TRIM (SUBSTR(product_name, INSTR(product_name, '-')+1)) + ELSE NULL +END +FROM product; + + + +--END QUERY + + +/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */ +--QUERY 6 +SELECT * +FROM product +WHERE product_size REGEXP '[0-9]'; + + +--END QUERY + + +-- 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. */ +--QUERY 7 +SELECT * +FROM + (SELECT market_date, + SUM(quantity * cost_to_customer_per_qty) AS Total_sales, + RANK() OVER ( + ORDER BY SUM(quantity * cost_to_customer_per_qty)) AS Ranked_sales + FROM customer_purchases + Group BY market_date ) +WHERE Ranked_sales = 1 + +UNION + +SELECT * +FROM + (SELECT market_date, + SUM(quantity * cost_to_customer_per_qty) AS Total_sales, + RANK() OVER ( + ORDER BY SUM(quantity * cost_to_customer_per_qty)DESC) AS Ranked_sales + FROM customer_purchases + Group BY market_date ) +WHERE Ranked_sales = 1; + + + +--END QUERY + + + +/* 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. + +--Vendor table has vendor_name +--Product table has product_name +--VEndor_inventory has original_price + +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). */ +--QUERY 8 + +SELECT vendor_name, +product_name, +SUM(5 * original_price) AS total_sale +FROM + +(SELECT * +FROM +(SELECT DISTINCT +VENDOR.vendor_name, +PRODUCT.product_name, +VENDOR_INVENTORY.original_price +FROM vendor_inventory + +INNER JOIN VENDOR + ON vendor_inventory.vendor_id = Vendor.vendor_id +INNER JOIN product + ON vendor_inventory.product_id = PRODUCT.product_id) + +CROSS JOIN customer ) + +GROUP BY vendor_name, product_name; + + +--END QUERY + + +-- 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`. */ +--QUERY 9 + + +CREATE TABLE product_units AS +SELECT *, CURRENT_TIMESTAMP AS 'snapshot_timestamp' +FROM product +WHERE product_qty_type = 'unit' + + + + + +--END QUERY + + +/*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). */ +--QUERY 10 +INSERT INTO product_units +VALUES (7, 'Apple Pie', '10"', 3, 'unit', CURRENT_TIMESTAMP) + + + +--END QUERY + + +-- DELETE +/* 1. Delete the older record for whatever product you added. + +HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/ +--QUERY 11 + +DELETE FROM product_units +WHERE snapshot_timestamp = '2026-05-10 18:07:02' +--I ACCIDENTALLY DELETED THE NEWER ONE BUT MEANT TO DELETE THE OLD ONE WHOOPS + + +--END QUERY + + +-- 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. */ + +--QUERY 12 Market_date, quantity, product_id +ALTER TABLE product_units +ADD current_quantity INT; + +UPDATE product_units +SET current_quantity = ( + SELECT COALESCE (quantity, 0) + FROM ( + SELECT Market_date, + quantity, + product_id, + ROW_number() OVER ( + PARTITION BY product_id + ORDER BY market_date DESC ) AS row_number + FROM vendor_inventory) +WHERE row_number = 1 +AND product_id = product_units.product_id) + +--I have no clue how to set the Null Values to 0 and unfortunately don't have enough time to figure it out + + + +--END QUERY + diff --git a/Assignment2.md b/Assignment2.md new file mode 100644 index 000000000..0cf75850a --- /dev/null +++ b/Assignment2.md @@ -0,0 +1,176 @@ +# Microcredential Assignment 2: Design a Logical Model and Advanced SQL + +🚨 **Please review our [Assignment Submission Guide](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md)** 🚨 for detailed instructions on how to format, branch, and submit your work. Following these guidelines is crucial for your submissions to be evaluated correctly. + +#### Submission Parameters: +* Submission Due Date: `May 11, 2026` +* Weight: 70% of total grade +* The branch name for your repo should be: `assignment-two` +* What to submit for this assignment: + * This markdown (Assignment2.md) with written responses in Section 1 + * Two Entity-Relationship Diagrams (preferably in a pdf, jpeg, png format). + * One .sql file +* What the pull request link should look like for this assignment: `https://github.com//sql/pulls/` + * Open a private window in your browser. Copy and paste the link to your pull request into the address bar. Make sure you can see your pull request properly. This helps the technical facilitator and learning support staff review your submission easily. + +Checklist: +- [ ] Create a branch called `assignment-two`. +- [ ] Ensure that the repository is public. +- [ ] Review [the PR description guidelines](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md#guidelines-for-pull-request-descriptions) and adhere to them. +- [ ] Verify that the link is accessible in a private browser window. + +If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges. + +*** + +## Section 1: +You can start this section following *session 1*, but you may want to wait until you feel comfortable wtih basic SQL query writing. + +Steps to complete this part of the assignment: +- Design a logical data model +- Duplicate the logical data model and add another table to it following the instructions +- Write, within this markdown file, an answer to Prompt 3 + + +### Design a Logical Model + +#### Prompt 1 +Design a logical model for a small bookstore. 📚 + +At the minimum it should have employee, order, sales, customer, and book entities (tables). Determine sensible column and table design based on what you know about these concepts. Keep it simple, but work out sensible relationships to keep tables reasonably sized. + +Additionally, include a date table. +A date table (also called a calendar table) is a permanent table containing a list of dates and various components of those dates. Some theory, tips, and commentary can be found [here](https://www.sqlshack.com/designing-a-calendar-table/), [here](https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/) and [here](https://sqlgeekspro.com/creating-calendar-table-sql-server/). +Remember, you don't actually need to run any of the queries in these articles, but instead understand *why* date tables in SQL make sense, and how to situate them within your logical models. + +There are several tools online you can use, I'd recommend [Draw.io](https://www.drawio.com/) or [LucidChart](https://www.lucidchart.com/pages/). + +**HINT:** You do not need to create any data for this prompt. This is a conceptual model only. + +#### Prompt 2 +We want to create employee shifts, splitting up the day into morning and evening. Add this to the ERD. + +#### Prompt 3 +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. + + +For Type 1, the database would overwrite the old addresses whenever a customer updates their address. This keeps things clean and up to date, but the issue is that you lose the history of previous customer addresses once it is updated because the old address is deleted. + +For Type 2, the database would retain changes by creating a new row for the updated address instead of overwriting the old one. This allows the bookstore to keep address history and see previous customer information if needed. For example, if customer_id = 1, the table could contain both customer_id 1 with address 1 and customer_id 1 with address 2 stored as separate rows. + + + +*** + +## Section 2: +You can start this section following *session 4*. + +Steps to complete this part of the assignment: +- Open the assignment2.sql file in DB Browser for SQLite: + - from [Github](./02_activities/assignments/assignment2.sql) + - or, from your local forked repository +- Complete each question, by writing responses between the QUERY # and END QUERY blocks + + +### Write SQL + +#### 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 column with nulls, and 'unit' for the second column with nulls. + +**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(). + +Filter the visits to dates before April 29, 2022. + +2. Reverse the numbering of the query 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. +**HINT**: Do not use the previous visit dates filter. + +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. + +You can make this a running count by including an ORDER BY within the PARTITION BY if desired. +Filter the visits to dates before April 29, 2022. + +
-
+ +#### 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: +You can start this section following *session 5*. + +Steps to complete this part of the assignment: +- Open the assignment2.sql file in DB Browser for SQLite: + - from [Github](./02_activities/assignments/assignment2.sql) + - or, from your local forked repository +- Complete each question, by writing responses between the QUERY # and END QUERY blocks + +### Write SQL + +#### 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_unit 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 whatever product you added. + +**HINT**: If you don't specify a WHERE clause, [you are going to have a bad time](https://imgflip.com/i/8iq872). + +
-
+ +#### 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. diff --git a/Assignment2pg1.drawio.png b/Assignment2pg1.drawio.png new file mode 100644 index 000000000..d49b5c1c1 Binary files /dev/null and b/Assignment2pg1.drawio.png differ diff --git a/Assignment2pg2.drawio.png b/Assignment2pg2.drawio.png new file mode 100644 index 000000000..11c0c0584 Binary files /dev/null and b/Assignment2pg2.drawio.png differ