diff --git a/.gitignore b/.gitignore index 6d4f0c0d6..4a2a7b485 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .DS_Store .vscode/ .sqbpro +#.sqbpro + diff --git a/03_homework/Homework/Homework_4.sqbpro b/03_homework/Homework_4.sqbpro similarity index 100% rename from 03_homework/Homework/Homework_4.sqbpro rename to 03_homework/Homework_4.sqbpro diff --git a/03_homework/Homework/Homework_6.pdf b/03_homework/Homework_6.pdf similarity index 100% rename from 03_homework/Homework/Homework_6.pdf rename to 03_homework/Homework_6.pdf diff --git a/03_homework/Homework/homework 1.jpeg b/03_homework/homework 1.jpeg similarity index 100% rename from 03_homework/Homework/homework 1.jpeg rename to 03_homework/homework 1.jpeg diff --git a/03_homework/homework_-1-6/Homework_4.sqbpro b/03_homework/homework_-1-6/Homework_4.sqbpro new file mode 100644 index 000000000..10a9cf061 --- /dev/null +++ b/03_homework/homework_-1-6/Homework_4.sqbpro @@ -0,0 +1,92 @@ +
--COALESCE +SELECT + product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')' AS product_description +FROM product; + +--#1 Windowed Functions +SELECT customer_id, market_date +FROM customer_purchases; + +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 + +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 visits +WHERE + visit_number = 1; + + -- #3 Using a COUNT() +SELECT + customer_id, + product_id, + market_date, + COUNT(*) OVER (PARTITION BY customer_id, product_id) AS purchase_count +FROM + customer_purchases; + + + --String Manipulation + SELECT + product_name, + TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1)) AS description +FROM + product +WHERE + INSTR(product_name, '-') > 0; + +--UNION +WITH SalesByDate AS ( + SELECT + market_date, + SUM(quantity * cost_to_customer_per_qty) AS total_sales + FROM + customer_purchases + GROUP BY + market_date +), +RankedSales AS ( + SELECT + market_date, + total_sales, + RANK() OVER (ORDER BY total_sales DESC) AS sales_rank_desc, + RANK() OVER (ORDER BY total_sales ASC) AS sales_rank_asc + FROM + SalesByDate +) +SELECT + market_date, + total_sales, + 'Highest Sales' AS description +FROM + RankedSales +WHERE + sales_rank_desc = 1 + +UNION + +SELECT + market_date, + total_sales, + 'Lowest Sales' AS description +FROM + RankedSales +WHERE + sales_rank_asc = 1; + +
diff --git a/03_homework/homework_-1-6/Homework_6.pdf b/03_homework/homework_-1-6/Homework_6.pdf new file mode 100644 index 000000000..dee678cb3 Binary files /dev/null and b/03_homework/homework_-1-6/Homework_6.pdf differ diff --git a/03_homework/homework_-1-6/homework 1.jpeg b/03_homework/homework_-1-6/homework 1.jpeg new file mode 100644 index 000000000..6a700ec40 Binary files /dev/null and b/03_homework/homework_-1-6/homework 1.jpeg differ diff --git a/03_homework/Homework/homework_2.sqbpro b/03_homework/homework_-1-6/homework_2.sqbpro similarity index 100% rename from 03_homework/Homework/homework_2.sqbpro rename to 03_homework/homework_-1-6/homework_2.sqbpro diff --git a/03_homework/Homework/homework_3.sqbpro b/03_homework/homework_-1-6/homework_3.sqbpro similarity index 100% rename from 03_homework/Homework/homework_3.sqbpro rename to 03_homework/homework_-1-6/homework_3.sqbpro diff --git a/03_homework/Homework/homework_5.sqbpro b/03_homework/homework_-1-6/homework_5.sqbpro similarity index 100% rename from 03_homework/Homework/homework_5.sqbpro rename to 03_homework/homework_-1-6/homework_5.sqbpro diff --git a/03_homework/homework_1.jpeg b/03_homework/homework_1.jpeg new file mode 100644 index 000000000..6a700ec40 Binary files /dev/null and b/03_homework/homework_1.jpeg differ diff --git a/03_homework/homework_1.md b/03_homework/homework_1.md deleted file mode 100644 index c50fe783c..000000000 --- a/03_homework/homework_1.md +++ /dev/null @@ -1,78 +0,0 @@ -# Homework 1: farmersmarket.db - -- Due on Thursday, May 16 at 11:59pm -- Weight: 8% of total grade - -## Get to know the farmersmarket.db -Steps to complete this part of the homework: - -#### 1) Load Database -- Open DB Browser for SQLite -- Go to File > Open Database -- Navigate to your farmersmarket.db - - This will be wherever you cloned the GH Repo (within the **SQL** folder) - - ![db_browser_for_sqlite_choose_db.png](./images/01_db_browser_for_sqlite_choose_db.png) - -#### 2) Configure your windows -By default, DB Browser for SQLite has three windows, with four tabs in the main window and three tabs in the bottom right window -- Window 1: Main Window (Centre) - - Stay in the Database Structure tab for now -- Window 2: Edit Database Cell (Top Right) -- Window 3: Remote (Bottom Right) - - Switch this to DB Schema tab (very bottom) - -Your screen should look like this (or very similar) -![db_browser_for_sqlite.png](./images/01_db_browser_for_sqlite.png) - -#### 3) The farmersmarket.db -There are 10 tables in the Main Window: -1) booth -2) customer -3) customer_purchases -4) market_date_info -5) product -6) product_category -7) vendor -8) vendor_booth_assignments -9) vendor_inventory -10) zip_data - -Switch to the Browse Data tab, booth is selected by default - ![01_the_browse_data_tab.png](./images/01_the_browse_data_tab.png) - - -Using the table drop down at the top left, explore some of the contents of the database -![01_the_table_drop_down_at_the_top_left.png](./images/01_the_table_drop_down_at_the_top_left.png) - -Move on to the Logical Data Model task when you have looked through the tables - - -## Logical Data Model - -Recall during the module: - -I diagramed the following four tables: -- product -- product_category -- vendor -- vendor_inventory - -![01_farmers_market_logical_model_partial.png](./images/01_farmers_market_logical_model_partial.png) - - -Your task: choose two tables and create a logical data model. There are lots of tools you can do this (including drawing this by hand), but I'd recommend [Draw.io](https://www.drawio.com/) or [LucidChart](https://www.lucidchart.com/pages/). - -A logical data model must contain: -- table name -- column names -- relationship type - -Please do not pick the exact same tables that I have already diagramed. For example, you shouldn't diagram the relationship between `product` and `product_category`, but you could diagram `product` and `customer_purchases`. - -**A few hints**: -- You will need to use the Browse Data tab in the main window to figure out the relationship types. -- You can't diagram tables that don't share a common column - - These are the tables that are connected - - ![01_farmers_market_conceptual_model.png](./images/01_farmers_market_conceptual_model.png) -- The column names can be found in a few spots (DB Schema window in the bottom right, the Database Structure tab in the main window by expanding each table entry, at the top of the Browse Data tab in the main window) - diff --git a/03_homework/homework_2.md b/03_homework/homework_2.md deleted file mode 100644 index bd3055227..000000000 --- a/03_homework/homework_2.md +++ /dev/null @@ -1,23 +0,0 @@ -# Homework 2: Basic SQL - -- Due on Friday, May 17 at 11:59pm -- Weight: 8% of total grade -- Upload one .sql file with your queries - -# SELECT -1. Write a query that returns everything in the customer table. -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. - -# WHERE -1. Write a query that returns all customer purchases of product IDs 4 and 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 - -# 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.” - -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. - -# 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`. diff --git a/03_homework/homework_2.sqbpro b/03_homework/homework_2.sqbpro new file mode 100644 index 000000000..8f6ac243f --- /dev/null +++ b/03_homework/homework_2.sqbpro @@ -0,0 +1,48 @@ +--SELECT Function by last, first name 10 rows + +SELECT *FROM customer +ORDER BY customer_last_name, customer_first_name +LIMIT 10; + +--WHERE #1 Function product IDs 4 and 9. +SELECT * FROM customer_purchases +WHERE product_id IN (4, 9); + +--WHERE #2 AND and BETWEEN Function +SELECT *, (quantity * cost_to_customer_per_qty) AS price +FROM customer_purchases +WHERE vendor_id BETWEEN 8 and 10; + +-- CASE #1 +SELECT product_id, + product_name, + CASE + WHEN product_qty_type = 'unit' THEN 'unit' + ELSE 'bulk' + END AS product_qty_type_condensed +FROM product; + + +-- CASE #2 - 'pepper' function +SELECT product_id, + product_name, + CASE + WHEN 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 +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; + diff --git a/03_homework/homework_3.md b/03_homework/homework_3.md deleted file mode 100644 index 3f089c3f9..000000000 --- a/03_homework/homework_3.md +++ /dev/null @@ -1,24 +0,0 @@ -# Homework 3: Essential SQL - -- Due on Saturday, May 17 at 11:59pm -- Weight: 8% of total grade -- Upload one .sql file with your queries - -# 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`. -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. - -# 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) - -To insert the new row use VALUES, specifying the value you want for each column: -`VALUES(col1,col2,col3,col4,col5)` - -# 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! -2. Using the previous query as a base, determine how much money each customer spent in April 2019. 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!! - diff --git a/03_homework/homework_3.sqbpro b/03_homework/homework_3.sqbpro new file mode 100644 index 000000000..897b49257 --- /dev/null +++ b/03_homework/homework_3.sqbpro @@ -0,0 +1,43 @@ +
-- Homework 3 Essential SQL +--AGGREGATE #1 +--Vendor booth Acount +SELECT vendor_id, COUNT(*) AS booth_count +FROM vendor_booth_assignments +GROUP BY vendor_id; + +--AGGREGATE #2 +SELECT c.customer_id, c.customer_last_name, c.customer_first_name, +SUM(cp.quantity * cp.cost_to_customer_per_qty) AS total_spent +FROM customer_purchases cp +JOIN customer c ON cp.customer_id = c.customer_id +GROUP BY c.customer_id, c.customer_last_name, c.customer_first_name +HAVING total_spent >2000 +ORDER BY c.customer_last_name, customer_first_name; + +-- Temp TABLE +DROP TABLE IF EXISTS new_vendor; +CREATE 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'); +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 +SELECT + customer_id, + strftime('%m', market_date) AS month, + strftime('%Y', market_date) AS year +FROM customer_purchases; + +--customer spent in April 2019 +SELECT + customer_id, + SUM(quantity * cost_to_customer_per_qty) AS total_spent +FROM customer_purchases +WHERE + strftime('%m', market_date) = '04' -- Month is April + AND strftime('%Y', market_date) = '2019' -- Year is 2019 +GROUP BY customer_id; + +
diff --git a/03_homework/homework_4.md b/03_homework/homework_4.md deleted file mode 100644 index 945cc4fac..000000000 --- a/03_homework/homework_4.md +++ /dev/null @@ -1,45 +0,0 @@ -# Homework 4: Advanced SQL - -- Due on Thursday, May 23 at 11:59pm -- Weight: 8% of total grade -- Upload one .sql file with your queries - -# 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. - -# 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. diff --git a/03_homework/homework_5.md b/03_homework/homework_5.md deleted file mode 100644 index fb78fddbf..000000000 --- a/03_homework/homework_5.md +++ /dev/null @@ -1,37 +0,0 @@ -# Homework 5: Expanding your Database - -- Due on Friday, May 24 at 11:59pm -- Weight: 8% of total grade -- Upload one .sql file with your queries - -# 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 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](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/03_homework/homework_5.sqbpro b/03_homework/homework_5.sqbpro new file mode 100644 index 000000000..c6719f42d --- /dev/null +++ b/03_homework/homework_5.sqbpro @@ -0,0 +1,85 @@ +-- Cross Join +WITH vendor_customer_combinations AS ( + SELECT + vi.vendor_id, + vi.product_id, + c.customer_id + FROM + vendor_inventory vi + CROSS JOIN + customer c +), +vendor_sales AS ( + SELECT + v.vendor_name, + p.product_name, + COUNT(*) * 5 * vi.original_price AS total_sales + FROM + vendor_customer_combinations vc + JOIN + vendor_inventory vi ON vc.vendor_id = vi.vendor_id AND vc.product_id = vi.product_id + JOIN + vendor v ON vi.vendor_id = v.vendor_id + JOIN + product p ON vi.product_id = p.product_id + GROUP BY + v.vendor_name, p.product_name +) +SELECT + vendor_name, + product_name, + total_sales +FROM + vendor_sales +ORDER BY + vendor_name, product_name; + +-- INSERT +-- Verify if the product_units table +SELECT name +FROM sqlite_master +WHERE type='table' AND name='product_units'; + +-- Create the product_units table +CREATE TABLE IF NOT EXISTS product_units AS +SELECT *, CURRENT_TIMESTAMP AS snapshot_timestamp +FROM product +WHERE product_qty_type = 'unit'; + +INSERT INTO product_units (product_id, product_name, product_size, product_category_id, product_qty_type) +SELECT product_id, product_name, product_size, product_category_id, product_qty_type +FROM product +WHERE product_name = 'Apple Pie' AND product_qty_type = 'unit'; + +-- Check for "Apple Pie" in the product table +SELECT product_id, product_name, product_size, product_category_id, product_qty_type +FROM product +WHERE product_name = 'Apple Pie' AND product_qty_type = 'unit'; + +-- DELETE + +DELETE FROM product_units +WHERE rowid = ( + SELECT rowid + FROM product_units + WHERE product_name = 'Apple Pie' + ORDER BY snapshot_timestamp ASC + LIMIT 1 +); + +-- UPDATE + +PRAGMA table_info(product_units); + +-- Update the current_quantity column with the last quantity value from vendor_inventory +UPDATE product_units +SET current_quantity = COALESCE(( + SELECT vi.quantity + FROM vendor_inventory vi + WHERE vi.product_id = product_units.product_id + ORDER BY vi.market_date DESC + LIMIT 1 +), 0); + +SELECT * FROM product_units; + diff --git a/03_homework/homework_6.md b/03_homework/homework_6.md deleted file mode 100644 index f16c02202..000000000 --- a/03_homework/homework_6.md +++ /dev/null @@ -1,8 +0,0 @@ -# Homework 6: Reflecton - -- Due on Saturday, May 25 at 11:59pm -- Weight: 8% of total grade - -
- -**Write**: Reflect on your previous work and how you would adjust to include ethics and inequity components. Total length should be a few paragraphs, no more than one page.