l02-06-sql - v180129

Post on 02-Oct-2021

2 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

124

L04:SQL

125

Announcements!

• PollsonPiazza.Openfor2days• Outlinetoday:- practicingmorejoinsandspecifyingkeyandFKconstraints- nestedqueries

• Nexttime:"witnesses"(traditionallystudentsfindthistopicthemostdifficult)

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

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)

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)

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)

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)

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

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:𝑍 = (𝑦. 𝐴, )𝑓𝑜𝑟𝑦 ∈ 𝑌

133

Note:wesay“semantics”not“executionorder”

• Theprecedingslidesshowwhatajoinmeans

• NotactuallyhowtheDBMSexecutesitunderthecovers

134

Practicing more Joins

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

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

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

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

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

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

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!

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!

143

Nested queries(Subqueries)

144

High-levelnoteonnestedqueries

• WecandonestedqueriesbecauseSQLiscompositional:

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

• Thisisextremelypowerful!

• High-levelidea:subqueriesreturnrelations(yetsometimesjustvalues)

145

Subqueries=Nestedqueries

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

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

Outerblock

Innerblock

146

Subqueries

important!

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

• Ruleofthumb:avoidwritingnestedquerieswhenpossible;keepinmindthatsometimesit’simpossible

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

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

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

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

151

Subqueries in WHERE clause

IN, ANY, ALL

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

?

?

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

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‘)

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)

156

Correlatedsubqueries

• Inallpreviouscases,thenestedsubqueryintheinnerselectblockcouldbeentirelyevaluatedbeforeprocessingtheouterselectblock.

• Thisisnolongerthecaseforcorrelatednestedqueries.• WheneveraconditionintheWHEREclauseofanestedqueryreferencessomecolumnofatabledeclaredintheouterquery,thetwoqueriesaresaidtobecorrelated.

• Thenestedqueryisthenevaluatedonceforeachtuple(orcombinationoftuples)intheouterquery.

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!

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)

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

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"

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

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

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

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

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

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

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

168

QuestionforDatabaseFans&Friends

• Howcanweunnesttheuniversalquantifierquery?

Thistopicgoesbeyondthecourseobjectives;onlyfor

thosewhoarereallyinterested

169

Queriesthatmustbenested

• Definition:AqueryQismonotoneif:- Wheneverweaddtuplestooneormoreofthetables…- …theanswertothequerycannotcontainfewertuples

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

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

• Consequence:wecannotunnestaquerywithauniversalquantifier

Thistopicgoesbeyondthecourseobjectives;onlyfor

thosewhoarereallyinterested

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

171

BasicSQLSummary

• SQLprovidesahigh-leveldeclarativelanguageformanipulatingdata(DML)

• TheworkhorseistheSFWblock

• Setoperatorsarepowerfulbuthavesomesubtleties

• Powerful,nestedqueriesalsoallowed.

1721

WITH clause

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

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

175

Witnesses

176

Motivation:Whatarethesequeriessupposedtoreturn?

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

Product2cid cname city1 GizmoWorks Oslo2 Apple MountainView

Company2

Findforeachcompanyid,themaximumpriceamongstitsproducts ?

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 ?

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)

179

Witnesses:simple(1/4)

Q:Findthemostexpensiveproduct +itsprice315

Product2 (pname, price, cid)

(Findingthemaximumpricealonewouldbeeasy)

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)

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)

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

top related