0% found this document useful (0 votes)
46 views

SQL-03 - Joins

The document discusses using SQL joins to combine data from multiple tables. It provides examples of queries that: 1) Mark vendors as selling fresh produce or other goods and list vendor details. 2) Bin customer purchases by price range and include purchase details. 3) List product names and their categories by joining product and category tables. 4) Find customers who have not made purchases by left joining to the purchases table.

Uploaded by

Arun Jith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

SQL-03 - Joins

The document discusses using SQL joins to combine data from multiple tables. It provides examples of queries that: 1) Mark vendors as selling fresh produce or other goods and list vendor details. 2) Bin customer purchases by price range and include purchase details. 3) List product names and their categories by joining product and category tables. 4) Find customers who have not made purchases by left joining to the purchases table.

Uploaded by

Arun Jith
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

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

With table aliasing:

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.

SELECT c.* # select columns from customer table only


FROM customer AS c
LEFT JOIN customer_purchases AS cp
ON c.customer_id = cp.customer_id
WHERE cp.customer_id IS NULL
Question: Let’s say we want to write a query that returns a list of all
customers who did not make a purchase on March 2, 2019.
Question: Let’s say we want to write a query that returns a list of all
customers who did not make a purchase on March 2, 2019.

Two problems with the output:

SELECT c.*, cp.market_date


1. Some rows/ customers are missing
FROM customer AS c
because the market_date is NULL.
LEFT JOIN customer_purchases AS cp
2. We are getting multiple rows for
ON c.customer_id = cp.customer_id
each customer which is not
WHERE cp.market_date <> '2019-03-02'
required.

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

You might also like