59
124 L04: SQL

L02-06-SQL - v180129

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: L02-06-SQL - v180129

124

L04:SQL

Page 2: L02-06-SQL - v180129

125

Announcements!

• PollsonPiazza.Openfor2days• Outlinetoday:- practicingmorejoinsandspecifyingkeyandFKconstraints- nestedqueries

• Nexttime:"witnesses"(traditionallystudentsfindthistopicthemostdifficult)

Page 3: L02-06-SQL - v180129

126

QueriesviaSQLhavemultiplewords:Ifyoumasterthisstructureyouknow50%aboutSQLQueries

SELECT …FROM …WHERE …GROUP BY …HAVING …ORDER BY …

• Listofattributes tobeincludedinfinalresult(alsocalledprojection!("*"selectsallattributes)

• Indicatesthetable(s)fromwhichdataistoberetrieved

• Listsacomparisonpredicate,whichrestrictstherowsreturnedbythequery,e.g.“price<20”ordifferentjoin conditions

• Groups rowsthathaveonemorecommonvaluestogetherintoasmallersetofrows

• Acomparisonpredicate usedtorestricttherowsresultingfromtheGROUPBY clause

• Identifieswhichcolumns areusedtosorttheresultingdata,plusthedirection eachcolumnissortedby(ascendingordescending)

Note1 :SQLisgenerallycaseinsensitive,e.g.SELECT

=Select=select

Note2 :Thewordsalwaysappearinthisorder– youCANNOTreorderthem

Page 4: L02-06-SQL - v180129

127

HowtospecifyForeignKeyconstraints

• Supposewehavethefollowingschema:

• Andwewanttoimposethefollowingconstraint:- ‘Onlybonafidestudentsmayenrollincourses’i.e.astudentmustappearinthe

Studentstabletoenrollinaclass

student_id aloneisnotakey- whatis?

sid name gpa

101 Bob 3.2

123 Mary 3.8

student_id cid grade

123 564 A

123 537 A+

Students Enrolled

Wesaythatstudent_id isaforeignkey thatreferstoStudents

Students(sid: string, name: string, gpa: float)Enrolled(student_id: string, cid: string, grade: string)

Page 5: L02-06-SQL - v180129

128

DeclaringPrimaryKeys

CREATE TABLE Students(sid CHAR(20) PRIMARY KEY,name CHAR(20),gpa REAL

)

Students(sid: string, name: string, gpa: float)Enrolled(student_id: string, cid: string, grade: string)

Page 6: L02-06-SQL - v180129

129

DeclaringPrimaryKeys

CREATE TABLE Students(sid CHAR(20),name CHAR(20),gpa REAL,PRIMARY KEY (sid)

)

Students(sid: string, name: string, gpa: float)Enrolled(student_id: string, cid: string, grade: string)

Page 7: L02-06-SQL - v180129

130

DeclaringForeignKeys

CREATE TABLE Enrolled(student_id CHAR(20),cid CHAR(20),grade CHAR(10),

)

Students(sid: string, name: string, gpa: float)Enrolled(student_id: string, cid: string, grade: string)

PRIMARY KEY (student_id, cid),FOREIGN KEY (student_id) REFERENCES Students(sid)

Page 8: L02-06-SQL - v180129

131

AnexampleofSQLsemantics

SELECT R.AFROM R, SWHERE R.A = S.B

A13

B C2 33 43 5

A B C1 2 31 3 41 3 53 2 33 3 43 3 5

CrossProduct

A B C3 3 43 3 5

A33

ApplyProjectionApply

Selections/Conditions

Output

Page 9: L02-06-SQL - v180129

132

NotethesemanticsofajoinSELECT R.AFROM R, SWHERE R.A = S.B

Recall:Crossproduct(AXB)isthesetofalluniquetuplesinA,B

Ex:{a,b,c}X{1,2}={(a,1),(a,2),(b,1),(b,2),(c,1),(c,2)}

=Filtering!

=Returningonlysome attributes

Rememberingthisorderiscriticaltounderstandingtheoutputofcertainqueries(seelateron…)

1. Takecrossproduct:𝑋 = 𝑅×𝑆

2. Applyselections/conditions:𝑌 = 𝑟, 𝑠 ∈ 𝑋 𝑟. 𝐴 = 𝑟. 𝐵}

