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 modified 01_slides/slides_03.pdf
Binary file not shown.
Binary file modified 01_slides/slides_04.pdf
Binary file not shown.
Binary file modified 01_slides/slides_05.pdf
Binary file not shown.
Binary file modified 01_slides/slides_06.pdf
Binary file not shown.
3 changes: 3 additions & 0 deletions 03_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)

I established a logical relationship between the vendor,booth and ventor_booth_assignments I will be pasting the image below

-
62 changes: 61 additions & 1 deletion 03_homework/homework_2.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
--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 TOP 10 from customer ORDER BY customer_last_name,customer_first_name


--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 BETWEEN 4 AND 9

-- option 2
SELECT * from customer_purchases WHERE product_id >= 4 AND 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:
Expand All @@ -22,21 +26,77 @@ filtered by vendor IDs between 8 and 10 (inclusive) using either:
*/
-- option 1


SELECT
customer_id,
product_id,
quantity,
cost_to_customer_per_qty,
(quantity * cost_to_customer_per_qty) AS price
FROM
purchases
WHERE
vendor_id >= 8 AND vendor_id <= 10;

-- option 2

SELECT
customer_id,
product_id,
quantity,
cost_to_customer_per_qty,
(quantity * cost_to_customer_per_qty) AS price
FROM
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
products;

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


--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,
v.vendor_name,
vba.market_date
FROM
vendor v
INNER JOIN
vendor_booth_assignments vba ON v.vendor_id = vba.vendor_id
ORDER BY
v.vendor_name,
vba.market_date;
45 changes: 45 additions & 0 deletions 03_homework/homework_3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,42 @@
/* 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. */

Here are the SQL queries for each of the tasks described:
Aggregate

Counting Vendor Booth Assignments per Vendor ID:

sql

SELECT
vendor_id,
COUNT(*) AS booth_rentals
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
c.customer_id,
c.first_name,
c.last_name,
SUM(p.quantity * p.cost_to_customer_per_qty) AS total_spent
FROM
customers c
JOIN
customer_purchases p ON c.customer_id = p.customer_id
GROUP BY
c.customer_id, c.first_name, c.last_name
HAVING
total_spent > 2000
ORDER BY
c.last_name, c.first_name;


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

CREATE TABLE temp.new_vendor AS
SELECT * FROM vendor;

INSERT INTO temp.new_vendor (vendor_id, vendor_name, vendor_type, owner_name, store_type)
VALUES (1231321, 'Thomass Superfood Store', 'Fresh Focused', 'Thomas Rosenthal', 'Fresh Focused');


-- 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', purchase_date) AS purchase_month,
STRFTIME('%Y', purchase_date) AS purchase_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!! */


125 changes: 124 additions & 1 deletion 03_homework/homework_4.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,17 @@ SELECT
product_name || ', ' || product_size|| ' (' || product_qty_type || ')'
FROM product





But wait! The product table has some bad data (a few NULL values).
Find the NULLs and then using COALESCE, replace the NULL with a
blank for the first problem, and 'unit' for the second problem.




HINT: keep the syntax the same, but edited the correct components with the string.
The `||` values concatenate the columns into strings.
Edit the appropriate columns -- you're making two edits -- and the NULL rows will be fixed.
Expand All @@ -25,16 +32,132 @@ All the other rows will remain the same.) */
visits to the farmer’s market (labeling each market date with a different number).
Each customer’s first visit is labeled 1, second visit is labeled 2, etc.



You can either display all rows in the customer_purchases table, with the counter changing on
each new market date for each customer, or select only the unique market dates per customer
(without purchase details) and number those visits.
HINT: One of these approaches uses ROW_NUMBER() and one uses DENSE_RANK(). */

SELECT
product_name || ', ' ||
COALESCE(product_size, '') || ' (' ||
COALESCE(product_qty_type, 'unit') || ')' AS product_details
FROM
product;

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


WITH customer_visit AS (
SELECT
customer_id,
market_date,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY market_date DESC) AS reverse_visit_number
FROM
customer_purchases
)

SELECT
customer_id,
market_date
FROM
customer_ visit
WHERE
reverse_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;



-- String manipulations
/* 1. Some product names in the product table have descriptions like "Jar" or "Organic".
These are separated from the product name with a hyphen.
Create a column using SUBSTR (and a couple of other commands) that captures these, but is otherwise NULL.
Remove any trailing or leading whitespaces. Don't just use a case statement for each product!

| product_name | description |
|----------------------------|-------------|
| Habanero Peppers - Organic | Organic |

Hint: you might need to use INSTR(product_name,'-') to find the hyphens. INSTR will help split the column. */

SELECT
product_name,
TRIM(SUBSTR(product_name, INSTR(product_name, '-') + 1)) AS description
FROM
product
WHERE
INSTR(product_name, '-') > 0;

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

SELECT
product_name,
product_size
FROM
product
WHERE
product_size REGEXP '[0-9]';

-- UNION
/* 1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales.

HINT: There are a possibly a few ways to do this query, but if you're struggling, try the following:
1) Create a CTE/Temp Table to find sales values grouped dates;

2) Create another CTE/Temp table with a rank windowed function on the previous query to create
"best day" and "worst day";
3) Query the second temp table twice, once for the best day, once for the worst day,
with a UNION binding them. */


WITH sales_by_date AS (
SELECT
market_date,
SUM(quantity * cost_to_customer_per_qty) AS total_sales
FROM
customer_purchases
GROUP BY
market_date
),


ranked_sales AS (
SELECT
market_date,
total_sales,
RANK() OVER (ORDER BY total_sales DESC) AS sales_rank_desc,
RANK() OVER (ORDER BY total_sales ASC) AS sales_rank_asc
FROM
sales_by_date
)

SELECT
market_date,
total_sales
FROM
ranked_sales
WHERE
sales_rank_desc = 1

UNION

SELECT
market_date,
total_sales
FROM
ranked_sales
WHERE
sales_rank_asc = 1;
Loading