Download presentation
Presentation is loading. Please wait.
Published byGerard David Cook Modified over 9 years ago
1
CMPT 258 Database Systems SQL: Queries, Constraints, Triggers (Chapter 5) Part II home.manhattan.edu/~tina.tian
2
Tutors Available! M 10-11am T 3-4pm W 12-1pm, 1-2pm R 2-3pm F 10-11am RLC 203
3
3 Agenda Introduction Basic SQL Query Union, Intersection and Except Nested Queries Aggregate Operations Complex Integrity Constraints in SQL Triggers
4
Examples Reserves Sailors Boats 4
5
Union (supported in MySQL) Intersect Except/Minus 5
6
Find sid’s of sailors who’ve reserved a red or a green boat SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ UNION SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘green’ supported in MySQL
7
Find sid’s of sailors who’ve reserved a red and a green boat SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘green’
8
Find sid’s of sailors who’ve reserved a red boat but not a green boat SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘red’ EXCEPT SELECT R.sid FROM Boats B, Reserves R WHERE R.bid=B.bid AND B.color=‘green’
9
Example SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid=103 Find names of sailors who’ve reserved boat #103: Reserves Sailors
10
Nested Queries Understand semantics of nested queries ▫For each Sailors tuple, check the qualification by computing the subquery. SELECT sname FROM Sailors WHERE sid IN (SELECT sid FROM Reserves WHERE bid=103) Find names of sailors who’ve reserved boat #103:
11
Nested Queries Find names of sailors who’ve NOT reserved boat #103: SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid = R.sid AND R.bid<>103 Reserves Sailors Is this correct?
12
Nested Queries “Find names of sailors whose ids do not appear among the ids that reserved boat #103” is different with “Find names of sailors who have reserved a boat that is not 103” Find names of sailors who’ve NOT reserved boat #103: Reserves Sailors
13
Nested Queries SELECT sname FROM Sailors WHERE sid NOT IN (SELECT sid FROM Reserves WHERE bid=103) Find names of sailors who’ve NOT reserved boat #103: A very powerful feature of SQL: a WHERE clause can itself contain an SQL query!
14
14 Q: Find the sids of sailors who have reserved a red boat SELECT R.sid FROM Reserves R, Boats B WHERE R.bid = B.bid and B.color=‘red’ Reserves Boats Rewrite the query using IN?
15
15 Q: Find the names of sailors who have reserved a red boat
16
16 Q: Find the names of sailors who have not reserved a red boat
17
Rewriting INTERSECT Queries Using IN Q: Find sid’s of sailors who’ve reserved both a red and a green boat: Reserves Boats
18
Rewriting EXCEPT Queries Q: Find sid’s of sailors who’ve reserved a red but not a green boat :
19
19 Find the names of sailors who’ve reserved a red and a green boat Is this query correct? Why SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ INTERSECT SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
20
20 Find the names of sailors who’ve reserved a red and a green boat
21
Set-Comparison Operators Also available: op ANY, op ALL ▫OP is one of, <> Q: Find sailors whose rating is equal to that of some sailor called Horatio: Can you rewrite the query without using set-comparison operators? SELECT * FROM Sailors WHERE rating = ANY ( SELECT rating FROM Sailors WHERE sname=‘Horatio’)
22
Set-Comparison Operators IN is equivalent to =ANY NOT IN is equivalent to <>ANY SELECT * FROM Sailors WHERE rating <> ANY ( SELECT rating FROM Sailors WHERE sname=‘Horatio’)
23
23 Set-Comparison Operators Q: Find sailors whose rating is better than every sailor called Horatio. SELECT * FROM Sailors WHERE rating > ALL ( SELECT rating FROM Sailors WHERE sname=‘Horatio’)
24
24 Set-Comparison Operators Q: Find the sailors with the highest rating.
25
Summary ANY ALL >,>=,<,<= [ANY/ALL] IN is equivalent to =ANY NOT IN is equivalent to <>ANY 25
26
Nested Queries with Correlation SELECT S.sname FROM Sailors S WHERE EXISTS ( SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Q: Find names of sailors who’ve reserved boat #103: Reserves Sailors
27
Nested Queries with Correlation SELECT S.sname FROM Sailors S WHERE EXISTS ( SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Q: Find names of sailors who’ve reserved boat #103: SELECT S.sname FROM Sailors S WHERE S.sid IN ( SELECT R.sid FROM Reserves R WHERE R.bid=103 SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid =R.sid and R.bid=103
28
Nested Queries with Correlation EXISTS is another set comparison operator, like IN. NOT EXISTS SELECT S.sname FROM Sailors S WHERE NOT EXISTS ( SELECT * FROM Reserves R WHERE R.bid=103 AND S.sid=R.sid) Q: Find names of sailors who’ve not reserved boat #103: Reserves Sailors
29
Division in SQL Find names of sailors that there is no boat that has not been reserved by this sailor Q: Find names of sailors who’ve reserved all boats. Reserves Sailors Boats
30
Division in SQL SELECT S.sname FROM Sailors S WHERE NOT EXISTS (SELECT bid FROM Boats WHERE bid NOT IN (SELECT R.bid FROM Reserves R WHERE R.sid=S.sid)) For each sailor we check that there is no boat that has not been reserved by this sailor Q: Find names of sailors who’ve reserved all boats.
31
IN NOT IN EXISTS NOT EXISTS 31
32
Readings Chapter 5
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.