3. Applyprojections togetfinaloutput:𝑍 = (𝑦. 𝐴, )𝑓𝑜𝑟𝑦 ∈ 𝑌

Page 10: L02-06-SQL - v180129

133

Note:wesay“semantics”not“executionorder”

• Theprecedingslidesshowwhatajoinmeans

• NotactuallyhowtheDBMSexecutesitunderthecovers

Page 11: L02-06-SQL - v180129

134

Practicing more Joins

Page 12: L02-06-SQL - v180129

135

Quiz4

SELECT DISTINCT cNameFROM WHERE

Product (pName, price, category, manufacturer)Company (cName, stockPrice, country)

Q: Find all US companies that manufacture at least twodifferent products.

302

Page 13: L02-06-SQL - v180129

136

Quiz4

Q: Find all US companies that manufacture at least twodifferent products.

Product (pName, price, category, manufacturer)Company (cName, stockPrice, country)

SELECT DISTINCT cNameFROM Product P1, Product P2, CompanyWHERE country = 'USA'

and P1.manufacturer = cNameand P2.manufacturer = cNameand P1.pName <> P2.pName <>

C

P1

P2

302

Page 14: L02-06-SQL - v180129

137

Quiz4PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

... ... ... ...

P1

CompanyCName StockPrice Country

GizmoWorks 25 USA

... ... ...

Cname

GizmoWorks

PName Price Category Manufacturer

... ... ... ...

Powergizmo $29.99 Gadgets GizmoWorks

P2

SELECT DISTINCT cNameFROM Product P1, Product P2, CompanyWHERE country = 'USA'

and P1.manufacturer = cNameand P2.manufacturer = cNameand P1.pName <> P2.pName

<>

302

Page 15: L02-06-SQL - v180129

138

Quiz5

Q: Find all US companies that manufacture a product below $20 and a product above $15.

Product (pName, price, category, manufacturer)Company (cName, stockPrice, country)

SELECT DISTINCT cNameFROM Product as P1, Product as P2, CompanyWHERE country = 'USA'

and P1.price < 20and P2.price > 15and P1.manufacturer = cNameand P2.manufacturer = cName

PName Price Category Manufacturer

Gizmo 19.99 Gadgets GizmoWorks

Powergizmo 29.99 Gadgets GizmoWorks

SingleTouch 149.99 Photography Canon

MultiTouch 203.99 Household Hitachi

Product

CompanyCName StockPrice Country

GizmoWorks 25 USA

Canon 65 Japan

Hitachi 15 Japan

302

Page 16: L02-06-SQL - v180129

139

Quiz5

Q: Find all US companies that manufacture a product below $20 and a product above $15.

Product (pName, price, category, manufacturer)Company (cName, stockPrice, country)

P.price > 15

C

P1

P2 P.price < 20

Note that we did not specify any condition that P1 and P2 need to be distinct. An alternative interpretation is "...and another product above..."

302

Page 17: L02-06-SQL - v180129

140

Quiz5PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

... ... ... ...

P1

CompanyCName StockPrice Country

GizmoWorks 25 USA

... ... ...

Cname

GizmoWorks

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

... ... ... ...

P2

SELECT DISTINCT cNameFROM Product as P1, Product as P2, CompanyWHERE country = 'USA'

and P1.price < 20and P2.price > 15and P1.manufacturer = cNameand P2.manufacturer = cName

302

Page 18: L02-06-SQL - v180129

141

Quiz6 302

?SELECT countryFROM Product, CompanyWHERE manufacturer = cName

and category = 'Gadgets'

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product CompanyCName StockPrice Country

GizmoWorks 25 USA

