subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger...
Transcript of subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger...
![Page 1: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/1.jpg)
CSE 344JANUARY 17TH – SUBQUERIES
![Page 2: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/2.jpg)
GROUPING AND AGGREGATIONPurchase(product, price, quantity)
SELECT product, Sum(quantity) AS TotalSalesFROM PurchaseWHERE price > 1GROUP BY product
How is this query processed?
Find total quantities for all sales over $1, by product.
![Page 3: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/3.jpg)
GROUPING AND AGGREGATIONPurchase(product, price, quantity)
SELECT product, Sum(quantity) AS TotalSalesFROM PurchaseWHERE price > 1GROUP BY product
Find total quantities for all sales over $1, by product.
SELECT product, Sum(quantity) AS TotalSalesFROM PurchaseGROUP BY product
Do these queries return the same number of rows? Why?
![Page 4: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/4.jpg)
GROUPING AND AGGREGATIONPurchase(product, price, quantity)
SELECT product, Sum(quantity) AS TotalSalesFROM PurchaseWHERE price > 1GROUP BY product
Find total quantities for all sales over $1, by product.
SELECT product, Sum(quantity) AS TotalSalesFROM PurchaseGROUP BY product
Do these queries return the same number of rows? Why?
Empty groups are removed, hencefirst query may return fewer groups
![Page 5: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/5.jpg)
GROUPING AND AGGREGATION
CSE 344 - 2017au 5
1. Compute the FROM and WHERE clauses.
2. Group by the attributes in the GROUPBY
3. Compute the SELECT clause:grouped attributes and aggregates.
FWGSTM
![Page 6: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/6.jpg)
1,2: FROM, WHERE
6
SELECT product, Sum(quantity) AS TotalSalesFROM PurchaseWHERE price > 1GROUP BY product
Product Price QuantityBagel 3 20Bagel 1.50 20
Banana 0.5 50Banana 2 10Banana 4 10
FWGS
WHEREprice>1
![Page 7: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/7.jpg)
3,4. GROUPING, SELECT
7
SELECT product, Sum(quantity) AS TotalSalesFROM PurchaseWHERE price > 1GROUP BY product
Product TotalSalesBagel 40
Banana 20
Product Price QuantityBagel 3 20Bagel 1.50 20
Banana 0.5 50Banana 2 10Banana 4 10
FWGS
![Page 8: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/8.jpg)
ORDERING RESULTS
CSE 344 - 2017au 8
SELECT product, sum(price*quantity) as revFROM PurchaseGROUP BY productORDER BY rev desc
FWGOS
Purchase(pid, product, price, quantity, month)
Note: some SQL engineswant you to say ORDER BY sum(price*quantity) desc
TM
![Page 9: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/9.jpg)
HAVING CLAUSE
CSE 344 - 2017au 9
SELECT product, sum(price*quantity)FROM PurchaseWHERE price > 1GROUP BY productHAVING sum(quantity) > 30
Same query as before, except that we consider only productsthat had at least 30 sales.
HAVING clause contains conditions on aggregates.
Purchase(pid, product, price, quantity, month)
![Page 10: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/10.jpg)
GENERAL FORM OF GROUPINGAND AGGREGATION
S = may contain attributes a1,…,ak and/or any aggregates but NO OTHER ATTRIBUTES
C1 = is any condition on the attributes in R1,…,Rn
C2 = is any condition on aggregate expressionsand on attributes a1,…,ak
CSE 344 - 2017au 10
Why ?
SELECT SFROM R1,…,RnWHERE C1GROUP BY a1,…,akHAVING C2
![Page 11: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/11.jpg)
SEMANTICS OF SQL WITH GROUP-BY
CSE 344 - 2017au 11
Evaluation steps:1. Evaluate FROM-WHERE using Nested Loop Semantics2. Group by the attributes a1,…,ak
3. Apply condition C2 to each group (may have aggregates)4. Compute aggregates in S and return the result
SELECT SFROM R1,…,RnWHERE C1GROUP BY a1,…,akHAVING C2
FWGHOS
![Page 12: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/12.jpg)
EXERCISE
CSE 344 - 2017au 12
Compute the total income per monthShow only months with less than 10 items soldOrder by quantity sold and display as “TotalSold”
Purchase(pid, product, price, quantity, month)
![Page 13: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/13.jpg)
EXERCISE
CSE 344 - 2017au 13
FROM Purchase
Compute the total income per monthShow only months with less than 10 items soldOrder by quantity sold and display as “TotalSold”
Purchase(pid, product, price, quantity, month)
![Page 14: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/14.jpg)
EXERCISE
CSE 344 - 2017au 14
Compute the total income per monthShow only months with less than 10 items soldOrder by quantity sold and display as “TotalSold”
FROM PurchaseGROUP BY month
Purchase(pid, product, price, quantity, month)
![Page 15: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/15.jpg)
EXERCISE
CSE 344 - 2017au 15
Compute the total income per monthShow only months with less than 10 items soldOrder by quantity sold and display as “TotalSold”
FROM PurchaseGROUP BY monthHAVING sum(quantity) < 10
Purchase(pid, product, price, quantity, month)
![Page 16: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/16.jpg)
EXERCISE
CSE 344 - 2017au 16
Compute the total income per monthShow only months with less than 10 items soldOrder by quantity sold and display as “TotalSold”
SELECT month, sum(price*quantity), sum(quantity) as TotalSold
FROM PurchaseGROUP BY monthHAVING sum(quantity) < 10
Purchase(pid, product, price, quantity, month)
![Page 17: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/17.jpg)
EXERCISE
CSE 344 - 2017au 17
Compute the total income per monthShow only months with less than 10 items soldOrder by quantity sold and display as “TotalSold”
SELECT month, sum(price*quantity), sum(quantity) as TotalSold
FROM PurchaseGROUP BY monthHAVING sum(quantity) < 10ORDER BY sum(quantity)
Purchase(pid, product, price, quantity, month)
![Page 18: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/18.jpg)
WHERE VS HAVING
WHERE condition is applied to individual rows• The rows may or may not contribute to the aggregate• No aggregates allowed here• Occasionally, some groups become empty and are
removed
HAVING condition is applied to the entire group• Entire group is returned, or removed• May use aggregate functions on the group
CSE 344 - 2017au 18
![Page 19: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/19.jpg)
MYSTERY QUERY
19
SELECT month, sum(quantity), max(price)FROM PurchaseGROUP BY month
What do they compute?
SELECT month, sum(quantity)FROM PurchaseGROUP BY month
SELECT monthFROM PurchaseGROUP BY month
Purchase(pid, product, price, quantity, month)
![Page 20: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/20.jpg)
MYSTERY QUERY
20
SELECT month, sum(quantity), max(price)FROM PurchaseGROUP BY month
What do they compute?
SELECT month, sum(quantity)FROM PurchaseGROUP BY month
SELECT monthFROM PurchaseGROUP BY month
Lesson:DISTINCT isa special caseof GROUP BY
Purchase(pid, product, price, quantity, month)
![Page 21: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/21.jpg)
AGGREGATE + JOIN
Product(pid,pname,manufacturer)Purchase(id,product_id,price,month)
For each manufacturer, compute how many products with price > $100 they sold
![Page 22: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/22.jpg)
AGGREGATE + JOIN
Product(pid,pname,manufacturer)Purchase(id,product_id,price,month)
For each manufacturer, compute how many products with price > $100 they sold
Problem: manufacturer is in Purchase, price is in Product...
![Page 23: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/23.jpg)
AGGREGATE + JOIN
Product(pid,pname,manufacturer)Purchase(id,product_id,price,month)
For each manufacturer, compute how many products with price > $100 they sold
Problem: manufacturer is in Purchase, price is in Product...
-- step 1: think about their joinSELECT ...FROM Product x, Purchase yWHERE x.pid = y.product_idand y.price > 100
manufacturer ... price ...
Hitachi 150
Canon 300
Hitachi 180
![Page 24: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/24.jpg)
AGGREGATE + JOIN
-- step 2: do the group-by on the joinSELECT x.manufacturer, count(*)FROM Product x, Purchase yWHERE x.pid = y.product_idand y.price > 100
GROUP BY x.manufacturer
Product(pid,pname,manufacturer)Purchase(id,product_id,price,month)
manufacturer count(*)
Hitachi 2
Canon 1
...
For each manufacturer, compute how many products with price > $100 they sold
Problem: manufacturer is in Purchase, price is in Product...
-- step 1: think about their joinSELECT ...FROM Product x, Purchase yWHERE x.pid = y.product_idand y.price > 100
manufacturer ... price ...
Hitachi 150
Canon 300
Hitachi 180
![Page 25: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/25.jpg)
AGGREGATE + JOIN
SELECT x.manufacturer, y.month, count(*)FROM Product x, Purchase yWHERE x.pid = y.product_idand y.price > 100
GROUP BY x.manufacturer, y.month
Product(pid,pname,manufacturer)Purchase(id,product_id,price,month)
manufacturer month count(*)
Hitachi Jan 2
Hitachi Feb 1
Canon Jan 3
...
Variant:For each manufacturer, compute how many products with price > $100 they sold in each month
![Page 26: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/26.jpg)
INCLUDING EMPTY GROUPSIn the result of a group by query, there is one row per group in the result
CSE 344 - 2017au 26
SELECT x.manufacturer, count(*)FROM Product x, Purchase yWHERE x.pname = y.productGROUP BY x.manufacturer
Count(*) is never 0
FWGHOS
![Page 27: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/27.jpg)
INCLUDING EMPTY GROUPS
CSE 344 - 2017au 27
SELECT x.manufacturer, count(y.pid)FROM Product x LEFT OUTER JOIN Purchase yON x.pname = y.productGROUP BY x.manufacturer
Count(pid) is 0when all pid’s in
the group areNULL
![Page 28: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/28.jpg)
SUBQUERIES
A subquery is a SQL query nested inside a larger querySuch inner-outer queries are called nested queriesA subquery may occur in:
• A SELECT clause• A FROM clause• A WHERE clause
Rule of thumb: avoid nested queries when possible• But sometimes it’s impossible, as we will see
CSE 344 - 2017au 28
![Page 29: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/29.jpg)
SUBQUERIES…Can return a single value to be included in a SELECT clauseCan return a relation to be included in the FROMclause, aliased using a tuple variableCan return a single value to be compared with another value in a WHERE clauseCan return a relation to be used in the WHERE or HAVING clause under an existential quantifier
CSE 344 - 2017au 29
![Page 30: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/30.jpg)
1. SUBQUERIES IN SELECT
CSE 344 - 2017au 30
Product (pname, price, cid)Company (cid, cname, city)
For each product return the city where it is manufactured
SELECT X.pname, (SELECT Y.cityFROM Company YWHERE Y.cid=X.cid) as City
FROM Product X
What happens if the subquery returns more than one city?
We get a runtime error(and SQLite simply ignores the extra values…)
“correlated subquery”
![Page 31: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/31.jpg)
1. SUBQUERIES IN SELECT
CSE 344 - 2017au 31
Whenever possible, don’t use a nested queries:
SELECT X.pname, Y.cityFROM Product X, Company YWHERE X.cid=Y.cid
=
SELECT X.pname, (SELECT Y.cityFROM Company YWHERE Y.cid=X.cid) as City
FROM Product X
Product (pname, price, cid)Company (cid, cname, city)
We have “unnested”the query
![Page 32: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/32.jpg)
1. SUBQUERIES IN SELECT
CSE 344 - 2017au 32
Compute the number of products made by each company
SELECT DISTINCT C.cname, (SELECT count(*)FROM Product P WHERE P.cid=C.cid)
FROM Company C
Product (pname, price, cid)Company (cid, cname, city)
![Page 33: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/33.jpg)
1. SUBQUERIES IN SELECT
CSE 344 - 2017au 33
Compute the number of products made by each company
SELECT DISTINCT C.cname, (SELECT count(*) FROM Product P WHERE P.cid=C.cid)
FROM Company C
Better: we can unnest using a GROUP BY
SELECT C.cname, count(*)FROM Company C, Product PWHERE C.cid=P.cidGROUP BY C.cname
Product (pname, price, cid)Company (cid, cname, city)
![Page 34: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/34.jpg)
1. SUBQUERIES IN SELECT
CSE 344 - 2017au 34
But are these really equivalent?
SELECT DISTINCT C.cname, (SELECT count(*) FROM Product P WHERE P.cid=C.cid)
FROM Company C
SELECT C.cname, count(*)FROM Company C, Product PWHERE C.cid=P.cidGROUP BY C.cname
Product (pname, price, cid)Company (cid, cname, city)
![Page 35: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/35.jpg)
1. SUBQUERIES IN SELECT
CSE 344 - 2017au 35
But are these really equivalent?
SELECT DISTINCT C.cname, (SELECT count(*) FROM Product P WHERE P.cid=C.cid)
FROM Company C
No! Different results if a company has no products
SELECT C.cname, count(*)FROM Company C, Product PWHERE C.cid=P.cidGROUP BY C.cname
SELECT C.cname, count(pname)FROM Company C LEFT OUTER JOIN Product PON C.cid=P.cidGROUP BY C.cname
Product (pname, price, cid)Company (cid, cname, city)
![Page 36: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/36.jpg)
2. SUBQUERIES IN FROM
CSE 344 - 2017au 36
Find all products whose prices is > 20 and < 500
SELECT X.pnameFROM (SELECT *
FROM Product AS Y WHERE price > 20) as X
WHERE X.price < 500
Product (pname, price, cid)Company (cid, cname, city)
![Page 37: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/37.jpg)
2. SUBQUERIES IN FROM
CSE 344 - 2017au 37
Find all products whose prices is > 20 and < 500
SELECT X.pnameFROM (SELECT *
FROM Product AS Y WHERE price > 20) as X
WHERE X.price < 500
Try unnest this query !
Product (pname, price, cid)Company (cid, cname, city)
![Page 38: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/38.jpg)
2. SUBQUERIES IN FROM
CSE 344 - 2017au 38
Find all products whose prices is > 20 and < 500
SELECT X.pnameFROM (SELECT *
FROM Product AS Y WHERE price > 20) as X
WHERE X.price < 500
Try unnest this query !
Product (pname, price, cid)Company (cid, cname, city)
Side note: This is not a correlated subquery. (why?)
![Page 39: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/39.jpg)
2. SUBQUERIES IN FROM
Sometimes we need to compute an intermediate table only to use it later in a SELECT-FROM-WHEREOption 1: use a subquery in the FROM clauseOption 2: use the WITH clause
CSE 344 - 2017au 39
![Page 40: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/40.jpg)
2. SUBQUERIES IN FROM
CSE 344 - 2017au 40
SELECT X.pnameFROM (SELECT *
FROM Product AS Y WHERE price > 20) as X
WHERE X.price < 500
Product (pname, price, cid)Company (cid, cname, city)
=WITH myTable AS (SELECT * FROM Product AS Y WHERE price > 20)SELECT X.pnameFROM myTable as XWHERE X.price < 500
A subquery whoseresult we called myTable
![Page 41: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/41.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 41
Find all companies that make some products with price < 200
Product (pname, price, cid)Company (cid, cname, city)
![Page 42: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/42.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 42
Find all companies that make some products with price < 200
Existential quantifiers
Product (pname, price, cid)Company (cid, cname, city)
![Page 43: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/43.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 43
Find all companies that make some products with price < 200
SELECT DISTINCT C.cnameFROM Company CWHERE EXISTS (SELECT *
FROM Product PWHERE C.cid = P.cid and P.price < 200)
Existential quantifiers
Using EXISTS:
Product (pname, price, cid)Company (cid, cname, city)
![Page 44: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/44.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 44
SELECT DISTINCT C.cnameFROM Company CWHERE C.cid IN (SELECT P.cid
FROM Product PWHERE P.price < 200)
Using IN
Find all companies that make some products with price < 200
Existential quantifiers
Product (pname, price, cid)Company (cid, cname, city)
![Page 45: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/45.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 45
SELECT DISTINCT C.cnameFROM Company CWHERE 200 > ANY (SELECT price
FROM Product PWHERE P.cid = C.cid)
Using ANY:
Find all companies that make some products with price < 200
Existential quantifiers
Product (pname, price, cid)Company (cid, cname, city)
![Page 46: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/46.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 46
SELECT DISTINCT C.cnameFROM Company CWHERE 200 > ANY (SELECT price
FROM Product PWHERE P.cid = C.cid)
Using ANY:
Find all companies that make some products with price < 200
Existential quantifiers
Product (pname, price, cid)Company (cid, cname, city)
Not supported in sqlite
![Page 47: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/47.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 47
SELECT DISTINCT C.cnameFROM Company C, Product PWHERE C.cid = P.cid and P.price < 200
Now let’s unnest it:
Find all companies that make some products with price < 200
Existential quantifiers
Product (pname, price, cid)Company (cid, cname, city)
![Page 48: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/48.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 48
SELECT DISTINCT C.cnameFROM Company C, Product PWHERE C.cid = P.cid and P.price < 200
Existential quantifiers are easy! J
Now let’s unnest it:
Find all companies that make some products with price < 200
Existential quantifiers
Product (pname, price, cid)Company (cid, cname, city)
![Page 49: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/49.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 49
same as:
Product (pname, price, cid)Company (cid, cname, city)
Find all companies that make only products with price < 200
Find all companies s.t. all their products have price < 200
![Page 50: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/50.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 50
same as:
Universal quantifiers
Product (pname, price, cid)Company (cid, cname, city)
Find all companies that make only products with price < 200
Find all companies s.t. all their products have price < 200
![Page 51: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/51.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 51
Universal quantifiers are hard! L
same as:
Universal quantifiers
Product (pname, price, cid)Company (cid, cname, city)
Find all companies that make only products with price < 200
Find all companies s.t. all their products have price < 200
![Page 52: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/52.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 52
1. Find the other companies that make some product ≥ 200
SELECT DISTINCT C.cnameFROM Company CWHERE C.cid IN (SELECT P.cid
FROM Product PWHERE P.price >= 200)
Product (pname, price, cid)Company (cid, cname, city)
Find all companies s.t. all their products have price < 200
![Page 53: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/53.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 53
2. Find all companies s.t. all their products have price < 200
1. Find the other companies that make some product ≥ 200
SELECT DISTINCT C.cnameFROM Company CWHERE C.cid IN (SELECT P.cid
FROM Product PWHERE P.price >= 200)
Product (pname, price, cid)Company (cid, cname, city)
Find all companies s.t. all their products have price < 200
SELECT DISTINCT C.cnameFROM Company CWHERE C.cid NOT IN (SELECT P.cid
FROM Product PWHERE P.price >= 200)
![Page 54: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/54.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 54
SELECT DISTINCT C.cnameFROM Company CWHERE NOT EXISTS (SELECT *
FROM Product PWHERE P.cid = C.cid and P.price >= 200)
Using EXISTS:
Universal quantifiers
Product (pname, price, cid)Company (cid, cname, city)
Find all companies s.t. all their products have price < 200
![Page 55: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/55.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 55
SELECT DISTINCT C.cnameFROM Company CWHERE 200 >= ALL (SELECT price
FROM Product PWHERE P.cid = C.cid)
Using ALL:
Universal quantifiers
Product (pname, price, cid)Company (cid, cname, city)
Find all companies s.t. all their products have price < 200
![Page 56: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/56.jpg)
3. SUBQUERIES IN WHERE
CSE 344 - 2017au 56
SELECT DISTINCT C.cnameFROM Company CWHERE 200 >= ALL (SELECT price
FROM Product PWHERE P.cid = C.cid)
Using ALL:
Universal quantifiers
Product (pname, price, cid)Company (cid, cname, city)
Find all companies s.t. all their products have price < 200
Not supported in sqlite
![Page 57: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/57.jpg)
QUESTION FOR DATABASE THEORY FANSAND THEIR FRIENDS
Can we unnest the universal quantifierquery?
We need to first discuss the concept of monotonicity
CSE 344 - 2017au 57
![Page 58: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/58.jpg)
MONOTONE QUERIESDefinition A query Q is monotone if:
• Whenever we add tuples to one or more input tables, the answer to the query will not lose any of the tuples
CSE 344 - 2017au 58
Product (pname, price, cid)Company (cid, cname, city)
![Page 59: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/59.jpg)
MONOTONE QUERIESDefinition A query Q is monotone if:
• Whenever we add tuples to one or more input tables, the answer to the query will not lose any of the tuples
CSE 344 - 2017au 59
pname price cid
Gizmo 19.99 c001
Gadget 999.99 c004
Camera 149.99 c003
Product (pname, price, cid)Company (cid, cname, city)
cid cname city
c002 Sunworks Bonn
c001 DB Inc. Lyon
c003 Builder Lodtz
Product Company
Q pname city
Gizmo Lyon
Camera Lodtz
![Page 60: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/60.jpg)
MONOTONE QUERIESDefinition A query Q is monotone if:
• Whenever we add tuples to one or more input tables, the answer to the query will not lose any of the tuples
pname price cid
Gizmo 19.99 c001
Gadget 999.99 c004
Camera 149.99 c003
Product (pname, price, cid)Company (cid, cname, city)
pname price cid
Gizmo 19.99 c001
Gadget 999.99 c004
Camera 149.99 c003
iPad 499.99 c001
cid cname city
c002 Sunworks Bonn
c001 DB Inc. Lyon
c003 Builder Lodtz
Product Companypname city
Gizmo Lyon
Camera Lodtz
pname city
Gizmo Lyon
Camera Lodtz
iPad Lyon
Product Company
Q
Qcid cname city
c002 Sunworks Bonn
c001 DB Inc. Lyon
c003 Builder Lodtz
So far it looks monotone...
![Page 61: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/61.jpg)
MONOTONE QUERIESDefinition A query Q is monotone if:
• Whenever we add tuples to one or more input tables, the answer to the query will not lose any of the tuples
pname price cid
Gizmo 19.99 c001
Gadget 999.99 c004
Camera 149.99 c003
Product (pname, price, cid)Company (cid, cname, city)
pname price cid
Gizmo 19.99 c001
Gadget 999.99 c004
Camera 149.99 c003
iPad 499.99 c001
cid cname city
c002 Sunworks Bonn
c001 DB Inc. Lyon
c003 Builder Lodtz
Product Companypname city
Gizmo Lyon
Camera Lodtz
pname city
Gizmo Lodtz
Camera Lodtz
iPad Lyon
Product Company
Q
Qcid cname city
c002 Sunworks Bonn
c001 DB Inc. Lyon
c003 Builder Lodtz
c004 Crafter Lodtz
Q is not monotone!
![Page 62: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/62.jpg)
MONOTONE QUERIESTheorem: If Q is a SELECT-FROM-WHERE query that does not have subqueries, and no aggregates, then it is monotone.
CSE 344 - 2017au 62
![Page 63: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/63.jpg)
MONOTONE QUERIESTheorem: If Q is a SELECT-FROM-WHERE query that does not have subqueries, and no aggregates, then it is monotone.
Proof. We use the nested loop semantics: if we insert a tuple in a relation Ri, this will not remove any tuples from the answer
CSE 344 - 2017au 63
SELECT a1, a2, …, akFROM R1 AS x1, R2 AS x2, …, Rn AS xnWHERE Conditions
for x1 in R1 dofor x2 in R2 do
…for xn in Rn doif Conditionsoutput (a1,…,ak)
![Page 64: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/64.jpg)
MONOTONE QUERIESThe query:
is not monotone
64
Find all companies s.t. all their products have price < 200
Product (pname, price, cid)Company (cid, cname, city)
![Page 65: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/65.jpg)
MONOTONE QUERIESThe query:
is not monotone
65
Find all companies s.t. all their products have price < 200
pname price cid
Gizmo 19.99 c001
cid cname city
c001 Sunworks Bonn
cname
Sunworks
Product (pname, price, cid)Company (cid, cname, city)
![Page 66: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/66.jpg)
MONOTONE QUERIESThe query:
is not monotone
Consequence: If a query is not monotonic, then we cannot write it as a SELECT-FROM-WHERE query without nested subqueries
66
Find all companies s.t. all their products have price < 200
pname price cid
Gizmo 19.99 c001
cid cname city
c001 Sunworks Bonn
cname
Sunworks
pname price cid
Gizmo 19.99 c001
Gadget 999.99 c001
cid cname city
c001 Sunworks Bonn
cname
Product (pname, price, cid)Company (cid, cname, city)
![Page 67: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/67.jpg)
QUERIES THAT MUST BE NESTEDQueries with universal quantifiers or with negation
CSE 344 - 2017au 67
![Page 68: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/68.jpg)
QUERIES THAT MUST BE NESTEDQueries with universal quantifiers or with negation
Queries that use aggregates in certain ways
• sum(..) and count(*) are NOT monotone, because they do not satisfy set containment
• select count(*) from R is not monotone!
CSE 344 - 2017au 68
![Page 69: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/69.jpg)
INTRODUCTION TO DATA MANAGEMENTCSE 344
LECTURE 7-8: SQL WRAP-UP
RELATIONAL ALGEBRA
CSE 344 - 2017au 69
![Page 70: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/70.jpg)
ANNOUNCEMENTSYou received invitation email to @csYou will be prompted to choose passwd
• Problems with existing account?• In the worst case we will ask you to create a new @outlook
account just for this classIf OK, create the database server
• Choose cheapest pricing tier!Remember: WQ2 due on Friday
70
![Page 71: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/71.jpg)
GROUP BY V.S. NESTED QUERIES
CSE 344 - 2017au 71
SELECT product, Sum(quantity) AS TotalSalesFROM PurchaseWHERE price > 1GROUP BY product
SELECT DISTINCT x.product, (SELECT Sum(y.quantity)FROM Purchase yWHERE x.product = y.productAND y.price > 1)
AS TotalSalesFROM Purchase xWHERE x.price > 1
Why twice ?
Purchase(pid, product, quantity, price)
![Page 72: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/72.jpg)
MORE UNNESTING
CSE 344 - 2017au 72
Author(login,name)Wrote(login,url)
Find authors who wrote ≥ 10 documents:
![Page 73: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/73.jpg)
MORE UNNESTING
CSE 344 - 2017au 73
SELECT DISTINCT Author.nameFROM AuthorWHERE (SELECT count(Wrote.url)
FROM WroteWHERE Author.login=Wrote.login)
>= 10
This isSQL bya novice
Attempt 1: with nested queries
Author(login,name)Wrote(login,url)
Find authors who wrote ≥ 10 documents:
![Page 74: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/74.jpg)
MORE UNNESTING
CSE 344 - 2017au 74
Attempt 1: with nested queries
Author(login,name)Wrote(login,url)
Find authors who wrote ≥ 10 documents:
SELECT Author.nameFROM Author, WroteWHERE Author.login=Wrote.loginGROUP BY Author.nameHAVING count(wrote.url) >= 10
This isSQL by
an expert
Attempt 2: using GROUP BY and HAVING
![Page 75: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/75.jpg)
FINDING WITNESSES
CSE 344 - 2017au 75
Product (pname, price, cid)Company (cid, cname, city)
For each city, find the most expensive product made in that city
![Page 76: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/76.jpg)
FINDING WITNESSES
CSE 344 - 2017au 76
SELECT x.city, max(y.price)FROM Company x, Product yWHERE x.cid = y.cidGROUP BY x.city;
Finding the maximum price is easy…
But we need the witnesses, i.e., the products with max price
For each city, find the most expensive product made in that city
Product (pname, price, cid)Company (cid, cname, city)
![Page 77: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/77.jpg)
FINDING WITNESSES
CSE 344 - 2017au 77
To find the witnesses, compute the maximum pricein a subquery (in FROM or in WITH)
Product (pname, price, cid)Company (cid, cname, city)
WITH CityMax AS (SELECT x.city, max(y.price) as maxpriceFROM Company x, Product yWHERE x.cid = y.cidGROUP BY x.city)
SELECT DISTINCT u.city, v.pname, v.priceFROM Company u, Product v, CityMax wWHERE u.cid = v.cid
and u.city = w.cityand v.price = w.maxprice;
![Page 78: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/78.jpg)
FINDING WITNESSES
CSE 344 - 2017au 78
To find the witnesses, compute the maximum pricein a subquery (in FROM or in WITH)
SELECT DISTINCT u.city, v.pname, v.priceFROM Company u, Product v,
(SELECT x.city, max(y.price) as maxpriceFROM Company x, Product yWHERE x.cid = y.cidGROUP BY x.city) w
WHERE u.cid = v.cidand u.city = w.cityand v.price = w.maxprice;
Product (pname, price, cid)Company (cid, cname, city)
![Page 79: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/79.jpg)
FINDING WITNESSES
CSE 344 - 2017au 79
Or we can use a subquery in where clause
SELECT u.city, v.pname, v.priceFROM Company u, Product vWHERE u.cid = v.cidand v.price >= ALL (SELECT y.price
FROM Company x, Product y WHERE u.city=x.cityand x.cid=y.cid);
Product (pname, price, cid)Company (cid, cname, city)
![Page 80: subqueries - University of Washington · SUBQUERIES A subqueryis a SQL query nested inside a larger query Such inner-outer queries are called nested queries A subquerymay occur in:](https://reader033.fdocuments.in/reader033/viewer/2022051914/60061d108ccb2665240d6633/html5/thumbnails/80.jpg)
FINDING WITNESSES
CSE 344 - 2017au 80
There is a more concise solution here:
SELECT u.city, v.pname, v.priceFROM Company u, Product v, Company x, Product yWHERE u.cid = v.cid and u.city = x.cityand x.cid = y.cidGROUP BY u.city, v.pname, v.priceHAVING v.price = max(y.price)
Product (pname, price, cid)Company (cid, cname, city)