Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there...
Transcript of Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there...
![Page 1: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/1.jpg)
Lecture4:AdvancedSQL– PartII
![Page 2: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/2.jpg)
Announcements!
1. ProblemSet#1isreleased!• Wewilldiscusssomeofthequestionsattheendofthislecture
2. Projectgroupassignments• Doeseverybodyhaveateam?
3. Askquestions,Gotoofficehours,EngageonPiazza
2
Lecture4
![Page 3: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/3.jpg)
Lecture4:AdvancedSQL– PartII
Lecture4
![Page 4: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/4.jpg)
Today’sLecture
1. Aggregation&GROUPBY• ACTIVITY:FancySQLPartI
2. AdvanceSQL-izing• ACTIVITY:FancySQLPartII
3. ProblemSet#1Overview
4
Lecture4
![Page 5: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/5.jpg)
1.Aggregation&GROUPBY
5
Lecture4>Section1
![Page 6: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/6.jpg)
Whatyouwilllearnaboutinthissection
1. Aggregationoperators
2. GROUPBY
3. GROUPBY:withHAVING,semantics
4. ACTIVITY:FancySQLPt.I
6
Lecture4>Section1
![Page 7: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/7.jpg)
7
Aggregation
SELECT COUNT(*)FROM ProductWHERE year > 1995
ExceptCOUNT,allaggregationsapplytoasingleattribute
SELECT AVG(price)FROM ProductWHERE maker = “Toyota”
• SQLsupportsseveralaggregation operations:• SUM,COUNT,MIN,MAX,AVG
Lecture4>Section1>Aggregation
![Page 8: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/8.jpg)
8
• COUNTappliestoduplicates,unlessotherwisestated
SELECT COUNT(category) FROM ProductWHERE year > 1995
Note:SameasCOUNT(*).Why?
Weprobablywant:
SELECT COUNT(DISTINCT category)FROM ProductWHERE year > 1995
Aggregation:COUNT
Lecture4>Section1>Aggregation
![Page 9: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/9.jpg)
9
Purchase(product, date, price, quantity)
MoreExamples
SELECT SUM(price * quantity)FROM Purchase
SELECT SUM(price * quantity)FROM PurchaseWHERE product = ‘bagel’
Whatdothesemean?
Lecture4>Section1>Aggregation
![Page 10: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/10.jpg)
10
SimpleAggregationsPurchaseProduct Date Price Quantitybagel 10/21 1 20banana 10/3 0.5 10banana 10/10 1 10bagel 10/25 1.50 20
SELECT SUM(price * quantity)FROM PurchaseWHERE product = ‘bagel’
50(=1*20+1.50*20)
Lecture4>Section1>Aggregation
![Page 11: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/11.jpg)
11
GroupingandAggregation
SELECT product,SUM(price * quantity) AS TotalSales
FROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product
Let’sseewhatthismeans…
Findtotalsalesafter10/1/2005perproduct.
Lecture4>Section1>GROUPBY
Purchase(product, date, price, quantity)
![Page 12: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/12.jpg)
12
GroupingandAggregation
1.ComputetheFROM andWHERE clauses
2.GroupbytheattributesintheGROUPBY
3.ComputetheSELECT clause:groupedattributesandaggregates
Semanticsofthequery:
Lecture4>Section1>GROUPBY
![Page 13: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/13.jpg)
13
1.ComputetheFROM andWHERE clauses
Product Date Price QuantityBagel 10/21 1 20Bagel 10/25 1.50 20Banana 10/3 0.5 10Banana 10/10 1 10
SELECT product, SUM(price*quantity) AS TotalSalesFROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product
Lecture4>Section1>GROUPBY
FROM
![Page 14: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/14.jpg)
Product Date Price QuantityBagel 10/21 1 20Bagel 10/25 1.50 20Banana 10/3 0.5 10Banana 10/10 1 10
14
2.GroupbytheattributesintheGROUPBY
SELECT product, SUM(price*quantity) AS TotalSalesFROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product
Lecture4 >Section1>GROUPBY
GROUP BY Product Date Price Quantity
Bagel10/21 1 2010/25 1.50 20
Banana10/3 0.5 1010/10 1 10
![Page 15: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/15.jpg)
15
3.ComputetheSELECT clause:groupedattributesandaggregatesSELECT product, SUM(price*quantity) AS TotalSalesFROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product
Product TotalSales
Bagel 50
Banana 15
SELECTProduct Date Price Quantity
Bagel10/21 1 2010/25 1.50 20
Banana10/3 0.5 1010/10 1 10
Lecture4 >Section1>GROUPBY
![Page 16: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/16.jpg)
16
GROUPBYv.s.NestedQuereis
SELECT product, Sum(price*quantity) AS TotalSalesFROM PurchaseWHERE date > ‘10/1/2005’GROUP BY product
SELECT DISTINCT x.product, (SELECT Sum(y.price*y.quantity)FROM Purchase yWHERE x.product = y.product
AND y.date > ‘10/1/2005’) AS TotalSalesFROM Purchase xWHERE x.date > ‘10/1/2005’
Lecture3>Section2>GROUPBY
![Page 17: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/17.jpg)
17
HAVINGClause
Samequeryasbefore,exceptthatweconsideronlyproductsthathavemorethan100buyers
HAVINGclausescontainsconditionsonaggregates
SELECT product, SUM(price*quantity)FROM PurchaseWHERE date > ‘10/1/2005’GROUP BY productHAVING SUM(quantity) > 100
WhereasWHEREclausesconditiononindividualtuples…
Lecture4 >Section1>GROUPBY
![Page 18: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/18.jpg)
18
GeneralformofGroupingandAggregation
• S=CanONLYcontainattributesa1,…,ak and/oraggregatesoverotherattributes• C1 =isanyconditionontheattributesinR1,…,Rn• C2 =isanyconditionontheaggregateexpressions
SELECT SFROM R1,…,RnWHERE C1GROUP BY a1,…,akHAVING C2
Why?
Lecture4 >Section1>GROUPBY
![Page 19: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/19.jpg)
19
GeneralformofGroupingandAggregationSELECT SFROM R1,…,RnWHERE C1GROUP BY a1,…,akHAVING C2
Evaluationsteps:1. EvaluateFROM-WHERE:applyconditionC1 onthe
attributesinR1,…,Rn2. GROUPBYtheattributesa1,…,ak3. ApplyconditionC2 toeachgroup(mayhaveaggregates)4. ComputeaggregatesinSandreturntheresult
Lecture4 >Section1>GROUPBY
![Page 20: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/20.jpg)
20
Group-byv.s.NestedQuery
• Findauthorswhowrote³ 10documents:• Attempt1:withnestedqueries
SELECT DISTINCT Author.nameFROM AuthorWHERE COUNT(
SELECT Wrote.urlFROM WroteWHERE Author.login = Wrote.login) > 10
Author(login, name)Wrote(login, url)
ThisisSQLbyanovice
Lecture4 >Section1>GROUPBY
![Page 21: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/21.jpg)
21
Group-byv.s.NestedQuery
• Findallauthorswhowroteatleast10documents:• Attempt2:SQLstyle(withGROUPBY)
SELECT Author.nameFROM Author, WroteWHERE Author.login = Wrote.loginGROUP BY Author.nameHAVING COUNT(Wrote.url) > 10
NoneedforDISTINCT:automaticallyfromGROUPBY
ThisisSQLbyanexpert
Lecture4 >Section1>GROUPBY
![Page 22: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/22.jpg)
Group-byvs.NestedQuery
Whichwayismoreefficient?
• Attempt#1-Withnested:HowmanytimesdowedoaSFWqueryoveralloftheWroterelations?
• Attempt#2-Withgroup-by:Howaboutwhenwrittenthisway?
WithGROUPBYcanbemuch moreefficient!
Lecture4 >Section1>GROUPBY
![Page 23: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/23.jpg)
Activity-4-1.ipynb
23
Lecture4>Section1 >ACTIVITY
![Page 24: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/24.jpg)
3.AdvancedSQL-izing
24
Lecture4>Section2
![Page 25: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/25.jpg)
Whatyouwilllearnaboutinthissection
1. Quantifiers
2. NULLs
3. OuterJoins
4. ACTIVITY:FancySQLPt.II
25
Lecture4>Section2
![Page 26: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/26.jpg)
26
QuantifiersProduct(name, price, company)Company(name, city)
Findallcompaniesthatmakesomeproductswithprice<100
SELECT DISTINCT Company.cnameFROM Company, ProductWHERE Company.name = Product.company
AND Product.price < 100
Existential:easy!J
Lecture4>Section2 >Quantifiers
Anexistentialquantifier isalogicalquantifier(roughly)oftheform“thereexists”
![Page 27: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/27.jpg)
27
QuantifiersProduct(name, price, company)Company(name, city)
Findallcompanieswithproductsallhavingprice<100
SELECT DISTINCT Company.cnameFROM CompanyWHERE Company.name NOT IN(
SELECT Product.companyFROM Product.price >= 100)
Auniversalquantifier isoftheform“forall” Universal:hard!L
Findallcompaniesthatmakeonlyproductswithprice<100
Equivalent
Lecture4>Section2 >Quantifiers
![Page 28: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/28.jpg)
28
NULLSinSQL• Wheneverwedon’thaveavalue,wecanputaNULL
• Canmeanmanythings:• Valuedoesnotexists• Valueexistsbutisunknown• Valuenotapplicable• Etc.
• Theschemaspecifiesforeachattributeifcanbenull(nullable attribute)ornot
• HowdoesSQLcopewithtablesthathaveNULLs?
Lecture4 >Section2>NULLs
![Page 29: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/29.jpg)
29
NullValues
• Fornumericaloperations,NULL->NULL:• Ifx=NULLthen4*(3-x)/7isstillNULL
• Forboolean operations,inSQLtherearethreevalues:
FALSE= 0UNKNOWN= 0.5TRUE= 1
• Ifx=NULLthenx=“Joe”isUNKNOWN
Lecture4 >Section2>NULLs
![Page 30: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/30.jpg)
30
NullValues
• C1ANDC2=min(C1,C2)• C1OR C2=max(C1,C2)• NOTC1=1– C1
SELECT *FROM PersonWHERE (age < 25)
AND (height > 6 AND weight > 190)
Won’treturne.g.(age=20height=NULLweight=200)!
RuleinSQL:includeonlytuplesthatyieldTRUE(1.0)
Lecture4 >Section2>NULLs
![Page 31: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/31.jpg)
31
NullValues
Unexpectedbehavior:
SELECT *FROM PersonWHERE age < 25 OR age >= 25
SomePersonsarenotincluded!
Lecture4 >Section2>NULLs
![Page 32: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/32.jpg)
32
NullValues
CantestforNULLexplicitly:• xISNULL• xISNOTNULL
SELECT *FROM PersonWHERE age < 25 OR age >= 25
OR age IS NULL
NowitincludesallPersons!
Lecture4 >Section2>NULLs
![Page 33: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/33.jpg)
33
RECAP:InnerJoinsBy default,joinsinSQLare“innerjoins”:
SELECT Product.name, Purchase.storeFROM Product JOIN Purchase ON Product.name = Purchase.prodName
SELECT Product.name, Purchase.storeFROM Product, PurchaseWHERE Product.name = Purchase.prodName
Product(name, category)Purchase(prodName, store)
Bothequivalent:BothINNERJOINS!
Lecture4 >Section2>NULLs
![Page 34: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/34.jpg)
34
InnerJoins+NULLS=Lostdata?By default,joinsinSQLare“innerjoins”:
However:Productsthatneversold(withnoPurchasetuple)willbelost!
SELECT Product.name, Purchase.storeFROM Product JOIN Purchase ON Product.name = Purchase.prodName
SELECT Product.name, Purchase.storeFROM Product, PurchaseWHERE Product.name = Purchase.prodName
Product(name, category)Purchase(prodName, store)
Lecture4 >Section2>NULLs
![Page 35: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/35.jpg)
35
OuterJoins
• Anouterjoin returnstuplesfromthejoinedrelationsthatdon’thaveacorrespondingtupleintheotherrelations• I.e.IfwejoinrelationsAandBona.X =b.X,andthereisanentryinAwithX=5,butnoneinBwithX=5…• ALEFTOUTERJOINwillreturnatuple(a,NULL)!
• LeftouterjoinsinSQL:
Lecture4>Section2>OuterJoins
SELECT Product.name, Purchase.storeFROM Product LEFT OUTER JOIN Purchase ON
Product.name = Purchase.prodName
Nowwe’llgetproductseveniftheydidn’tsell
![Page 36: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/36.jpg)
36
name category
Gizmo gadget
Camera Photo
OneClick Photo
prodName store
Gizmo Wiz
Camera Ritz
Camera Wiz
name store
Gizmo Wiz
Camera Ritz
Camera Wiz
Product PurchaseINNERJOIN:
SELECT Product.name, Purchase.storeFROM Product
INNER JOIN Purchase ON Product.name = Purchase.prodName
Note:anotherequivalentwaytowriteanINNERJOIN!
Lecture4>Section2>OuterJoins
![Page 37: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/37.jpg)
37
name category
Gizmo gadget
Camera Photo
OneClick Photo
prodName store
Gizmo Wiz
Camera Ritz
Camera Wiz
name store
Gizmo Wiz
Camera Ritz
Camera Wiz
OneClick NULL
Product PurchaseLEFTOUTERJOIN:
SELECT Product.name, Purchase.storeFROM Product
LEFT OUTER JOIN Purchase ON Product.name = Purchase.prodName
Lecture4>Section2>OuterJoins
![Page 38: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/38.jpg)
38
OtherOuterJoins
• Leftouterjoin:• Includethelefttupleevenifthere’snomatch
• Rightouterjoin:• Includetherighttupleevenifthere’snomatch
• Fullouterjoin:• Includethebothleftandrighttuplesevenifthere’snomatch
Lecture4>Section2>OuterJoins
![Page 39: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/39.jpg)
Activity-4-2.ipynb
39
Lecture3>Section3>ACTIVITY
![Page 40: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/40.jpg)
Summary
SQLisarichprogramminglanguagethathandlesthewaydataisprocessed
declaratively
40
Lecture2,3,&4>SUMMARY
![Page 41: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/41.jpg)
ProblemSet#1:SQLUberAlles
Lecture4>ProblemSet#1
![Page 42: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/42.jpg)
ProblemsinPS#1
42
Lecture4>ProblemSet#1
1. LinearalgebrainSQL
2. Precipitationdataandnestedqueries
3. ThetravelingSQLsalesman:GraphtraversalsinSQL
![Page 43: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/43.jpg)
LinearalgebrainSQL
1. Simplejoinswithaggregations
2. Hint1:UsingaliasesleadstocleanSQL
3. Hint2:SQLsupportsmanyoperationsovernumericattributes(intheSELECTpartofanSFWquery)
43
Lecture4>ProblemSet#1
SELECT MAX(A.val*B.val)FROM A, BWHERE A.i= B.i AND A.j = B.j
![Page 44: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/44.jpg)
Precipitationdataandnestedqueries
1. Aggregatesinsidenestedqueries.RememberSQLiscompositional
2. Hint1:Breakdownquerydescriptiontosteps(subproblems)
3. Hint2:Wheneverindoubtalwaysgobacktothedefinition
44
Lecture4>ProblemSet#1
![Page 45: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/45.jpg)
PrecipitationdataandnestedqueriesExample:“Usinga singleSQLquery,findallofthestationsthathadthehighestdailyprecipitation(acrossallstations)onanygivenday.”
45
Lecture4>ProblemSet#1
SELECT station_id, dayFROM precipitation,
(SELECT day AS maxd, MAX(precipitation)AS maxpFROM precipitationGROUP BY day)
WHERE day = maxd AND precipitation = maxp
Precipitation
![Page 46: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/46.jpg)
ThetravelingSQLsalesman:GraphtraversalsinSQL
1. Views:Detailsinthedescription.Nothingmorethantempaliasesforqueries.Remember:SQLiscompositional!
2. Self-joinsareverypowerful
46
Lecture4>ProblemSet#1
![Page 47: Lecture 4 SQL Adv II - GitHub Pages · •I.e. If we join relations A and B on a.X= b.X, and there is an entry in A with X=5, but none in B with X=5… • A LEFT OUTER JOIN will](https://reader034.fdocuments.us/reader034/viewer/2022042221/5ec752edba938172c948542e/html5/thumbnails/47.jpg)
Example:“Findallpathsofsizetwoinadirectedgraph”
47
Lecture4>ProblemSet#1
SELECT e1.src, e1.trg, e2.trgFROM edges AS e1, edges AS e2, WHERE e1.trg = e2.src
ThetravelingSQLsalesman:GraphtraversalsinSQL
edge_id src trg
1 A B
2 B C
3 C D
Edges
Somemoreexamples:https://www.fusionbox.com/blog/detail/graph-algorithms-in-a-database-recursive-ctes-and-topological-sort-with-postgres/620/