Canon 65 Japan

Hitachi 15 Japan

Q:Findallcountriesthathavecompaniesthatmanufacturesomeproductinthe‘Gadgets’category!

Page 19: L02-06-SQL - v180129

142

Quiz6

PName Price Category Manufacturer

Gizmo $19.99 Gadgets GizmoWorks

Powergizmo $29.99 Gadgets GizmoWorks

SingleTouch $149.99 Photography Canon

MultiTouch $203.99 Household Hitachi

Product CompanyCName StockPrice Country

GizmoWorks 25 USA

Canon 65 Japan

Hitachi 15 Japan

302

Q:Findallcountriesthathavecompaniesthatmanufacturesomeproductinthe‘Gadgets’category!

SELECT countryFROM Product, CompanyWHERE manufacturer = cName

and category = 'Gadgets'

Country

USA

USA

Joins can introduce duplicates -> remember to use DISTINCT!

Page 20: L02-06-SQL - v180129

143

Nested queries(Subqueries)

Page 21: L02-06-SQL - v180129

144

High-levelnoteonnestedqueries

• WecandonestedqueriesbecauseSQLiscompositional:

- Everything(inputs/outputs)isrepresentedasmultisets- theoutputofonequerycanthusbeusedastheinputtoanother(nesting)!

• Thisisextremelypowerful!

• High-levelidea:subqueriesreturnrelations(yetsometimesjustvalues)

Page 22: L02-06-SQL - v180129

145

Subqueries=Nestedqueries

SELECT ...FROM ...WHERE ...

(SELECT ...FROM ...WHERE ... )

Outerblock

Innerblock

Page 23: L02-06-SQL - v180129

146

Subqueries

important!

• AsubqueryisaSQLquerynestedinsidealargerquery• Suchinner-outerqueriesarecallednestedqueries• Asubquerymayoccurina:- SELECTclause- FROMclause- WHEREclause- HAVINGclause

• Ruleofthumb:avoidwritingnestedquerieswhenpossible;keepinmindthatsometimesit’simpossible

Page 24: L02-06-SQL - v180129

147

1.SubqueriesinSELECT

What happens if the subquery returns more than one city ?Runtime error

Q: For each product return the city where it is manufactured!

SELECT P.pname, (SELECT C.cityFROM Company2 CWHERE C.cid = P.cid)

FROM Product2 P

Product2 (pname, price, cid)Company2 (cid, cname, city)

® "Scalar subqueries"

315

Page 25: L02-06-SQL - v180129

148

1.SubqueriesinSELECT

Q: For each product return the city where it is manufactured!

SELECT P.pname, C.cityFROM Product2 P, Company2 CWHERE C.cid = P.cid

"unnesting the query" Whenever possible, don't use nested queries

SELECT P.pname, (SELECT C.cityFROM Company2 CWHERE C.cid = P.cid)

FROM Product2 P

Product2 (pname, price, cid)Company2 (cid, cname, city)

315

Page 26: L02-06-SQL - v180129

149

1.SubqueriesinSELECT

Better: we can unnestby using a GROUP BY:

Q: Compute the number of products made by each company!

SELECT C.cname, ( SELECTcount (*)FROM Product2 PWHERE P.cid = C.cid)

FROM Company2 C

Product2 (pname, price, cid)Company2 (cid, cname, city)

SELECT C.cname, count(*)FROM Company2 C, Product2 PWHERE C.cid=P.cidGROUP BY C.cname

315

Page 27: L02-06-SQL - v180129

150

2.SubqueriesinFROMclause

Q: Find all products whose prices are > 20 and < 30!

SELECT X.pname FROM ( SELECT *

FROM Product2 as PWHERE price >20 ) as X

WHERE X.price < 30

SELECT pnameFROM Product2WHERE price > 20 and price < 30

unnesting

Product2 (pname, price, cid)Company2 (cid, cname, city)

XPName Price cid

Powergizmo $29.99 1

MultiTouch $203.99 3

315

