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 not shown.
Binary file not shown.
76 changes: 65 additions & 11 deletions 02_activities/assignments/Cohort_8/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,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*
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. */

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


/*2. Write a query that returns all customer purchases and a new calculated column 'price' (quantity * cost_to_customer_per_qty),
Expand All @@ -23,38 +29,61 @@ filtered by customer IDs between 8 and 10 (inclusive) using either:
2. one condition using BETWEEN
*/
-- option 1

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

-- option 2

SELECT *, (quantity*cost_to_customer_per_qty) AS price
FROM customer_purchases
WHERE product_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 product_qty_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_condensed,
CASE WHEN 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;

/* SECTION 3 */

-- AGGREGATE
/* 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(vendor_id) AS booth_rental_count
FROM vendor_booth_assignments
GROUP BY vendor_id;



Expand All @@ -63,7 +92,22 @@ sticker to everyone who has ever spent more than $2000 at the market. Write a qu
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
customer.customer_id,
customer.customer_first_name,
customer.customer_last_name,
SUM(customer_purchases.quantity * customer_purchases.cost_to_customer_per_qty) AS total_spent
FROM customer
INNER JOIN customer_purchases
ON customer.customer_id = customer_purchases.customer_id
GROUP BY
customer.customer_id,
customer.customer_first_name,
customer.customer_last_name
HAVING SUM(customer_purchases.quantity * customer_purchases.cost_to_customer_per_qty) > 2000
ORDER BY
customer.customer_last_name,
customer.customer_first_name;


--Temp Table
Expand All @@ -77,7 +121,17 @@ 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)
*/

CREATE TABLE temp.new_vendor AS
SELECT *
FROM vendor;
INSERT INTO temp.new_vendor
VALUES (
10,
'Thomas Superfood Store',
'Fresh Focused',
'Thomas',
'Rosenthal'
);


-- Date
Expand Down
140 changes: 134 additions & 6 deletions 02_activities/assignments/Cohort_8/assignment2.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* ASSIGNMENT 2 */
/* SECTION 2 */
/* SECTION 2 */

-- COALESCE
/* 1. Our favourite manager wants a detailed long list of products, but is afraid of tables!
Expand All @@ -21,7 +21,16 @@ 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 *
--identifying NULL
FROM product
WHERE product_name IS NULL
OR product_size IS NULL
OR product_qty_type IS NULL;
--COALESCE and concatenate
SELECT
COALESCE(product_name, '')|| ', ' || COALESCE(product_size, '')|| ' (' || COALESCE(product_qty_type, 'unit') || ')'
FROM product


--Windowed Functions
Expand All @@ -33,17 +42,44 @@ You can either display all rows in the customer_purchases table, with the counte
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*

FROM(
SELECT
customer_id,
market_date,
dense_rank() OVER(PARTITION BY customer_id ORDER BY market_date) as number_visits
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
customer_id,
market_date

FROM(
SELECT
customer_id,
market_date,
ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY market_date DESC) as number_visits
FROM customer_purchases
)
WHERE number_visits = 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,
vendor_id,
market_date,
COUNT(*) OVER(PARTITION BY customer_id, product_id) as customer_purchase_count
FROM customer_purchases



Expand All @@ -59,11 +95,22 @@ 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,
TRIM(
SUBSTR(
product_name,
INSTR(product_name, '-') + 1
)
) AS product_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
/* 1. Using a UNION, write a query that displays the market dates with the highest and lowest total sales.
Expand All @@ -75,6 +122,41 @@ 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 date_sales AS (
-- Create a CTE/temp table to find sales values grouped dates
SELECT
market_date,
SUM(quantity * cost_to_customer_per_qty) AS total_sales
FROM customer_purchases
GROUP BY market_date
),
ranked_dates AS (
/*Create anocther CTE/Temp table with a rank windowed function on the previous QUERY
to create BEST DAY and WORST DAY*/

SELECT
market_date,
total_sales,
RANK() OVER (ORDER BY total_sales DESC) AS best_rank,
RANK() OVER (ORDER BY total_sales ASC) AS worst_rank
FROM date_sales
)
--QUERY the second temp table twice, once for the BEST DAY,once for the WORST DAY, with a UNION
SELECT
market_date,
total_sales,
'BEST DAY' AS label
FROM ranked_dates
WHERE best_rank = 1

UNION

SELECT
market_date,
total_sales,
'WORST DAY' AS label
FROM ranked_dates
WHERE worst_rank = 1;



Expand All @@ -91,7 +173,37 @@ 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 distinct_vendor_products AS (

SELECT DISTINCT
vendor_id,
product_id,
original_price
FROM vendor_inventory
),
vendor_product_customer AS (

SELECT
dvp.vendor_id AS vendor_id,
dvp.product_id AS product_id,
dvp.original_price AS product_price,
cust.customer_id AS customer_id
FROM distinct_vendor_products dvp
CROSS JOIN customer cust
)
SELECT
ven.vendor_name,
prod.product_name,
SUM(5 * vpc.product_price) AS money_made_per_product
FROM vendor_product_customer vpc
JOIN vendor ven ON vpc.vendor_id = ven.vendor_id
JOIN product prod ON vpc.product_id = prod.product_id
GROUP BY
ven.vendor_name,
prod.product_name
ORDER BY
ven.vendor_name,
prod.product_name;

-- INSERT
/*1. Create a new table "product_units".
Expand All @@ -104,14 +216,30 @@ Name the timestamp column `snapshot_timestamp`. */
/*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). */


CREATE TABLE product_units AS
SELECT
product_id,
product_name,
product_size,
product_category_id,
product_qty_type,
CURRENT_TIMESTAMP AS snapshot_timestamp
FROM product
WHERE product_qty_type = 'unit';

-- 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_units2
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The table product_units2 does not exist. You previously created product_units.

--ROWID because all the rows shared the same snapshot_timestamp, realized this after delete all the rows without using ROWID
WHERE ROWID = (
SELECT ROWID
FROM product_units2
ORDER BY snapshot_timestamp ASC
LIMIT 1
);

-- UPDATE
/* 1.We want to add the current_quantity to the product_units table.
Expand Down
28 changes: 28 additions & 0 deletions 05_src/sql/SELECT.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* MODULE 2 */
/* SELECT */


/* 1. Select everything in the customer table */
SELECT *FROM customer;

/* 2. Use sql as a calculator */



/* 3. Add order by and limit clauses */
SELECT *FROM customer
ORDER BY customer_last_name DESC--ZA
LIMIT 5;--ONLY 5 ROWS


/* 4. Select multiple specific columns */
SELECT product_name,
product_size,
product_qty_type
FROM product;

/* 5. Add a static value in a column */
SELECT '2025' AS [this_year],
vendor_name,
vendor_type
FROM vendor;