Skip to content
Merged
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
16 changes: 14 additions & 2 deletions 02_activities/assignments/DC_Cohort/Assignment2.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ 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...
The updated diagram (ERD_3) presents two ways to store customer addresses, depending on whether the business needs history or only the current information.

The first method (on the left) keeps just one address per customer. When the customer moves, the old address is simply overwritten. This Type 1 SCD approach is straightforward. It works well for tasks like shipping or billing, but any past addresses disappear because no history is kept.

The second method (on the right) adds versioning. Each time an address changes, the previous record becomes inactive and a new row is added with start/end dates. This Type 2 SCD preserves every address a customer has had, enabling historical reporting. It requires more data storage, but provides traceability.
```

***
Expand Down Expand Up @@ -183,5 +187,13 @@ Consider, for example, concepts of labour, bias, LLM proliferation, moderating c


```
Your thoughts...
When Vicki Boykis writes that “every single piece of decision-making in a high-tech neural network initially rests on a human being manually putting something together and making a choice,” she exposes something that’s easy to forget. Behind every AI system are countless human hands and minds. The choices they make, sometimes rushed, sometimes unconscious, shape how these systems see the world.

Bias in AI isn’t magic or mystery. It’s people. When Mechanical Turk workers label an image or when linguists in the 1960s decide which words belong together as explained in the piece, or an engineer picks what examples to include in a dataset, each decision changes the model’s worldview. Even the act of defining a category, what counts as a “person,” a “pizza,” or a “criminal” is a judgment.

So when AI systems mislabel someone or reinforce a stereotype, they are not going wrong. They’re just revealing the human assumptions embedded into them. ImageNet Roulette showed that the machine wasn’t being offensive on purpose;, but it learned to reflect the messy, biased judgments of the humans who built its training data, often without realizing the harm those labels could cause.

The most troubling part is what happens when these human-made judgments become embedded in systems that increasingly shape everyday life. A biased label in a dataset might seem harmless, but once that dataset powers hiring tools or surveillance systems, a private human choice becomes a public social consequence.

This is where technology and society collide. AI systems are treated as "neutral", and more and more authoritative, even though their foundations are built from the decisions of real people. Recognizing this human core doesn’t make AI less powerful, but it makes its ethical stakes clearer. If AI is built out of human choices, then building fairer systems means taking responsibility for the people and social dynamics that shape those choices in the first place.
```
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
159 changes: 158 additions & 1 deletion 02_activities/assignments/DC_Cohort/assignment2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ 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 || ', ' || COALESCE(product_size,'') || ' (' || COALESCE(product_qty_type,'unit') || ')' AS product_label
FROM product;


--Windowed Functions
Expand All @@ -32,17 +35,64 @@ 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(). */

WITH visits AS (
SELECT DISTINCT customer_id, market_date
FROM customer_purchases
)
SELECT
customer_id,
market_date,
DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number
FROM visits
ORDER BY customer_id, market_date;



/* 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 visits AS (
SELECT DISTINCT customer_id, market_date
FROM customer_purchases
),
ranked AS (
SELECT
customer_id,
market_date,
DENSE_RANK() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_reverse
FROM visits
)
SELECT *
FROM ranked
WHERE visit_reverse = 1
ORDER BY customer_id;


/* 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. */

WITH distinct_visit AS (
SELECT DISTINCT customer_id, product_id, market_date
FROM customer_purchases
),
counts AS (
SELECT
customer_id,
product_id,
market_date,
COUNT(*) OVER (PARTITION BY customer_id, product_id) AS distinct_days_for_purchased_product
FROM distinct_visit
)
SELECT
cp.*,
c.distinct_days_for_purchased_product
FROM customer_purchases cp
JOIN counts c
ON c.customer_id = cp.customer_id
AND c.product_id = cp.product_id
AND c.market_date = cp.market_date;



-- String manipulations
Expand All @@ -57,11 +107,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,6 +133,31 @@ 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,
COUNT(*) AS total_sales
FROM customer_purchases
GROUP BY market_date
),

ranked AS (
SELECT
market_date,
total_sales,
RANK() OVER (ORDER BY total_sales DESC) AS r_desc,
RANK() OVER (ORDER BY total_sales ASC) AS r_asc
FROM sales_by_date
)

SELECT 'best_day' AS label, market_date, total_sales
FROM ranked
WHERE r_desc = 1
UNION
SELECT 'worst_day' AS label, market_date, total_sales
FROM ranked
WHERE r_asc = 1
ORDER BY label;



Expand All @@ -89,6 +174,16 @@ 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
v.vendor_name,
p.product_name,
( (SELECT COUNT(*) FROM customer) * 5 * vi.original_price ) AS projected_total_revenue
FROM vendor_inventory vi
JOIN vendor v
ON vi.vendor_id = v.vendor_id
JOIN product p
ON vi.product_id = p.product_id
ORDER BY v.vendor_name, p.product_name;


-- INSERT
Expand All @@ -97,19 +192,61 @@ 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`. */

DROP TABLE IF EXISTS product_units;

CREATE TABLE product_units AS
SELECT
*,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_qty_type = 'unit';

SELECT * FROM product_units;

/*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_qty_type,
product_category_id,
snapshot_timestamp
)
VALUES (
10,
'Apple Pie',
'Large',
'unit',
3,
CURRENT_TIMESTAMP
);

SELECT * FROM product_units WHERE product_name = '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.*/

SELECT *
FROM product_units
WHERE product_name = 'Apple Pie';

DELETE FROM product_units
WHERE product_name = 'Apple Pie'
AND snapshot_timestamp < (
SELECT MAX(snapshot_timestamp)
FROM product_units
WHERE product_name = 'Apple Pie'
);

SELECT *
FROM product_units
WHERE product_name = 'Apple Pie';

-- UPDATE
/* 1.We want to add the current_quantity to the product_units table.
Expand All @@ -129,5 +266,25 @@ 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. */


-- Adding the column by altering the table
ALTER TABLE product_units
ADD COLUMN current_quantity INT;

-- coalesce null values to 0
UPDATE product_units
SET current_quantity = 0
WHERE current_quantity IS NULL;

-- filling with the most recent vendor_inventory.quantity for each product
UPDATE product_units AS pu
SET current_quantity = COALESCE((
SELECT vi.quantity
FROM vendor_inventory vi
WHERE vi.product_id = pu.product_id
ORDER BY vi.rowid DESC
LIMIT 1
), 0);

SELECT product_id, product_name, current_quantity
FROM product_units;

Loading