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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
118 changes: 106 additions & 12 deletions 02_activities/assignments/Cohort_8/assignment1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,62 @@
/* SECTION 2 */


--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. */
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 product_id,
vendor_id,
market_date,
customer_id,
quantity,
cost_to_customer_per_qty,
transaction_time
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),
filtered by customer IDs between 8 and 10 (inclusive) using either:
1. two conditions using AND
2. one condition using BETWEEN
*/
-- option 1

select product_id,
vendor_id,
market_date,
customer_id,
quantity,
cost_to_customer_per_qty,
transaction_time,
(quantity * cost_to_customer_per_qty) as price
from customer_purchases
where customer_id in ('8','10')

-- option 2

select product_id,
vendor_id,
market_date,
customer_id,
quantity,
cost_to_customer_per_qty,
transaction_time,
(quantity * cost_to_customer_per_qty) as price
from customer_purchases
where customer_id between '8'and '10'


--CASE
Expand All @@ -35,18 +66,32 @@ Using the product table, write a query that outputs the product_id and product_n
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 product_name like '%Peppers%' 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 market_date,v.* from vendor v
inner join vendor_booth_assignments vba on v.vendor_id=vba.vendor_id
order by vendor_name,market_date ASC



Expand All @@ -56,7 +101,12 @@ vendor_id field they both have in common, and sorts the result by vendor_name, t
/* 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(booth_number) as count_of_rent
from vendor_booth_assignments
group by vendor_id
order by count_of_rent DESC

/* 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
Expand All @@ -65,6 +115,18 @@ 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_first_name,
customer_last_name,
c.customer_id,
(quantity * cost_to_customer_per_qty) as total
from customer c
inner join customer_purchases cp on c.customer_id=cp.customer_id
group by
customer_first_name,
customer_last_name,
c.customer_id
having (quantity * cost_to_customer_per_qty) > '2000'

--Temp Table
/* 1. Insert the original vendor table into a temp.new_vendor and then add a 10th vendor:
Expand All @@ -86,11 +148,43 @@ 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
distinct
customer_id,
product_name,
strftime('%m', market_date) as mon,---- this did not produced the desired result hence the substring used
substr(market_date,6,2) as month,
substr(market_date,1,4) as year,
strftime('%Y', market_date) as year2
from customer_purchases a
inner join product b on a.product_id=b.product_id

/* 2. Using the previous query as a base, determine how much money each customer spent in April 2022.
Remember that money spent is quantity*cost_to_customer_per_qty.

Remember that money spent is quantity*cost_to_customer_per_qty. */

with base as (select
customer_first_name,
customer_last_name,
c.customer_id,
substr(market_date,6,2) as mon,
market_date,
(quantity * cost_to_customer_per_qty) as total
from customer c
inner join customer_purchases cp on c.customer_id=cp.customer_id
group by
customer_first_name,
customer_last_name,
market_date,
c.customer_id)
select customer_first_name,
customer_last_name,
customer_id,
sum(total)
from base
where strftime('%m', market_date) ='04'
group by customer_last_name,
customer_id
HINTS: you will need to AGGREGATE, GROUP BY, and filter...
but remember, STRFTIME returns a STRING for your WHERE statement!! */