CMPT 258 Database Systems SQL Queries (Chapter 5).
-
Upload
doris-harmon -
Category
Documents
-
view
232 -
download
1
Transcript of CMPT 258 Database Systems SQL Queries (Chapter 5).
![Page 1: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/1.jpg)
CMPT 258 Database SystemsSQL Queries(Chapter 5)
![Page 2: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/2.jpg)
2
Agenda
•Introduction•Basic SQL Query•Union, Intersection and Except•Nested Queries•Aggregate Operations
![Page 3: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/3.jpg)
Examples
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.5 58 rusty 10 35.0
bid bname color101 Interlake Blue102 Interlake Red103 Clipper Green104 Marine Red
sid bid day
22 101 10/10/14 58 103 11/12/14
Reserves
Sailors
Boats
3
![Page 4: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/4.jpg)
Basic SQL Query
•relation-list A list of relation names (possibly with a range-variable after each name).
• target-list A list of attributes of relations in relation-list
•qualification Comparisons (Attr op const or Attr1 op Attr2, where op is one of ) combined using AND and OR .
SELECT target-listFROM relation-listWHERE qualification
, , , , ,
![Page 5: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/5.jpg)
•The query uses the optional keyword AS to introduce a range variable.
5
![Page 6: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/6.jpg)
Basic SQL Query
• DISTINCT is an optional keyword indicating that the answer should not contain duplicates. ▫Default is that duplicates are not eliminated!
SELECT [DISTINCT] target-listFROM relation-listWHERE qualification
![Page 7: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/7.jpg)
7
![Page 8: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/8.jpg)
8
![Page 9: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/9.jpg)
Conceptual Evaluation Strategy
•Semantics of an SQL query defined in terms of the following conceptual evaluation strategy:▫ Compute the cross-product of relation-list.▫ Discard resulting tuples if they fail
qualifications.▫ Delete attributes that are not in target-list.▫ If DISTINCT is specified, eliminate duplicate
rows.
SELECT [DISTINCT] target-listFROM relation-listWHERE qualification
![Page 10: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/10.jpg)
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.558 rusty 10 35.0
sid bid day
22 101 10/10/14 58 103 11/12/14
Reserves
Sailors
bid bname color101 Interlake Blue102 Interlake Red103 Clipper Green104 Marine Red
Boats
![Page 11: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/11.jpg)
(sid) sname rating age (sid) bid day
22 dustin 7 45.0 22 101 10/ 10/ 96
22 dustin 7 45.0 58 103 11/ 12/ 96
31 lubber 8 55.5 22 101 10/ 10/ 96
31 lubber 8 55.5 58 103 11/ 12/ 96
58 rusty 10 35.0 22 101 10/ 10/ 96
58 rusty 10 35.0 58 103 11/ 12/ 96
![Page 12: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/12.jpg)
A Note on Range Variables•Really needed only if the same relation
appears twice in the FROM clause. •The previous query can also be written as:
SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid AND R.bid=103
SELECT snameFROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103
It is good style,however, to userange variablesalways!
OR
![Page 13: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/13.jpg)
13
Query Examples•Find the sids of sailors who have reserved
a red boat
![Page 14: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/14.jpg)
14
Query Examples•Find the sids of sailors who have reserved
a red boat
•Find the name of sailors who have reserved a red boat
SELECT R.sidFROM Reserves R, Boats BWHERE R.bid=B.bid AND B.color = ‘red’
![Page 15: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/15.jpg)
15
Query Examples•Find the sids of sailors who have reserved
a red boat
•Find the name of sailors who have reserved a red boat
SELECT R.sidFROM Reserves R, Boats BWHERE R.bid=B.bid AND B.color = ‘red’
SELECT S.snameFROM Sailors S, Reserves R, Boats BWHERE S.sid=R.sid AND R.bid = B.bid AND B.color = ‘red’
![Page 16: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/16.jpg)
•Find the color of the boats reserved by Lubber
16
![Page 17: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/17.jpg)
•Find the color of the boats reserved by Lubber
17
SELECT B.colorFROM Sailors S, Reserves R, Boats BWHERE S.sid=R.sid AND R.bid = B.bid AND S.name = ‘Lubber’
![Page 18: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/18.jpg)
Find the names of sailors who’ve reserved at least one boat
![Page 19: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/19.jpg)
Find the names of sailors who’ve reserved at least one boat
SELECT DISTINCT S.nameFROM Sailors S, Reserves RWHERE S.sid=R.sid
![Page 20: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/20.jpg)
20
•Find the names of sailors who have sailed two different boats on the same day.
![Page 21: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/21.jpg)
21
•Find the names of sailors who have sailed two different boats on the same day.
SELECT S.snameFROM Sailors S, Reserves R1, Reserves R2WHERE S.sid= R1.sid and R1.sid=R2.sid
AND R1.day = R2.day AND R1.bid <> R2.bid
![Page 22: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/22.jpg)
Expressions and Strings• Find ages of sailors whose names begin and end
with B and contain at least three characters.
• LIKE is used for string matching. ‘_’ stands for any one character and ‘%’ stands for 0 or more arbitrary characters (including blanks).
• ‘_AB%’• Comparison operators (=, <, >, etc.) can be used
for string comparison
![Page 23: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/23.jpg)
Expressions and Strings• Find ages of sailors whose names begin and end
with B and contain at least three characters.
SELECT S.ageFROM Sailors SWHERE S.sname LIKE ‘B_%B’
![Page 24: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/24.jpg)
Query FROM More than One Table Using JOINs
• A join combines the rows of two tables, based on a rule called a join condition; this compares values from the rows of both tables to determine which rows should be joined.
• Basic types of join:▫inner join▫outer join
LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
24
![Page 25: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/25.jpg)
![Page 26: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/26.jpg)
Inner Join
• select a, b• from A, B• where a=b
26
![Page 27: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/27.jpg)
27
![Page 28: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/28.jpg)
28
![Page 29: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/29.jpg)
29
![Page 30: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/30.jpg)
sid sname rating age
22 dustin 7 45.0
31 lubber 8 55.558 rusty 10 35.0
sid bid day
22 101 10/10/14 58 103 11/12/14
Reserves
Sailors
bid bname color101 Interlake Blue102 Interlake Red103 Clipper Green104 Marine Red
Boats
![Page 31: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/31.jpg)
•Union•Intersect•Except
31
![Page 32: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/32.jpg)
Find sid’s of sailors who’ve reserved a red or a green boat
![Page 33: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/33.jpg)
Find sid’s of sailors who’ve reserved a red or a green boat• UNION: Can be used
to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries).
SELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’)
![Page 34: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/34.jpg)
Find sid’s of sailors who’ve reserved a red or a green boat• UNION: Can be used
to compute the union of any two union-compatible sets of tuples (which are themselves the result of SQL queries).
• If we replace OR by AND in the first version, what do we get?
SELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’)
SELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘red’UNIONSELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘green’
![Page 35: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/35.jpg)
35
Find the sids of sailors who have a rating of 10 or reserved boat 104
![Page 36: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/36.jpg)
36
Find the sids of sailors who have a rating of 10 or reserved boat 104
•The default for UNION queries is that duplicates are eliminated.
•To retain duplicates, use UNION ALL
SELECT S.sidFROM Sailors SWHERE S.rating = 10 UNIONSELECT R.sidFROM Reserves RWHERE R.bid=104
![Page 37: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/37.jpg)
Find sid’s of sailors who’ve reserved a red and a green boat
• INTERSECT: Can be used to compute the intersection of any two union-compatible sets of tuples.
• Some systems don’t support it.
SELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘red’INTERSECTSELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘green’
![Page 38: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/38.jpg)
38
Find the sids of all sailors who have reserved red boats but not green boats
SELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘red’EXCEPTSELECT R.sidFROM Boats B, Reserves RWHERE R.bid=B.bid AND B.color=‘green’
![Page 39: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/39.jpg)
39
Find the names of sailors who’ve reserved a red and a green boat
• Is this query correct? Why
SELECT S.snameFROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’INTERSECTSELECT S.snameFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
![Page 40: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/40.jpg)
40Find the names of sailors who’ve reserved a red and a green boat• Is this query correct?
WhySELECT S.snameFROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’INTERSECTSELECT S.snameFROM Sailors S, Boats B, Reserves RWHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
There are two sailors with the same name Horatio!
![Page 41: CMPT 258 Database Systems SQL Queries (Chapter 5).](https://reader035.fdocuments.in/reader035/viewer/2022062222/5697bffc1a28abf838cc1835/html5/thumbnails/41.jpg)
Readings
•Chapter 5