SQL Subqueries
SQL Subqueries
Basic - Intermediate
Announcements
• HW2 and WQ2 released
– Both due next Tuesday
Need to be Careful…
SELECT product, Product Price Quantity
max(quantity)
FROM Purchase Bagel 3 20
GROUP BY product Bagel 1.50 20
SELECT product, quantity Banana 0.5 50
FROM Purchase
Banana 2 10
GROUP BY product
-- what does this mean? Banana 4 10
Exercise FWGHOS
Compute the total income per month
Show only months with less than 10 items sold
Order by quantity sold and display as “TotalSold”
Aggregate + Join
For each manufacturer, compute how many products
with price > $100 they sold
Aggregate + Join
For each manufacturer, compute how many products
with price > $100 they sold
Problem: manufacturer is in Product, price is in Purchase...
Aggregate + Join
For each manufacturer, compute how many products
with price > $100 they sold
Problem: manufacturer is in Product, price is in Purchase...
manu
-- step 1: think about their join ... price ...
facturer
SELECT ... Hitachi 150
FROM Product x, Purchase y Canon 300
WHERE x.pid = y.product_id
Hitachi 180
and y.price > 100
Aggregate + Join
For each manufacturer, compute how many products
with price > $100 they sold
Problem: manufacturer is in Product, price is in Purchase...
manu
-- step 1: think about their join ... price ...
facturer
SELECT ... Hitachi 150
FROM Product x, Purchase y Canon 300
WHERE x.pid = y.product_id
Hitachi 180
and y.price > 100
Aggregate + Join
Variant:
For each manufacturer, compute how many products
with price > $100 they sold in each month
Hitachi Jan 2
Hitachi Feb 1
Canon Jan 3
...
CSE 414 - Spring 2018
FWGHOS
FWGHOS
CSE 414 - Spring 2018 23
Subqueries…
• Can return a single value to be included in a
SELECT clause
• Can return a relation to be included in the
FROM clause, aliased using a tuple variable
• Can return a single value to be compared
with another value in a WHERE clause
• Can return a relation to be used in the WHERE
or HAVING clause under an existential
quantifier
CSE 414 - Spring 2018 24
1. Subqueries in SELECT
Product (pname, price, cid)
Company (cid, cname, city)
For each product return the city where it is manufactured
SELECT X.pname, (SELECT Y.city “correlated
FROM Company Y subquery”
WHERE Y.cid=X.cid) as City
FROM Product X
1. Subqueries in SELECT
Whenever possible, don’t use a nested queries:
1. Subqueries in SELECT
Compute the number of products made by each company
1. Subqueries in SELECT
Compute the number of products made by each company
1. Subqueries in SELECT
But are these really equivalent?
SELECT DISTINCT C.cname, (SELECT count(*)
FROM Product P
WHERE P.cid=C.cid)
FROM Company C
1. Subqueries in SELECT
But are these really equivalent?
SELECT DISTINCT C.cname, (SELECT count(*)
FROM Product P
WHERE P.cid=C.cid)
FROM Company C
2. Subqueries in FROM
SELECT X.pname
FROM (SELECT *
FROM Product AS Y
WHERE price > 20) as X
WHERE X.price < 500
2. Subqueries in FROM
SELECT X.pname
FROM (SELECT *
FROM Product AS Y
WHERE price > 20) as X
WHERE X.price < 500
2. Subqueries in FROM
SELECT X.pname
FROM (SELECT *
FROM Product AS Y
Side note: This is not a
WHERE price > 20) as X correlated subquery. (why?)
WHERE X.price < 500
2. Subqueries in FROM
SELECT X.pname
FROM (SELECT *
FROM Product AS Y
WHERE price > 20) as X
WHERE X.price < 500
A subquery whose
=
result we called myTable