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
98 changes: 97 additions & 1 deletion 02_activities/assignments/DC_Cohort/Assignment1.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ Please do not pick the exact same tables that I have already diagrammed. For exa
- <img src="./images/01_farmers_market_conceptual_model.png" width="600">
- 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)

<img src="./images/Tanveer_SQL_Assigment_1_section_1.png" alt="SQL Data Model">

***

## Section 2:
Expand All @@ -123,6 +125,14 @@ Steps to complete this part of the assignment:
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.

SELECT *
FROM customer;

SELECT *
FROM customer
ORDER BY customer_last_name, customer_first_name
LIMIT 10;

<div align="center">-</div>

#### WHERE
Expand All @@ -131,18 +141,67 @@ Steps to complete this part of the assignment:
1. two conditions using AND
2. one condition using BETWEEN

SELECT *
FROM customer_purchases
WHERE product_id = 4 OR product_id = 9;

-- 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;


<div align="center">-</div>

#### 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'
ELSE 'bulk'
END AS prod_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'
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;


<div align="center">-</div>

#### 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 v
INNER JOIN vendor_booth_assignments vba
ON v.vendor_id = vba.vendor_id
ORDER BY v.vendor_name, vba.market_date;

***

## Section 3:
Expand All @@ -154,6 +213,7 @@ Steps to complete this part of the assignment:
- or, from your local forked repository
- Complete each question


### Write SQL

#### AGGREGATE
Expand All @@ -162,6 +222,29 @@ Steps to complete this part of the assignment:

**HINT**: This query requires you to join two tables, use an aggregate function, and use the HAVING keyword.


SELECT
v.vendor_id,
v.vendor_name,
COUNT(*) AS booth_rental_count
FROM vendor v
INNER JOIN vendor_booth_assignments vba
ON v.vendor_id = vba.vendor_id
GROUP BY v.vendor_id, v.vendor_name
ORDER BY v.vendor_name;

SELECT
c.customer_id,
c.customer_first_name,
c.customer_last_name,
SUM(cp.cost_to_customer_per_qty) AS total_spent
FROM customer c
INNER JOIN customer_purchases cp
ON c.customer_id = cp.customer_id
GROUP BY c.customer_id, c.customer_first_name, c.customer_last_name
HAVING SUM(cp.cost_to_customer_per_qty) > 2000
ORDER BY c.customer_last_name, c.customer_first_name;

<div align="center">-</div>

#### Temp Table
Expand All @@ -172,6 +255,11 @@ Steps to complete this part of the assignment:
To insert the new row use VALUES, specifying the value you want for each column:
`VALUES(col1,col2,col3,col4,col5)`

CREATE TEMP 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');

<div align="center">-</div>

#### Date
Expand All @@ -183,6 +271,8 @@ To insert the new row use VALUES, specifying the value you want for each column:

**HINTS**: you will need to AGGREGATE, GROUP BY, and filter...but remember, STRFTIME returns a STRING for your WHERE statement!!

/* Not required */

***

## Section 4:
Expand All @@ -205,5 +295,11 @@ Consider, for example, concepts of fariness, inequality, social structures, marg


```
Your thoughts...
The Ghost of Values in Databases: A Marginalized Person’s Reflections
Understanding schema and relational databases allowed me to see through the technology to the human choices embedded within it. When I grasped that database designers must deliberately encode which relationships “count,” I recognized this was not just computational but political. This insight directly connects to my research on algorithms and AI, where human bias seeps into algorithmic bias through training data and design choices.
NADRA’s architects embedded assumptions about legitimate families into technical rules that marginalize orphans, khawaja sira communities, and single mothers. The schema itself becomes a gatekeeper of citizenship and rights. Edge cases reveal how the system works: orphans assigned random families, khawaja sira connected to gurus, women asked to fabricate marriage certificates. These are not bugs but the system working as designed, yet failing those whose lives do not fit the normalized template.
Databases encode inequality by naturalizing dominant social structures while rendering others invisible. Fairness is not a post-hoc consideration but baked into every design decision. The schema choice determines whose existence the system recognizes.
NADRA’s inflexibility reveals the danger of treating databases as static infrastructure rather than dynamic social systems. Good design must anticipate that categories will shift and that marginalized communities will demand recognition.
This extends to databases in my daily life. Consider Instagram, which requires users to select from gender categories that reflect cisnormative assumptions. Or Canadian financial services that use algorithms trained on historical data reflecting racial bias in lending. These systems encode whose creditworthiness the institutions value, whose life experiences count as legitimate financial history, and whose identities are recognized. Understanding the technical architecture means understanding the power structures it enforces.

```
71 changes: 68 additions & 3 deletions 02_activities/assignments/DC_Cohort/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@

--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;



Expand All @@ -23,9 +32,17 @@ 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;



Expand All @@ -34,18 +51,41 @@ filtered by customer IDs between 8 and 10 (inclusive) using either:
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'
ELSE 'bulk'
END AS prod_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'
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
/* 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 v
INNER JOIN vendor_booth_assignments vba
ON v.vendor_id = vba.vendor_id
ORDER BY v.vendor_name, vba.market_date;



Expand All @@ -55,6 +95,15 @@ vendor_id field they both have in common, and sorts the result by vendor_name, t
-- 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
v.vendor_id,
v.vendor_name,
COUNT(*) AS booth_rental_count
FROM vendor v
INNER JOIN vendor_booth_assignments vba
ON v.vendor_id = vba.vendor_id
GROUP BY v.vendor_id, v.vendor_name
ORDER BY v.vendor_name;



Expand All @@ -63,6 +112,17 @@ sticker to everyone who has ever spent more than $2000 at the market. Write a qu
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_id,
c.customer_first_name,
c.customer_last_name,
SUM(cp.cost_to_customer_per_qty) AS total_spent
FROM customer c
INNER JOIN customer_purchases cp
ON c.customer_id = cp.customer_id
GROUP BY c.customer_id, c.customer_first_name, c.customer_last_name
HAVING SUM(cp.cost_to_customer_per_qty) > 2000
ORDER BY c.customer_last_name, c.customer_first_name;



Expand All @@ -77,6 +137,10 @@ When inserting the new vendor, you need to appropriately align the columns to be
-> To insert the new row use VALUES, specifying the value you want for each column:
VALUES(col1,col2,col3,col4,col5)
*/
CREATE TEMP 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');



Expand All @@ -86,11 +150,12 @@ VALUES(col1,col2,col3,col4,col5)
HINT: you might need to search for strfrtime modifers sqlite on the web to know what the modifers for month
and year are! */


/* Not required */

/* 2. Using the previous query as a base, determine how much money each customer spent in April 2022.
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!! */

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