Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion 02_activities/assignments/DC_Cohort/Assignment1.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Please do not pick the exact same tables that I have already diagrammed. For exa

***



## Section 2:
You can start this section following *session 2*.

Expand Down Expand Up @@ -205,5 +207,5 @@ Consider, for example, concepts of fariness, inequality, social structures, marg


```
Your thoughts...
One random example that I can think about that might be embedded within something as small as sinks and paper towel machines is their ability to detect skin tones. This is something I notice almost every day in the office and why I am even very passionate about equity-oriented design because with a lot of tools in society they are designed based on white skin, so when people who have darker skin tones try to use these devices they end up not working for us. While this might seem like a small problem when it comes to sinks and paper towel machines but this problem cascades when we start to this about facial recognition devices, pulse oximeters, and other technologies and tools that have deep rooted algorithmic biases embedded within their databases.
```
2 changes: 1 addition & 1 deletion 02_activities/assignments/DC_Cohort/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ The store wants to keep customer addresses. Propose two architectures for the CU
**HINT:** search type 1 vs type 2 slowly changing dimensions.

```
Your answer...
Architecture Type 1: Would be the one that overwrites the changes so this would be the customer_address table that when it is updated changes are overwriten with no history. Achitecture Type 2: Would be the retains and preserves full history of changes so this would be the one that keep tracks of the change history. For this one, it would be important to add a row that tracks the changes by keeping track of the date when changes were made or implemented.
```

***
Expand Down
78 changes: 73 additions & 5 deletions 02_activities/assignments/DC_Cohort/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +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 = 4
OR product_id = 9;


/*2. Write a query that returns all customer purchases and a new calculated column 'price' (quantity * cost_to_customer_per_qty),
Expand All @@ -23,30 +29,66 @@ 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 10;

-- option 2

-- option 2
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 *
,CASE WHEN product_qty_type = 'unit'
THEN 'unit'
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 *
,CASE WHEN product_qty_type = 'unit'
THEN 'unit'
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 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 DISTINCT vendor_name,
market_date,
vb.vendor_id,
v.vendor_id

FROM vendor as v
INNER JOIN vendor_booth_assignments as vb
ON vb.vendor_id = v.vendor_id

ORDER BY vendor_name;




Expand All @@ -56,6 +98,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_booths
FROM vendor_booth_assignments
GROUP BY vendor_id;


/* 2. The Farmer’s Market Customer Appreciation Committee wants to give a bumper
Expand All @@ -64,6 +109,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
customer_first_name,
customer_last_name,
SUM(quantity*cost_to_customer_per_qty) AS total_spent

FROM customer_purchases AS cp
INNER JOIN customer AS c
ON cp.customer_id = c.customer_id

GROUP BY customer_first_name, customer_last_name

HAVING SUM(quantity*cost_to_customer_per_qty) > 2000
ORDER BY customer_last_name, customer_first_name;


--Temp Table
Expand All @@ -78,7 +136,17 @@ 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 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');

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.
Expand Down
153 changes: 142 additions & 11 deletions 02_activities/assignments/DC_Cohort/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,29 @@ 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 || ', ' || product_size|| ' (' || product_qty_type || ')'
FROM product;

SELECT *
FROM product
WHERE product_name IS NULL
OR product_size IS NULL
OR product_qty_type IS NULL;


SELECT
COALESCE(product_name, '') || ', ' ||
COALESCE(product_size, '') || ' (' ||
COALESCE(product_qty_type, '') || ')' AS product_revised1
FROM product;


SELECT
COALESCE(product_name, '') || ', ' ||
COALESCE(product_size, '') || ' (' ||
COALESCE(product_qty_type, 'unit') || ')' AS product_revised2
FROM product;

--Windowed Functions
/* 1. Write a query that selects from the customer_purchases table and numbers each customer’s
Expand All @@ -32,18 +54,39 @@ 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
,DENSE_RANK() OVER (PARTITION BY customer_id
ORDER BY market_date) AS [denserank_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 DISTINCT customer_id, market_date
,DENSE_RANK() OVER (PARTITION BY customer_id
ORDER BY market_date DESC) AS [denserank_visit_number]
FROM customer_purchases;

SELECT DISTINCT*
FROM (
SELECT customer_id, market_date
,DENSE_RANK() OVER (PARTITION BY customer_id
ORDER BY market_date DESC) AS [visit_no_rev]
FROM customer_purchases

) t
WHERE t.visit_no_rev = 1
ORDER BY customer_id, market_date;


/* 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 customer_product_count
FROM customer_purchases
ORDER BY customer_id, product_id, market_date;

-- String manipulations
/* 1. Some product names in the product table have descriptions like "Jar" or "Organic".
Expand All @@ -57,11 +100,21 @@ 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 *
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.
Expand All @@ -73,8 +126,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. */



DROP TABLE IF EXISTS temp.sales_date;
CREATE TABLE temp.sales_date AS
SELECT market_date,
SUM(quantity*cost_to_customer_per_qty) AS total_sales
FROM customer_purchases
GROUP BY market_date;

DROP TABLE IF EXISTS temp.ranked_days;
CREATE TEMP TABLE temp.ranked_days AS
SELECT
market_date,
total_sales,
DENSE_RANK() OVER (ORDER BY total_sales DESC) AS best_day_rank,
DENSE_RANK() OVER (ORDER BY total_sales ASC) AS worst_day_rank
FROM temp.sales_date;

SELECT 'Best day' AS day_type, market_date, total_sales
FROM temp.ranked_days
WHERE best_day_rank = 1
UNION
SELECT 'Worst day' AS day_type, market_date, total_sales
FROM temp.ranked_days
WHERE worst_day_rank = 1
ORDER BY day_type, market_date;

/* SECTION 3 */

Expand All @@ -89,27 +164,55 @@ 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
vp.vendor_name,
vp.product_name,
SUM(5 * vp.price) AS total_revenue
FROM (
SELECT DISTINCT
vi.vendor_id,
vi.product_id,
v.vendor_name,
p.product_name,
vi.original_price AS price
FROM vendor_inventory AS vi
JOIN vendor AS v ON v.vendor_id = vi.vendor_id
JOIN product AS p ON p.product_id = vi.product_id
) AS vp
CROSS JOIN (
SELECT customer_id FROM customer
) AS c
GROUP BY vp.vendor_name, vp.product_name
ORDER BY vp.vendor_name, vp.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`. */


CREATE TABLE product_units AS
SELECT *,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product AS p
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
VALUES(25,'Green Apples','small',1,'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_units
--SELECT * FROM product_units
WHERE product_id = 25

-- UPDATE
/* 1.We want to add the current_quantity to the product_units table.
Expand All @@ -128,6 +231,34 @@ 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 product_units
ADD current_quantity INT


WITH last_qty_per_product AS (
SELECT
vi.product_id,
COALESCE(
(SELECT v2.quantity
FROM vendor_inventory AS v2
WHERE v2.product_id = vi.product_id
ORDER BY v2.market_date DESC -- most recent first
LIMIT 1
),
0
) AS last_quantity
FROM vendor_inventory AS vi
GROUP BY vi.product_id
)
UPDATE product_units AS pu
SET current_quantity = (
SELECT l.last_quantity
FROM last_qty_per_product AS l
WHERE l.product_id = pu.product_id
)
WHERE EXISTS (
SELECT 1
FROM last_qty_per_product AS l2
WHERE l2.product_id = pu.product_id
)

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading