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
2 changes: 2 additions & 0 deletions 02_activities/homework/homework_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ Please do not pick the exact same tables that I have already diagramed. For exam
- ![01_farmers_market_conceptual_model.png](./images/01_farmers_market_conceptual_model.png)
- 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)

- ![homework_1.png](./images/homework_1.png)

53 changes: 51 additions & 2 deletions 02_activities/homework/homework_2.sql
Original file line number Diff line number Diff line change
@@ -1,42 +1,91 @@
--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. */
-- option 1
SELECT *
FROM customer_purchases
WHERE product_id in (4, 9)

-- option 2
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 vendor IDs between 8 and 10 (inclusive) using either:
1. two conditions using AND
2. one condition using BETWEEN
*/
-- option 1
SELECT *
,SUM (quantity * cost_to_customer_per_qty) as price

FROM customer_purchases as cp
WHERE vendor_id BETWEEN 8 AND 10

-- option 2
SELECT *
,SUM (quantity * cost_to_customer_per_qty) as price

FROM customer_purchases as cp
WHERE vendor_id>7
AND vendor_id <11

--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 product_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 product_qty_type_condensed

,CASE WHEN product_name like '%pepper%' or '%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 as v
INNER JOIN vendor_booth_assignments as vba
on v.vendor_id = vba.vendor_id
GROUP by vendor_name, market_date

52 changes: 52 additions & 0 deletions 02_activities/homework/homework_3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,33 @@
/* 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 (vba.booth_number)
, v.vendor_id
,v.vendor_name

FROM vendor_booth_assignments as vba
inner JOIN vendor as v on v.vendor_id = vba.vendor_id
GROUP by v.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
c.customer_last_name
,c.customer_first_name
,cp.customer_id
,sum (cp.quantity * cp.cost_to_customer_per_qty) as total_spent


FROM customer_purchases as cp
INNER JOIN customer as c on c.customer_id=cp.customer_id
GROUP by customer_last_name, customer_first_name

HAVING total_spent>2000


--Temp Table
Expand All @@ -23,18 +42,51 @@ 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)
*/
DROP TABLE IF EXISTS new_vendor;

CREATE TEMP TABLE new_vendor AS
SELECT *
from vendor;


INSERT INTO new_vendor
VALUES (10, 'Thomass Superfood Store', 'Fresh Focused' , 'Thomas', 'Rosenthal');

SELECT * FROM new_vendor


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

SELECT
customer_id
, strftime ('%m', market_date) as month
,strftime ('%Y', market_date) as year


FROM customer_purchases




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

SELECT *
, strftime ('%m', market_date) as month
,strftime ('%Y', market_date) as year
,sum (quantity * cost_to_customer_per_qty) as sales

FROM customer_purchases

GROUP by year, month
HAVING year = '2022'
AND month = '04'


34 changes: 33 additions & 1 deletion 02_activities/homework/homework_4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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') || ')'
FROM product


--Windowed Functions
Expand All @@ -30,16 +32,39 @@ 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(). */

SELECT *
--,row_number () OVER (PARTITION by customer_id ORDER by market_date ASC) as customer_visit
, dense_rank() OVER (PARTITION by customer_id ORDER by market_date ASC) as customer_visit_denserank
FROM customer_purchases

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

SELECT *

FROM (
SELECT
customer_id
,market_date
,vendor_id
,transaction_time
,row_number () OVER (PARTITION by customer_id ORDER by market_date DESC) as customer_visit

FROM customer_purchases
) x

WHERE x.customer_visit = 1

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


SELECT *
,count() OVER (PARTITION by customer_id ORDER by product_id DESC) as times_purchased

FROM customer_purchases



-- String manipulations
Expand All @@ -53,6 +78,13 @@ Remove any trailing or leading whitespaces. Don't just use a case statement for
| Habanero Peppers - Organic | Organic |

Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR will help split the column. */
SELECT *
,CASE WHEN INSTR(product_name,'-') > 0
THEN LTRIM(RTRIM(SUBSTR(product_name,INSTR(product_name,'-')+1)))
ELSE NULL END AS description

FROM product




Expand Down
8 changes: 8 additions & 0 deletions 02_activities/homework/homework_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@
<br>

**Write**: Reflect on your previous work and how you would adjust to include ethics and inequity components. Total length should be a few paragraphs, no more than one page.

Most databses are built around social and cultural norms as much they are built around effective data systems. Data is holds more power than we perceive, as data leaks and infringements have been some of the more newsworthy incidents in the computational world int he recent years. So information privacy needs to be built into large datasets at multiple levels. For example, sensitive information like hospital records need to be accessible to anyone controlling and inputting data as well as those making decisions based on that data. However, it needs to have enough security checks built in such that the people who have free access to that data cannot abuse their privileges. A fellow colleague's medical information is just as accessible as any other patients' data, and if that information comes to light, there is no guarantee that bias towards a collleague based on medical history can be avoided. Where we can, access to data must be limited by levels. For example colleagues' records might be restricted, and for higher level decision making anonymised data maybe sufficient.

Databases must also be built in a way that accommodates some room for flexibility in structure in the future. For example, Pakistan's national database (NADRA) was not futureproofed by including more than two genders, non-nuclear families, single parents or orphans. While we are unable to anticipate all the changes in the future, change is guaranteed, so smart database designs must always account for guaranteed circumstances. As social norms evolve, people may identify differently, choose to have families outside of legal marriage or not share last names with their partners or children, and data entry needs to accommodate for such evolving climates. While changing the structure of a ginourmous database like NADRA might be incredibly labourious, it is doing a great disservice to the people of their country by not including certain groups of people who do not conform to what was previously considered the norm.

Data and AI should work for everyone, not just a privileged group of people with money, status, citizenship, or decision-making power. Most decision making in the western world has been traditionally led by white men, with no room for women, gender-diverse people, other races or ethnicities, but their decisions affect the aforementioned minorities and more, usually negatively. The most streamlined method to avoid exclusion to include a diverse group of people in the research,designing and deploying phases. It is normal and somewhat expeceted to have our thinking and design influenced by our lives and backgrounds, so including as many diverse groups as possible will account for flexibility and ensuring that our data systems are inclusive for everyone.

Ethics, AI and data are a dice-game of money, power, and privilege, much like most other things in life. Therefore it is upto those with the money, power, and privilege to make decisions that will not marginalise disenfranchised groups down the line with a tool that likely benefited from their labour to become successful.
Binary file added 02_activities/homework/images/homework_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading