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
244 changes: 244 additions & 0 deletions 02_activities/Assignment1_Sandbox.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
/* One-to-Many: where a given row within a table can be referenced by multiple rows in
another table */

/* Check number of booth numbers available */
SELECT booth_number
FROM booth -- 12 booth numbers available

/* Compare how many booth_numbers are in the vendor_booth_assignments, one select with distinct, one without. */
SELECT booth_number
FROM vendor_booth_assignments -- 921 rows; There are 921 booth_number rows in vendor_booth_assignments.

SELECT DISTINCT booth_number
FROM vendor_booth_assignments -- 7 rows; There are 7 unique booth numbers assigned to vendor booths.

/* Compare how many booth_numbers and vendor_id are in vendor_booth_assignments, one select with distinct, one without. */
SELECT booth_number, vendor_id
FROM vendor_booth_assignments -- 921 rows. There are 921 booth_number and vendor_id rows.

SELECT DISTINCT booth_number, vendor_id
FROM vendor_booth_assignments -- 11 rows.

/* Compare how many booth_numbers, vendor_id and market_date are in vendor_booth_assignments, one select with distinct, one without. */
SELECT booth_number, vendor_id, market_date
FROM vendor_booth_assignments -- 921 rows

SELECT DISTINCT booth_number, vendor_id, market_date
FROM vendor_booth_assignments -- 921 rows


/* Assignment 1 - Section 2 */

/* Write a query that returns everything in the customer table. */

SELECT customer_id, customer_first_name, customer_last_name, customer_postal_code
FROM customer

SELECT *
FROM customer

/* 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 customer_id, customer_first_name, customer_last_name, customer_postal_code
FROM customer
ORDER BY customer_last_name, customer_first_name
LIMIT 10;

/* 1. Write a query that returns all customer purchases of product IDs 4 and 9. */
SELECT *
FROM customer_purchases
WHERE product_id = 4

SELECT *
FROM customer_purchases
WHERE product_id = 9

SELECT *
FROM customer_purchases
WHERE product_id IN (4,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), filtered by customer IDs between 8 and 10 (inclusive) using either:
1. two conditions using AND
2. one condition using BETWEEN */

SELECT product_id, vendor_id, market_date, customer_id, quantity, cost_to_customer_per_qty, transaction_time, (quantity*cost_to_customer_per_qty) AS price
FROM customer_purchases
WHERE customer_id BETWEEN 8 AND 10

/* CASE - Q1 */
SELECT product_id, product_name
, CASE WHEN product_qty_type = 'unit'
THEN 'unit'
ELSE 'bulk'
END prod_qty_type_condensed
FROM product;

/* CASE - Q2 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 product_name LIKE '%pepper%'
THEN 1
ELSE 0
END AS pepper_flag
FROM product;

/* Section 2 - 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
v.vendor_id,
vendor_name,
vendor_type,
vendor_owner_first_name,
vendor_owner_last_name,
booth_number,
market_date
FROM vendor AS v
INNER JOIN vendor_booth_assignments AS vb
ON v.vendor_id = vb.vendor_id
ORDER BY vendor_name, market_date


SELECT
v.vendor_id,
vendor_name,
vendor_type,
vendor_owner_first_name,
vendor_owner_last_name,
booth_number,
market_date
FROM vendor_booth_assignments AS vb
INNER JOIN vendor as v
ON v.vendor_id = vb.vendor_id
ORDER BY vendor_name, market_date

/* Secton 3 - 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
COUNT(booth_number)
, vendor_id
FROM vendor_booth_assignments
GROUP BY 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. */

SELECT
cp.customer_id
,product_id
,quantity
,cost_to_customer_per_qty
,customer_last_name
,customer_first_name
,market_date
,transaction_time
,SUM(quantity*cost_to_customer_per_qty) AS purchase_total

FROM customer_purchases AS cp
LEFT JOIN customer AS c
ON cp.customer_id = c.customer_id
ORDER BY customer_last_name, customer_first_name, customer_id
GROUP BY cp.customer_id

HAVING purchase_total > 2000


SELECT
cp.customer_id
--,product_id
--,quantity
--,cost_to_customer_per_qty
,customer_last_name
,customer_first_name
--,market_date
--,transaction_time
,SUM(quantity*cost_to_customer_per_qty) AS purchase_total
FROM customer_purchases AS cp
LEFT JOIN customer AS c
ON cp.customer_id = c.customer_id
GROUP BY cp.customer_id
HAVING purchase_total > 2000
ORDER BY customer_last_name, customer_first_name

, cp.customer_id

/* 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)`
*/

-- if a table named new_vendor exists, delete it, other do NOTHING
DROP TABLE IF EXISTS temp.new_vendor

