diff --git a/02_activities/assignments/DC_Cohort/Assignment1.md b/02_activities/assignments/DC_Cohort/Assignment1.md index d5ff55ce4..82243b7be 100644 --- a/02_activities/assignments/DC_Cohort/Assignment1.md +++ b/02_activities/assignments/DC_Cohort/Assignment1.md @@ -206,8 +206,20 @@ Link if you encounter a paywall: https://archive.is/srKHV or https://web.archive **What values systems are embedded in databases and data systems you encounter in your day-to-day life?** Consider, for example, concepts of fariness, inequality, social structures, marginalization, intersection of technology and society, etc. - - ``` Your thoughts... ``` +China's national ID (身份证 shenfenzheng) and household registration (户口 hukou). Similar to what I have read about the database of Pakistan, I think these systems reflect certain heteronormative assumptions about gender and family structures. + +First, the official gender classification recorded on Chinese national IDs is binary — either male or female. This excludes people who identify as non-binary, as there is no option that accurately represents their gender identity. +Consequently, non-binary individuals may feel marginalized or invisible within the legal system, since their identities cannot be adequately represented in official records. +What I find particularly interesting is that this binary classification is treated as a seemingly natural and objective way of categorizing people, even though it reflects particular social understandings of gender. + +Meanwhile, because same-sex marriage is not legally recognized in mainland China, same-sex couples cannot register as married partners. This can create significant difficulties when dealing with legal and administrative matters. +For instance, if one partner experiences a medical emergency and cannot make decisions independently, the other partner is not automatically recognized as their legal spouse or next of kin. +Same-sex couples do not automatically receive the same legal recognition as married heterosexual couples. + +Furthermore, even if same-sex partners raise children together, both partners may not be legally recognized as the children's parents in the household registration system. +In particular, a non-biological parent may face difficulties establishing a legally recognized parental relationship, despite having actively participated in raising the child. +This can create complications in matters involving guardianship, inheritance, and other parental responsibilities involved in schooling and children's education. + diff --git a/02_activities/assignments/DC_Cohort/Assignment2.md b/02_activities/assignments/DC_Cohort/Assignment2.md index 72f8c71fd..b40da62b0 100644 --- a/02_activities/assignments/DC_Cohort/Assignment2.md +++ b/02_activities/assignments/DC_Cohort/Assignment2.md @@ -59,7 +59,7 @@ The store wants to keep customer addresses. Propose two architectures for the CU ``` Your answer... ``` - +retaining changes is type 2, and overwriting is type1 *** ## Section 2: diff --git a/02_activities/assignments/DC_Cohort/assignment1.sql b/02_activities/assignments/DC_Cohort/assignment1.sql index a80923591..058a96d24 100644 --- a/02_activities/assignments/DC_Cohort/assignment1.sql +++ b/02_activities/assignments/DC_Cohort/assignment1.sql @@ -6,7 +6,7 @@ --SELECT /* 1. Write a query that returns everything in the customer table. */ --QUERY 1 - +SELECT * FROM customer ; @@ -17,9 +17,10 @@ sorted by customer_last_name, then customer_first_ name. */ --QUERY 2 - - - +SELECT * +FROM customer +ORDER BY customer_last_name, customer_first_name +LIMIT 10; --END QUERY @@ -27,9 +28,10 @@ sorted by customer_last_name, then customer_first_ name. */ /* 1. Write a query that returns all customer purchases of product IDs 4 and 9. Limit to 25 rows of output. */ --QUERY 3 - - - +SELECT * +FROM customer_purchases +WHERE product_id = 4 OR product_id = 9 +LIMIT 25; --END QUERY @@ -42,10 +44,10 @@ filtered by customer IDs between 8 and 10 (inclusive) using either: Limit to 25 rows of output. */ --QUERY 4 - - - - +SELECT *, quantity * cost_per_quantity AS price +FROM customer_purchases +WHERE customer_id BETWEEN 8 AND 10 +LIMIT 25; --END QUERY @@ -55,9 +57,12 @@ Using the product table, write a query that outputs the product_id and product_n 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.” */ --QUERY 5 - - - +SELECT product_id, product_name, + CASE + WHEN product_qty_type = 'unit' THEN 'unit' + ELSE 'bulk' + END AS prod_qty_type_condensed +FROM product; --END QUERY @@ -67,8 +72,16 @@ add a column to the previous query called pepper_flag that outputs a 1 if the pr contains the word “pepper” (regardless of capitalization), and otherwise outputs 0. */ --QUERY 6 - - +SELECT product_id, product_name, + CASE + WHEN product_qty_type = 'unit' THEN 'unit' + ELSE 'bulk' + END AS prod_qty_type_condensed, + CASE + WHEN product_name LIKE '%pepper%' THEN 1 + ELSE 0 + END AS pepper_flag +FROM product; --END QUERY @@ -78,9 +91,12 @@ contains the word “pepper” (regardless of capitalization), and otherwise out vendor_id field they both have in common, and sorts the result by market_date, then vendor_name. Limit to 24 rows of output. */ --QUERY 7 - - - +SELECT * +FROM vendor AS v +INNER JOIN vendor_booth_assignments AS vba +ON v.vendor_id = vba.vendor_id +ORDER BY market_date, vendor_name +LIMIT 24; --END QUERY @@ -92,9 +108,10 @@ Limit to 24 rows of output. */ /* 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. */ --QUERY 8 - - - +SELECT vendor_id, + SUM (booth_number) AS vendor_renting_times +FROM vendor_booth_assignments +GROUP BY vendor_id; --END QUERY @@ -106,8 +123,13 @@ 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. */ --QUERY 9 - - +SELECT cp.customer_id, cp.quantity, cp.cost_per_quantity, c.customer_id, c.customer_first_name, c.customer_last_name, + SUM(cp.quantity * cp.cost_per_quantity) AS total_purchase +FROM customer_purchases AS cp +INNER JOIN customer AS c +ON cp.customer_id = c.customer_id +GROUP BY c.customer_id +HAVING total_purchase > 2000; --END QUERY @@ -124,9 +146,12 @@ When inserting the new vendor, you need to appropriately align the columns to be VALUES(col1,col2,col3,col4,col5) */ --QUERY 10 +CREATE TEMP 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' ) --END QUERY @@ -134,14 +159,14 @@ 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 +HINT: you might need to search for strfrtime modifiers sqlite on the web to know what the modifiers for month and year are! Limit to 25 rows of output. */ --QUERY 11 - - - - +SELECT customer_id, + STRFTIME('%m', market_date) AS month, + STRFTIME('%Y', market_date) AS year +FROM customer_purchases; --END QUERY @@ -152,8 +177,13 @@ HINTS: you will need to AGGREGATE, GROUP BY, and filter... but remember, STRFTIME returns a STRING for your WHERE statement... AND be sure you remove the LIMIT from the previous query before aggregating!! */ --QUERY 12 - - - - ---END QUERY +SELECT customer_id, + quantity, + cost_per_quantity, + STRFTIME('%m', market_date) AS month, + STRFTIME('%Y', market_date) AS year, + SUM(quantity * cost_per_quantity) AS total_purchase +FROM customer_purchases +WHERE month = '04' AND year = '2022' +GROUP BY customer_id, year, month; +--END QUERY \ No newline at end of file diff --git a/02_activities/assignments/DC_Cohort/assignment2.sql b/02_activities/assignments/DC_Cohort/assignment2.sql index 4079c18ae..46aa9af60 100644 --- a/02_activities/assignments/DC_Cohort/assignment2.sql +++ b/02_activities/assignments/DC_Cohort/assignment2.sql @@ -22,10 +22,11 @@ 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 + product_name || ', ' || + COALESCE(product_size, '') || + ' (' || COALESCE(product_qty_type, 'unit') || ')' +FROM product; --END QUERY @@ -40,10 +41,16 @@ each new market date for each customer, or select only the unique market dates p 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_number +FROM ( + SELECT DISTINCT + customer_id, + market_date + FROM customer_purchases + WHERE market_date < '2022-04-29'); --END QUERY @@ -52,9 +59,19 @@ then write another query that uses this one as a subquery (or temp table) and fi 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, + ROW_NUMBER() OVER ( + PARTITION BY customer_id + ORDER BY market_date DESC + ) AS visit_number + FROM customer_purchases +) AS ranked_visits +WHERE visit_number = 1 +ORDER BY customer_id; --END QUERY @@ -65,10 +82,14 @@ customer_purchases table that indicates how many different times that customer h 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' +ORDER BY customer_id, product_id; --END QUERY @@ -84,19 +105,24 @@ 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. */ --QUERY 5 - - - - +SELECT + product_name, + TRIM(SUBSTR( + product_name, + NULLIF(INSTR(product_name, '-'), 0) + 1 + ) + ) AS description +FROM product; --END QUERY /* 2. Filter the query to show any product_size value that contain a number with REGEXP. */ --QUERY 6 - - - - +SELECT + product_name, + product_size +FROM product +WHERE product_size REGEXP '[0-9]'; --END QUERY @@ -110,14 +136,38 @@ 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. */ --QUERY 7 - - - - +WITH daily_sales AS ( + SELECT + market_date, + SUM(quantity * cost_per_quantity) AS total_sales + FROM customer_purchases + GROUP BY market_date), +ranked_sales AS ( + SELECT + market_date, + total_sales, + RANK() OVER (ORDER BY total_sales DESC) AS best_rank, + RANK() OVER (ORDER BY total_sales ASC) AS worst_rank + FROM daily_sales +) +SELECT + 'Best Day' AS day_type, + market_date, + total_sales +FROM ranked_sales +WHERE best_rank = 1 + +UNION + +SELECT + 'Worst Day' AS day_type, + market_date, + total_sales +FROM ranked_sales +WHERE worst_rank = 1; --END QUERY - /* SECTION 3 */ -- Cross Join @@ -131,9 +181,39 @@ 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). */ --QUERY 8 - - - +WITH vendor_products AS ( + SELECT + vendor_id, + product_id, + AVG(original_price) AS price + FROM vendor_inventory + GROUP BY vendor_id, product_id +), + +potential_sales AS ( + SELECT + vp.vendor_id, + vp.product_id, + 5 * vp.price AS sales_per_customer + FROM vendor_products AS vp + CROSS JOIN customer AS c +) + +SELECT + v.vendor_name, + p.product_name, + ROUND(SUM(ps.sales_per_customer), 2) AS total_sales +FROM potential_sales AS ps +JOIN vendor AS v + ON ps.vendor_id = v.vendor_id +JOIN product AS p + ON ps.product_id = p.product_id +GROUP BY + ps.vendor_id, + ps.product_id, + v.vendor_name, + p.product_name +ORDER BY v.vendor_name, p.product_name; --END QUERY @@ -144,20 +224,25 @@ 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 +SELECT + *, + CURRENT_TIMESTAMP AS snapshot_timestamp +FROM product +WHERE product_name = 'Apple Pie' +LIMIT 1; --END QUERY @@ -166,10 +251,14 @@ This can be any product you desire (e.g. add another record for Apple Pie). */ HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/ --QUERY 11 - - - - +DELETE FROM product_units +WHERE rowid = ( + SELECT rowid + FROM product_units + WHERE product_name = 'Apple Pie' + ORDER BY snapshot_timestamp ASC, rowid ASC + LIMIT 1 +); --END QUERY @@ -190,10 +279,10 @@ 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 +ALTER TABLE product_units +ADD current_quantity INT; - - - +UPDATE product_units --END QUERY diff --git a/02_activities/assignments/DC_Cohort/assignment2_promt2.png b/02_activities/assignments/DC_Cohort/assignment2_promt2.png new file mode 100644 index 000000000..7240a6fc5 Binary files /dev/null and b/02_activities/assignments/DC_Cohort/assignment2_promt2.png differ diff --git a/02_activities/assignments/DC_Cohort/assignment2_section1.png b/02_activities/assignments/DC_Cohort/assignment2_section1.png new file mode 100644 index 000000000..2471346bb Binary files /dev/null and b/02_activities/assignments/DC_Cohort/assignment2_section1.png differ diff --git a/02_activities/assignments/DC_Cohort/logical_datamodel_XL.png b/02_activities/assignments/DC_Cohort/logical_datamodel_XL.png new file mode 100644 index 000000000..37908d5fb Binary files /dev/null and b/02_activities/assignments/DC_Cohort/logical_datamodel_XL.png differ