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
186 changes: 186 additions & 0 deletions 02_activities/Assignment1_Sandbox.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/* 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

/* 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_first_name,
customer_last_name,
market_date,
transaction_time
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


SUM(quantity*cost_to_customer_per_qty) AS purchase_total
, customer_id
GROUP BY 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)`
*/

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




1 change: 1 addition & 0 deletions 02_activities/assignments/DC_Cohort/Assignment1.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,5 @@ 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.
```
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
107 changes: 88 additions & 19 deletions 02_activities/assignments/DC_Cohort/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,113 @@

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


SELECT customer_id, customer_first_name, customer_last_name, customer_postal_code
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 customer_id, customer_last_name, customer_first_name, customer_postal_code
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 IN (4,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
*/
-- option 1

-- option 1
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 = 8 OR customer_id = 9
AND customer_id = 9 OR customer_id = 10;

-- option 2


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

/* SECTION 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
GROUP BY cp.customer_id
HAVING purchase_total > 2000
ORDER BY customer_last_name, customer_first_name;

--Temp Table
/* 1. Insert the original vendor table into a temp.new_vendor and then add a 10th vendor:
Expand All @@ -77,7 +123,30 @@ 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)
*/
-- 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');


-- Date
Expand Down
29 changes: 29 additions & 0 deletions 04_this_cohort/live_code/DC/module_2/MOD2_SQL2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* MODULE 2 */
/* SELECT */


/* 1. Select everything in the customer table */
SELECT *
FROM customer;

/* 2. Use sql as a calculator */
SELECT 1 + 1 as somethingelse, 10*5 as somethingmore;


/* 3. Add order by and limit clauses */
SELECT *
From customer
ORDER BY customer_first_name
LIMIT 10;


/* 4. Select multiple specific columns */
SELECT customer_id, customer_first_name
FROM customer;



/* 5. Add a static value in a column */
SELECT 2025 as this_year, 'October' as this_month, customer_id
FROM customer;

Loading