--make the temporary new_vendor table
CREATE TABLE temp.new_vendor AS

-- define the table
SELECT *
FROM vendor;

-- put the temp.new_vendor into temp.new_new_vendor

-- if a table named new_new_vendor exists, delete it, other do NOTHING
DROP TABLE IF EXISTS temp.new_new_vendor;

-- make the temporary new_new_vendor table
CREATE TABLE temp.new_new_vendor AS

SELECT *
FROM temp.new_vendor;

-- add a single row of additonal data (see: https://www.w3schools.com/sql/sql_insert.asp)
INSERT INTO temp.new_new_vendor
VALUES (10,'Thomass Superfood Store','Fresh Focused','Thomas','Rosenthal');



(vendor_id, vendor_name, vendor_type, vendor_owner_first_name, vendor_owner_last_name)
/* 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 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!!
*/










-- Number of unique vendor_id values
SELECT COUNT(DISTINCT vendor_id)
FROM vendor_booth_assignments

-- Number of rows with unique combinations of vendor_id and booth_number
SELECT COUNT(*)
From (
SELECT DISTINCT vendor_id, booth_number
FROM vendor_booth_assignments
) AS number_vendor_booths

-- Sum of rows with unique combinations of vendor_id and booth_number
SELECT COUNT(*) FROM vendor_booth_assignments




FROM (
SELECT DISTINCT vendor_id
vendor_booth_assignments
2 changes: 2 additions & 0 deletions 02_activities/assignments/DC_Cohort/Assignment1.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,6 @@ Consider, for example, concepts of fariness, inequality, social structures, marg

```
Your thoughts...

When I consider a database that I have spent some time exploring, and the value systems embedded in it, I think of the Ontario Ministry of Education's datasets stored in the Government of Ontario's Data Catalogue: https://data.ontario.ca/dataset/?keywords_en=Education+and+Training. In the two datasets that I've recently downloaded and linked on the variable "district school board" the following: 1. School board financial reports, https://data.ontario.ca/dataset/school-board-financial-reports-estimates-revised-estimates-and-financial-statements, and 2. School information and student demographics https://data.ontario.ca/dataset/school-information-and-student-demographics, political, economic, and social value systems are evident. My interest in the datasets is primarily centred on special education identification and resourcing for students with special education needs. Data variable definitions can be vague and student special education counts vary across school boards, in part because board policies depend not only on the provincial legal framework government policy, but also on local political, economic and community pressures,. This can have the effect of marginalizing some student learning needs and can reinforce inequality of learning opportunity across Ontario's public education system. Moreover, data information definitions do not necessarily track changes in how special education programs are actually administered, generating, in the case of Ontario, essentially a bifurcation in how special education need is measured between "formally" and "informally" identified students and the corresponding funding they receive. Here, the Ontario education data system fails to capture the dynamic nature of public education policy development across the province, over time.
```
6 changes: 4 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,9 @@ 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...
Customers can move, sometimes necessitating address record changes over time. Therefore, it is necessary to decide whether to overwrite the address record with the new data information (Type 1 SCD) or to preserve the former address(es) and update the existing record with the new data information. (Type 2 SCD). It is necessary to add a column to record the date the record was initialized or updated (i.e,., customer_entry_date) and the date the record became obsolete (I.e., customer_end_date). A TRUE/FALSE boolean indicator shows whether the record is a current bookstore customer (TRUE) or former bookstore customer(FALSE). For Type 2 SCD, each update generates an additional customer address record.
Reference: https://www.sqlshack.com/implementing-slowly-changing-dimensions-scds-in-data-warehouses/

```

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


```
Your thoughts...
A primary ethical issue identified in this story is that the data sets used to train machine learning are subjective entities created and assembled by a collection of individuals.: data sets are no more objective than the human reasoning and motives used to inform them. And that's the crux. Machine learning modelling relies on human input, reflecting an incomplete and flawed understanding of what is presented as an objective and accurate depiction of what is being modelled. Complexity abounds and increases at the intersection of technology and society, especially as people construct more complex models across domains, whether visual representations of physical items or linguistic categorizations of human communication. As complexity increases and people compete to improve the datasets used to train machine learning models, the subjective elements central to machine learning modelling can go underrecognized —or, worse, ignored —leading to problematic content. Article author Vicki Boykis illustrates this ethical issue by describing the mislabeling of "the person subtree of ImageNet," where, in 2019, most of the dataset was disabled to remedy offensive image labelling. In short, data set creation for machine learning shows that, ethically, the reach of academic work (and corporate interests) can exceed its grasp of knowledge mobilization for the betterment of technology and society.
```
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.
Loading