Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homeworks #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions 03_homework/homework_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ 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)

![Fredy Rincon's Logical Data Model](image.png)
3 changes: 3 additions & 0 deletions 03_homework/homework_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@

# 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`.

[Link to Fredy's homework_2.sql](homework_2.sql)
[Link to Fredy's homework_2.txt](homework_2.txt)
65 changes: 65 additions & 0 deletions 03_homework/homework_2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- SELECT 1: Query to return everything in the customer table:

SELECT * FROM customer;

-- SELECT 2: Query to display all ten columns and ten rows from the customer table, sorted by customer_last_name and then customer_first_name:

SELECT *
FROM customer
ORDER BY customer_last_name, customer_first_name
LIMIT 10;


-- WHERE 1: Query to return all customer purchases of product IDs 4 and 9:

SELECT *
FROM customer_purchases
WHERE product_id IN (4, 9);


-- WHERE 2a: Query to return all customer purchases and a new calculated column 'price', filtered by vendor IDs between 8 and 10 using AND:

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


-- WHERE 2b: Query to return all customer purchases and a new calculated column 'price', filtered by vendor IDs between 8 and 10 using BETWEEN:

SELECT *, (quantity * cost_to_customer_per_qty) AS price
FROM customer_purchases
WHERE vendor_id BETWEEN 8 AND 10;


-- CASE 1: Query to output product_id, product_name, and add a column called prod_qty_type_condensed:

SELECT product_id, product_name,
CASE
WHEN product_qty_type = 'unit' THEN 'unit'
ELSE 'bulk'
END AS prod_qty_type_condensed
FROM product;


-- CASE 2: Query to add a column called pepper_flag:

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: Query to INNER JOIN the vendor table to the vendor_booth_assignments table on the vendor_id field and sort by vendor_name, then market_date:

SELECT *
FROM vendor
INNER JOIN vendor_booth_assignments
ON vendor.vendor_id = vendor_booth_assignments.vendor_id
ORDER BY vendor.vendor_name, vendor_booth_assignments.market_date;

65 changes: 65 additions & 0 deletions 03_homework/homework_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
-- SELECT 1: Query to return everything in the customer table:

SELECT * FROM customer;

-- SELECT 2: Query to display all ten columns and ten rows from the customer table, sorted by customer_last_name and then customer_first_name:

SELECT *
FROM customer
ORDER BY customer_last_name, customer_first_name
LIMIT 10;


-- WHERE 1: Query to return all customer purchases of product IDs 4 and 9:

SELECT *
FROM customer_purchases
WHERE product_id IN (4, 9);


-- WHERE 2a: Query to return all customer purchases and a new calculated column 'price', filtered by vendor IDs between 8 and 10 using AND:

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


-- WHERE 2b: Query to return all customer purchases and a new calculated column 'price', filtered by vendor IDs between 8 and 10 using BETWEEN:

SELECT *, (quantity * cost_to_customer_per_qty) AS price
FROM customer_purchases
WHERE vendor_id BETWEEN 8 AND 10;


-- CASE 1: Query to output product_id, product_name, and add a column called prod_qty_type_condensed:

SELECT product_id, product_name,
CASE
WHEN product_qty_type = 'unit' THEN 'unit'
ELSE 'bulk'
END AS prod_qty_type_condensed
FROM product;


-- CASE 2: Query to add a column called pepper_flag:

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: Query to INNER JOIN the vendor table to the vendor_booth_assignments table on the vendor_id field and sort by vendor_name, then market_date:

SELECT *
FROM vendor
INNER JOIN vendor_booth_assignments
ON vendor.vendor_id = vendor_booth_assignments.vendor_id
ORDER BY vendor.vendor_name, vendor_booth_assignments.market_date;

2 changes: 2 additions & 0 deletions 03_homework/homework_3.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ To insert the new row use VALUES, specifying the value you want for each column:
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!!

[Fredy's SQL Homework_3](homework_3.sql)

33 changes: 33 additions & 0 deletions 03_homework/homework_3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--Aggregate Query: Counting Vendor Booth Assignments
SELECT vendor_id, COUNT(*) AS booth_assignment_count
FROM vendor_booth_assignments
GROUP BY vendor_id;

--Aggregate Query: Customers Spending More Than $2000
SELECT c.customer_id, c.customer_first_name, c.customer_last_name, SUM(cp.quantity * cp.cost_to_customer_per_qty) AS total_spent
FROM customer c
JOIN customer_purchases cp ON c.customer_id = cp.customer_id
GROUP BY c.customer_id, c.customer_first_name, c.customer_last_name
HAVING total_spent > 2000
ORDER BY c.customer_last_name, c.customer_first_name;

--Temp Table: Insert New Vendor
CREATE TEMPORARY TABLE new_vendor AS
SELECT *
FROM vendor;

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

--Date Query: Extract Month and Year
SELECT customer_id, strftime('%m', market_date) AS month, strftime('%Y', market_date) AS year
FROM customer_purchases;

--Date Query: Customer Spending in April 2019
SELECT customer_id, SUM(quantity * cost_to_customer_per_qty) AS total_spent
FROM customer_purchases
WHERE strftime('%m', market_date) = '04' AND strftime('%Y', market_date) = '2019'
GROUP BY customer_id;
--FREDY RINCON
--uoft-dsi-c3-homework_3-20240517
--uoft-dsi-c3-sql_homework_3-20240517
Binary file added 03_homework/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.