Page 28: L02-06-SQL - v180129

151

Subqueries in WHERE clause

IN, ANY, ALL

Page 29: L02-06-SQL - v180129

152

3.SubqueriesinWHEREWhat do these queries compute?

SELECT aFROM RWHERE a IN

(SELECT * from U)?

305Ra12

SELECT aFROM RWHERE a < ANY

(SELECT * from U)

SELECT aFROM RWHERE a < ALL

(SELECT * from U)

Ua234

?

?

Page 30: L02-06-SQL - v180129

153

3.SubqueriesinWHEREWhat do these queries compute?

SELECT aFROM RWHERE a IN

(SELECT * from U)

Since 2 is in the set(2, 3, 4)

a2

305Ra12

SELECT aFROM RWHERE a < ANY

(SELECT * from U)

a12

SELECT aFROM RWHERE a < ALL

(SELECT * from U)

a1

Ua234

Since 1 and 2 are <than at least one ("any") of 2, 3 or 4

Since 1 is < than each ("all") of 2, 3, and 4

Page 31: L02-06-SQL - v180129

154

SomethingtrickyaboutNestedQueries

SELECT c.cityFROM Company c,

Product pr, Purchase p

WHERE c.name = pr.makerAND pr.name = p.productAND p.buyer = ‘Joe B’

Arethesequeriesequivalent?

Bewareofduplicates!

SELECT c.cityFROM Company cWHERE c.name IN (SELECT pr.makerFROM Purchase p, Product prWHERE p.name = pr.product

AND p.buyer = ‘Joe B‘)

Page 32: L02-06-SQL - v180129

155

SomethingtrickyaboutNestedQueries

SELECT DISTINCT c.cityFROM Company c,

Product pr, Purchase p

WHERE c.name = pr.makerAND pr.name = p.productAND p.buyer = ‘Joe B’

Arethesequeriesequivalent?

SELECT DISTINCT c.cityFROM Company cWHERE c.name IN (SELECT pr.makerFROM Purchase p, Product prWHERE p.name = pr.product

AND p.buyer = ‘Joe B‘)

Nowtheyareequivalent(bothusesetsemantics)

Page 33: L02-06-SQL - v180129

156

Correlatedsubqueries

• Inallpreviouscases,thenestedsubqueryintheinnerselectblockcouldbeentirelyevaluatedbeforeprocessingtheouterselectblock.

• Thisisnolongerthecaseforcorrelatednestedqueries.• WheneveraconditionintheWHEREclauseofanestedqueryreferencessomecolumnofatabledeclaredintheouterquery,thetwoqueriesaresaidtobecorrelated.

• Thenestedqueryisthenevaluatedonceforeachtuple(orcombinationoftuples)intheouterquery.

Page 34: L02-06-SQL - v180129

157

CorrelatedQueries(UsingExternalVars inInternalSubquery)

SELECT DISTINCT titleFROM Movie AS mWHERE year <> ANY(

SELECT yearFROM MovieWHERE title = m.title)

Movie(title, year, director, length)

Notealso:thiscanstillbeexpressedassingleSFWquery…

Findmovieswhosetitleappearsinmorethanoneyear.

Notethescopingofthevariables!

Page 35: L02-06-SQL - v180129

158

ComplexCorrelatedQuery

SELECT DISTINCT x.name, x.makerFROM Product AS xWHERE x.price > ALL(

SELECT y.priceFROM Product AS yWHERE x.maker = y.maker

AND y.year < 1972)

Findproducts(andtheirmanufacturers)thataremoreexpensivethanallproductsmadebythesamemanufacturerbefore1972

Product(name, price, category, maker, year)

Canbeverypowerful(alsomuchhardertooptimize)

Page 36: L02-06-SQL - v180129

159

3.SubqueriesinWHERE(existential)

Existential quantifiers $

Using IN:

Q: Find all companies that make some products with price < 25!

SELECT DISTINCT C.cname FROM Company2 CWHERE C.cid IN ( 1, 2 )

