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
Binary file added 03_homework/homework_1_answer.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 33 additions & 5 deletions 03_homework/homework_2.sql
Original file line number Diff line number Diff line change
@@ -1,42 +1,70 @@
--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 *, (quantity * cost_to_customer_per_qty) AS price
FROM customer_purchases
WHERE vendor_id >= 8 AND vendor_id <= 10

-- 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 product_id, product_name,
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 product_id, product_name,
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_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 v INNER JOIN vendor_booth_assignments vba
ON v.vendor_id = vba.vendor_id
ORDER BY v.vendor_name, vba.market_date
27 changes: 27 additions & 0 deletions 03_homework/homework_3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
/* 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 vendor_id, COUNT(*) as rent_count
FROM vendor_booth_assignments
GROUP by vendor_id


/* 2. The Farmer’s Market Customer Appreciation Committee wants to give a bumper
Expand All @@ -10,6 +13,11 @@ 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.*, sum(quantity * cost_to_customer_per_qty) as spent
FROM customer c INNER JOIN customer_purchases cp ON c.customer_id = cp.customer_id
GROUP by cp.customer_id
HAVING sum(cp.quantity * cp.cost_to_customer_per_qty) > 2000
ORDER by c.customer_first_name, c.customer_last_name


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

CREATE TEMPORARY TABLE temp.new_vendor AS SELECT * FROM vendor

INSERT INTO temp.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')

SELECT * FROM temp.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 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 customer_id,
quantity * cost_to_customer_per_qty as spent
FROM customer_purchases
WHERE strftime('%Y%m', market_date) = '201904'
GROUP by customer_id



70 changes: 67 additions & 3 deletions 03_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') || ')' AS Product_Detail
FROM product


--Windowed Functions
Expand All @@ -30,17 +32,49 @@ 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
FROM
(SELECT DISTINCT customer_id, market_date FROM customer_purchases) cp


/* 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) AS visit
FROM
(SELECT DISTINCT customer_id, market_date FROM customer_purchases) cp
) v
WHERE v.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 DISTINCT
customer_id,
product_id,
count(*) OVER (PARTITION BY customer_id, product_id) AS purchases_times
FROM
customer_purchases cp


--Or, I prefer the following but the count function is not used as window function
SELECT
customer_id,
product_id,
count(*) AS purchases_times
FROM
customer_purchases cp
GROUP by
customer_id,
product_id


-- String manipulations
/* 1. Some product names in the product table have descriptions like "Jar" or "Organic".
Expand All @@ -54,10 +88,20 @@ 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,
nullif(trim(substr(product_name, INSTR(product_name,'-') + 1)), product_name) AS description
FROM
product


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

SELECT *
FROM
product
WHERE
product_size REGEXP '[0-9]'


-- UNION
Expand All @@ -71,5 +115,25 @@ HINT: There are a possibly a few ways to do this query, but if you're struggling
with a UNION binding them. */



WITH daily_sales AS (
SELECT
market_date,
SUM(quantity * cost_to_customer_per_qty) AS daily_sales
FROM
customer_purchases
GROUP BY
market_date
)
SELECT
market_date,
max(daily_sales) AS daily_sales,
'Best Day' AS comment
FROM
daily_sales
UNION
SELECT
market_date,
min(daily_sales) AS daily_sales,
'Worst Day' AS comment
FROM daily_sales

52 changes: 49 additions & 3 deletions 03_homework/homework_5.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,62 @@ 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). */



WITH vendor_product AS (
SELECT
DISTINCT vendor_id, product_id, original_price
FROM
vendor_inventory
)

SELECT
v.vendor_name, p.product_name,
vp.original_price * 5 * (SELECT count(*) FROM customer) AS total_sales
FROM
vendor_product vp JOIN vendor v on vp.vendor_id = v.vendor_id
JOIN product p ON vp.product_id = p.product_id
ORDER by v.vendor_name, p.product_name


-- INSERT
/*1. Create a new table "product_units".
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`. */

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

SELECT * FROM product_units

/*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 (
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
snapshot_timestamp
)
SELECT
(SELECT max(product_id) + 1 FROM product),
'Apple Pie', '6"', 3, 'unit', 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_id = 7


-- UPDATE
Expand All @@ -49,3 +85,13 @@ Finally, make sure you have a WHERE statement to update the right row,
When you have all of these components, you can run the update statement. */


UPDATE product_units
SET
current_quantity = COALESCE((SELECT quantity
FROM vendor_inventory vi
WHERE product_units.product_id = vi.product_id
GROUP by vi.product_id
HAVING market_date = max(market_date)
), 0)

SELECT * FROM product_units
6 changes: 6 additions & 0 deletions 03_homework/homework_6.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@
<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.

As a software engineer working in a publication company, I would incorporate ethics into my work by ensuring the accuracy, privacy, and security of the metadata and sales data I handle. I would implement strict protocols to safeguard consumer information and respect data privacy laws such as GDPR. Transparency would be a key principle, informing stakeholders about data usage and obtaining necessary consents. I would also be responsible for ensuring that the data I collect, and share is accurate and unbiased, providing a reliable foundation for business decisions and maintaining the trust of both our company and our customers.

In addressing inequity components, I would focus on identifying and mitigating any biases in the data collection and analysis processes. I would ensure that the metadata and sales data represent a diverse range of books and authors, preventing any unintentional favoritism towards specific genres, authors, or demographics. Additionally, I would analyze the sales data to understand how different groups of consumers are impacted by various factors such as pricing and promotions. I would help create more inclusive and equitable business strategies that promote fairness and diversity in the marketplace.