SQL-03 - Joins
SQL-03 - Joins
Lecture Queries
Question: Get the report of all the vendors who primarily sell
fresh produce and who don’t and mark it in front of their
names.
Question: Get the report of all the vendors who primarily sell
fresh produce and who don’t and mark it in front of their
names.
SELECT
vendor_id,
vendor_name,
vendor_type,
CASE
WHEN LOWER(vendor_type) LIKE '%fresh%'
THEN 'Fresh Produce'
ELSE 'Other'
END AS vendor_type_condensed
FROM farmers_market.vendor
Question: Put the total cost to customer purchases into bins of
● under $5.00,
● $5.00–$9.99,
● $10.00–$19.99, or
● $20.00 and over.
Question: Put the total cost to customer purchases into bins of
● under $5.00,
● $5.00–$9.99, SELECT
market_date,
● $10.00–$19.99, or
customer_id,
● $20.00 and over. vendor_id,
ROUND(quantity * cost_to_customer_per_qty, 2) AS price,
CASE
WHEN quantity * cost_to_customer_per_qty < 5.00
THEN 'Under $5'
WHEN quantity * cost_to_customer_per_qty < 10.00
THEN '$5-$9.99'
WHEN quantity * cost_to_customer_per_qty < 20.00
THEN '$10-$19.99'
WHEN quantity * cost_to_customer_per_qty >= 20.00
THEN '$20 and Up'
END AS price_bin
FROM farmers_market.customer_purchases
LIMIT 10
Question: Let’s say we wanted to list each product name along with its
product category name.
Question: Let’s say we wanted to list each product name along with its
product category name.
SELECT * FROM
product
LEFT JOIN product_category
ON product.product_category_id = product_category.product_category_id
SELECT
p.product_id,
p.product_name,
pc.product_category_id,
pc.product_category_name
FROM product AS p
LEFT JOIN product_category AS pc
ON p.product_category_id = pc.product_category_id
Question: Get all the Customers who have not purchased anything from the market
yet.
Question: Get all the Customers who have not purchased anything from the market
yet.
solution
SELECT DISTINCT c.*
FROM customer AS c
LEFT JOIN customer_purchases AS cp
ON c.customer_id = cp.customer_id
WHERE (cp.market_date <> '2019-03-02' OR cp.market_date IS NULL)
Question: Let’s say we want details about all booths, as well as
every vendor booth assignment along with the vendor details.
Question: Let’s say we want details about all farmer’s market
booths, as well as every vendor booth assignment for every market
date.
SELECT
b.booth_number,
b.booth_type,
vba.market_date,
v.vendor_id,
v.vendor_name,
v.vendor_type
FROM booth AS b
LEFT JOIN vendor_booth_assignments AS vba ON b.booth_number = vba.
booth_number
LEFT JOIN vendor AS v ON v.vendor_id = vba.vendor_id
ORDER BY b.booth_number, vba.market_date