Product2 (pname, price, cid)Company2 (cid, cname, city)

PName Price cid

Gizmo $19.99 1

Powergizmo $29.99 1

SingleTouch $14.99 2

MultiTouch $203.99 3

cid CName City

1 GizmoWorks Oslo

2 Canon Osaka

3 Hitachi Kyoto

315

Page 37: L02-06-SQL - v180129

160

3.SubqueriesinWHERE(existential)

Existential quantifiers $

Using IN:

Q: Find all companies that make some products with price < 25!

SELECT DISTINCT C.cname FROM Company2 CWHERE C.cid IN ( SELECT P.cid

FROM Product2 PWHERE P.price < 25)

Product2 (pname, price, cid)Company2 (cid, cname, city)

PName Price cid

Gizmo $19.99 1

Powergizmo $29.99 1

SingleTouch $14.99 2

MultiTouch $203.99 3

cid CName City

1 GizmoWorks Oslo

2 Canon Osaka

3 Hitachi Kyoto

315

"Set membership"

Page 38: L02-06-SQL - v180129

161

3.SubqueriesinWHERE(existential)

Existential quantifiers $

Using EXISTS:

Q: Find all companies that make some products with price < 25!

SELECT DISTINCT C.cname FROM Company2 CWHERE EXISTS ( SELECT *

FROM Product2 PWHERE C.cid = P.cid

and P.price < 25)

Product2 (pname, price, cid)Company2 (cid, cname, city)

PName Price cid

Gizmo $19.99 1

Powergizmo $29.99 1

SingleTouch $14.99 2

MultiTouch $203.99 3

cid CName City

1 GizmoWorks Oslo

2 Canon Osaka

3 Hitachi Kyoto

315

"Test for empty relations"

Correlated subquery

Page 39: L02-06-SQL - v180129

162

3.SubqueriesinWHERE(existential)

Existential quantifiers $

Using ANY (also some):

Q: Find all companies that make some products with price < 25!

SELECT DISTINCT C.cname FROM Company2 CWHERE 25 > ANY ( SELECT price

FROM Product2 PWHERE P.cid = C.cid)

Product2 (pname, price, cid)Company2 (cid, cname, city)

SQLlite does not support "ANY" L

PName Price cid

Gizmo $19.99 1

Powergizmo $29.99 1

SingleTouch $14.99 2

MultiTouch $203.99 3

cid CName City

1 GizmoWorks Oslo

2 Canon Osaka

3 Hitachi Kyoto

315

"Set comparison"

Correlated subquery

Page 40: L02-06-SQL - v180129

163

3.SubqueriesinWHERE(existential)

Existential quantifiers $

Now, let's unnest:

Q: Find all companies that make some products with price < 25!

SELECT DISTINCT C.cname FROM Company2 C, Product2 PWHERE C.cid = P.cid

and P.price < 25

Existential quantifiers are easy ! J

Product2 (pname, price, cid)Company2 (cid, cname, city)

PName Price cid

Gizmo $19.99 1

Powergizmo $29.99 1

SingleTouch $14.99 2

MultiTouch $203.99 3

cid CName City

1 GizmoWorks Oslo

2 Canon Osaka

3 Hitachi Kyoto

315

Page 41: L02-06-SQL - v180129

164

3.SubqueriesinWHERE(universal)

Universal quantifiers "

Q: Find all companies that make only products with price < 25!

Q: Find all companies for which all products have price < 25!

Universal quantifiers are more complicated ! L(Think about the companies that should not be returned)

same as:

Product2 (pname, price, cid)Company2 (cid, cname, city)

315

Page 42: L02-06-SQL - v180129

165

3.SubqueriesinWHERE(existnot->universal)

2. Find all companies s.t. all their products have price < 25!

1. Find the other companies: i.e. they have some product ³ 25!

SELECT DISTINCT C.cname FROM Company2 CWHERE C.cid IN ( SELECT P.cid

FROM Product2 PWHERE P.price >= 25)

