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
3 changes: 3 additions & 0 deletions 02_activities/homework/homework_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,6 @@ 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](https://github.com/user-attachments/assets/42ca86e2-7cbc-41d0-9ad9-702393dce43f)

48 changes: 46 additions & 2 deletions 02_activities/homework/homework_2.sql
Original file line number Diff line number Diff line change
@@ -1,42 +1,86 @@
--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 = 4;
```

-- option 2

```
SELECT * FROM customer_purchases WHERE product_id = 10;
```

/*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 (quantity * cost_to_customer_per_qty) AS price, * FROM customer_purchases WHERE vendor_id > 7 AND vendor_id < 11;
```

-- option 2

```
SELECT (quantity * cost_to_customer_per_qty) AS price, * FROM customer_purchases WHERE vendor_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 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
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_count,
* 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 INNER JOIN vendor_booth_assignments
WHERE vendor.vendor_id = vendor_booth_assignments.vendor_id
ORDER BY vendor_name, market_date;
```
36 changes: 35 additions & 1 deletion 02_activities/homework/homework_3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@
/* 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(*) as total_visits, *
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 SUM(cost_to_customer_per_qty * quantity) AS total_spent, *
FROM customer INNER JOIN customer_purchases
WHERE customer.customer_id = customer_purchases.customer_id
GROUP BY customer.customer_id
HAVING total_spent > 2000
ORDER BY customer_last_name, customer_first_name;
```


--Temp Table
Expand All @@ -24,6 +36,17 @@ When inserting the new vendor, you need to appropriately align the columns to be
VALUES(col1,col2,col3,col4,col5)
*/

```
CREATE TEMP TABLE temp.new_vendor AS
SELECT *
FROM vendor;
```

```
INSERT INTO temp.new_vendor
VALUES (10, "Thomass Superfood Store", "a Fresh Focused store", "Thomas", "Rosenthal")
```



-- Date
Expand All @@ -32,9 +55,20 @@ 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! */

```
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 2019.
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 SUM(cost_to_customer_per_qty * quantity) AS total_spent, strftime('%m', market_date) AS month, strftime('%Y', market_date) AS year, *
FROM customer_purchases
WHERE year = "2019" AND month = "04"
GROUP BY customer_id;
```
52 changes: 50 additions & 2 deletions 02_activities/homework/homework_4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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.) */


```
SELECT
product_name || ', ' || COALESCE(product_size, '') || ' (' || COALESCE(product_qty_type, 'unit') || ')'
FROM product
```


--Windowed Functions
Expand All @@ -30,15 +34,32 @@ 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 customer_id, market_date, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date) AS visit_number
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, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS visit_number
FROM customer_purchases
)
WHERE visit_number = 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 customer_id, product_id, market_date, COUNT(*) OVER (PARTITION BY customer_id, product_id) AS purchase_count
FROM customer_purchases;
```



Expand All @@ -54,10 +75,21 @@ 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. */

```
SELECT product_name, CASE WHEN INSTR(product_name, '-') > 0 THEN SUBSTR(product_name, INSTR(product_name, '-') + 1) ELSE NULL
END AS description
FROM product;
```


/* 2. Filter the query to show any product_size value that contain a number with REGEXP. */

```
SELECT product_name, CASE WHEN INSTR(product_name, '-') > 0 THEN SUBSTR(product_name, INSTR(product_name, '-') + 1) ELSE NULL
END AS description
FROM product
WHERE product_size REGEXP '[0-9]';
```


-- UNION
Expand All @@ -70,6 +102,22 @@ 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. */


```
WITH total_sales AS (
SELECT market_date, SUM(cost_to_customer_per_qty * quantity) AS total_sold
FROM customer_purchases
GROUP BY market_date
),
best_sales AS (
SELECT market_date, total_sold, ROW_NUMBER() OVER (ORDER BY total_sold DESC) AS sale_rating
FROM total_sales
),
worst_sales AS (
SELECT market_date, total_sold, ROW_NUMBER() OVER (ORDER BY total_sold ASC) AS sale_rating
FROM total_sales
)

SELECT * from best_sales WHERE sale_rating = 1 UNION ALL SELECT * from worst_sales WHERE sale_rating = 1
```


22 changes: 19 additions & 3 deletions 02_activities/homework/homework_5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,30 @@ 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`. */


```
CREATE TABLE product_units AS
SELECT *, CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_qty_type = 'unit';
```

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


```
INSERT INTO product_units
VALUES (9000, 'Apple Pie', '1 pie', 'unit', 10, CURRENT_TIMESTAMP);
```

-- DELETE
/* 1. Delete the older record for the whatever product you added.

HINT: If you don't specify a WHERE clause, you are going to have a bad time.*/


```
DELETE FROM product_units
WHERE product_name = 'Apple Pie'
```

-- UPDATE
/* 1.We want to add the current_quantity to the product_units table.
Expand All @@ -48,4 +59,9 @@ 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. */

```
ALTER TABLE product_units
ADD current_quantity INT;
```


4 changes: 4 additions & 0 deletions 02_activities/homework/homework_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@
<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.

I think that ethics and inequality intersect with data when it involves people. If we were managing user's personal data, we could then discuss what types of analysis about those users would be appropriate. What types of biases are we introducing into the results simply based on our own preconceived notions of the world and how accurate are our decisions based on those results? Also, are we asking the right questions when working with data? We might have blind spots about the kind of questions we need to be asking. For example, there was an article about falsehoods that people believe with regards to names. For example, the falsehoods that all people have a last name, people’s names do not contain numbers, all names use the latin character set, etc., yet we have seen databases that make assumptions about the way people’s names ought to be structured. When a person is unable to sign up for a service, because they do not have a last name, then that is a form of inequality.

We could also discuss the management of data. For example, who should be able to access a user's data, and how can we secure it from malicious people? There is also the discussion around data retention. We should be aware that data and personally identifiable information should only be stored for as long as it is required. Ethically, we should be caring for people’s data as if it was our own. These are all topics we could dive further into and explore the ways in which we can organize our databases and tables to better align with the need for ethics and equality.