SELECT DISTINCT C.cname FROM Company2 CWHERE C.cid NOT IN ( SELECT P.cid

FROM Product2 PWHERE P.price >= 25)

Q: Find all companies that make only products with price < 25!315

Page 43: L02-06-SQL - v180129

166

3.SubqueriesinWHERE(existnot->universal)

Using NOT EXISTS:

SELECT DISTINCT C.cname FROM Company2 CWHERE NOT EXISTS ( SELECT *

FROM Product2 PWHERE C.cid = P.cid

and P.price >= 25)

Universal quantifiers "

Q: Find all companies that make only products with price < 25!

Product2 (pname, price, cid)Company2 (cid, cname, city)

315

Page 44: L02-06-SQL - v180129

167

3.SubqueriesinWHERE(existnot->universal)

Using ALL:

Universal quantifiers "

Q: Find all companies that make only products with price < 25!

SELECT DISTINCT C.cname FROM Company2 CWHERE 25 > ALL ( SELECT price

FROM Product2 PWHERE P.cid = C.cid)

Product2 (pname, price, cid)Company2 (cid, cname, city)

SQLlite does not support "ALL" L

315

Page 45: L02-06-SQL - v180129

168

QuestionforDatabaseFans&Friends

• Howcanweunnesttheuniversalquantifierquery?

Thistopicgoesbeyondthecourseobjectives;onlyfor

thosewhoarereallyinterested

Page 46: L02-06-SQL - v180129

169

Queriesthatmustbenested

• Definition:AqueryQismonotoneif:- Wheneverweaddtuplestooneormoreofthetables…- …theanswertothequerycannotcontainfewertuples

• Fact:allunnestedqueriesaremonotone- Proof:usingthe"nestedforloops"semantics

• Fact:Querywithuniversalquantifierisnotmonotone- Addonetupleviolatingthecondition.Then"all"returnsfewertuples

• Consequence:wecannotunnestaquerywithauniversalquantifier

Thistopicgoesbeyondthecourseobjectives;onlyfor

thosewhoarereallyinterested

Page 47: L02-06-SQL - v180129

170

Thedrinkers-bars-beersexample

Find drinkers that frequent some bar that serves some beer they like.

Find drinkers that frequent only bars that serve some beer they like.

Find drinkers that frequent only bars that serve only beer they like.

x: $y. $z. Frequents(x, y)ÙServes(y,z)ÙLikes(x,z)

x: "y. Frequents(x, y)Þ ($z. Serves(y,z)ÙLikes(x,z))

x: "y. Frequents(x, y)Þ "z.(Serves(y,z) Þ Likes(x,z))

Find drinkers that frequent some bar that serves only beers they like.

x: $y. Frequents(x, y)Ù"z.(Serves(y,z) Þ Likes(x,z))

Likes(drinker, beer)Frequents(drinker, bar)Serves(bar, beer)

Challenge: write these in SQL.Solutions: http://queryviz.com/online/

331

Page 48: L02-06-SQL - v180129

171

BasicSQLSummary

• SQLprovidesahigh-leveldeclarativelanguageformanipulatingdata(DML)

• TheworkhorseistheSFWblock

• Setoperatorsarepowerfulbuthavesomesubtleties

• Powerful,nestedqueriesalsoallowed.

Page 49: L02-06-SQL - v180129

1721

WITH clause

Page 50: L02-06-SQL - v180129

173

WITHclause:temporaryrelationsSELECT pname, priceFROM Product2WHERE price =

(SELECT max(price)FROM Product2)

315

WITH max_price(value) as(SELECT max(price)FROM Product2)

SELECT pname, priceFROM Product2, max_priceWHERE price = value

Product (pname, price, cid)

TheWITHclausedefinesatemporaryrelationthatisavailableonlytothequeryinwhichitoccurs.Sometimeseasiertoread.Veryusefulforqueriesthatneedtoaccessthesameintermediateresultmultipletimes

Page 51: L02-06-SQL - v180129

174

WITHclause:temporaryrelationsSELECT pname, priceFROM Product2WHERE price =

(SELECT max(price)FROM Product2)

315

WITH max_price as(SELECT max(price) as valueFROM Product2)

SELECT pname, priceFROM Product2, max_priceWHERE price = value

Product (pname, price, cid)

TheWITHclausedefinesatemporaryrelationthatisavailableonlytothequeryinwhichitoccurs.Sometimeseasiertoread.Veryusefulforqueriesthatneedtoaccessthesameintermediateresultmultipletimes

Page 52: L02-06-SQL - v180129

175

Witnesses

Page 53: L02-06-SQL - v180129

176

Motivation:Whatarethesequeriessupposedtoreturn?

PName Price cidGizmo 15 1SuperGizmo 20 1iTouch1 300 2iTouch2 300 2

Product2cid cname city1 GizmoWorks Oslo2 Apple MountainView

Company2

Findforeachcompanyid,themaximumpriceamongstitsproducts ?

Page 54: L02-06-SQL - v180129

177

Motivation:Whatarethesequeriessupposedtoreturn?

PName Price cidGizmo 15 1SuperGizmo 20 1iTouch1 300 2iTouch2 300 2

Product2cid cname city1 GizmoWorks Oslo2 Apple MountainView

Company2

cid mp1 202 300

Findforeachcompanyid,themaximumpriceamongstitsproducts

Findforeachcompanyid,theproductwithmaximumpriceamongstitsproducts ?

Page 55: L02-06-SQL - v180129

178

Motivation:Whatarethesequeriessupposedtoreturn?

PName Price cidGizmo 15 1SuperGizmo 20 1iTouch1 300 2iTouch2 300 2

Product2cid cname city1 GizmoWorks Oslo2 Apple MountainView

Company2

cid mp pname1 20 SuperGizmo2 300 iTouch12 300 iTouch2

cid mp1 202 300

Findforeachcompanyid,themaximumpriceamongstitsproducts

Findforeachcompanyid,theproductwithmaximumpriceamongstitsproducts(Recallthat"groupbycid"canjustgiveusonesingletuplepercid)

Page 56: L02-06-SQL - v180129

179

Witnesses:simple(1/4)

Q:Findthemostexpensiveproduct +itsprice315

Product2 (pname, price, cid)

(Findingthemaximumpricealonewouldbeeasy)

Page 57: L02-06-SQL - v180129

180

Witnesses:simple(2/4)

SELECT max(P1.price)FROM Product2 P1

Butwewantthe"witnesses,"i.e.theproduct(s)withthemaxprice.Howdowedothat?

OurPlan:• 1.Computemaxpriceinasubquery

Q:Findthemostexpensiveproduct +itsprice

Product2 (pname, price, cid)315

1.

(Findingthemaximumpricealonewouldbeeasy)

Page 58: L02-06-SQL - v180129

181

Witnesses:simple(3/4)

SELECT P2.pname, P2.priceFROM Product2 P2

OurPlan:• 1.Computemaxpriceinasubquery• 2.Computeeachproductanditsprice...

Q:Findthemostexpensiveproduct +itsprice

Product2 (pname, price, cid)

SELECT max(P1.price)FROM Product2 P1

Butwewantthe"witnesses,"i.e.theproduct(s)withthemaxprice.Howdowedothat?

315

1.

2.

(Findingthemaximumpricealonewouldbeeasy)

Page 59: L02-06-SQL - v180129

182

Witnesses:simple(4/4)

SELECT P2.pname, P2.priceFROM Product2 P2WHERE P2.price =

(SELECT max(P1.price)FROM Product2 P1)

OurPlan:• 1.Computemaxpriceinasubquery• 2.Computeeachproductanditsprice...

andcomparethepricewiththemaxprice

(Findingthemaximumpricealonewouldbeeasy)

Product2 (pname, price, cid)

Q:Findthemostexpensiveproduct +itsprice315