Download presentation
Presentation is loading. Please wait.
1
Chapter (7): Advanced SQL
SELECT Statement Chapter (7): Advanced SQL
2
Find names of sailors who’ve reserved boat #103
Reserves SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103
3
Find names of sailors who’ve reserved boat #103
Reserves SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND bid=103
4
CROSS JOIN Sailors Reserves SELECT * FROM Sailors S, Reserves R
FROM Sailors S CROSS JOIN Reserves R
5
INNER-JOIN Sailors Reserves SELECT * FROM Sailors S, Reserves R
WHERE S.sid=R.sid SELECT * FROM Sailors S INNER JOIN Reserves R ON S.sid=R.sid SELECT * FROM Sailors S INNER JOIN Reserves R USING sid
6
EQUI-JOIN Sailors Reserves SELECT * FROM Sailors S, Reserves R
WHERE S.sid=R.sid SELECT * FROM Sailors S INNER JOIN Reserves R ON S.sid=R.sid SELECT * FROM Sailors S INNER JOIN Reserves R USING sid
7
NATURAL-JOIN SELECT * FROM Sailors S INNER JOIN Reserves R USING sid
FROM Sailors S NATURAL JOIN Reserves R USING sid
8
Report all the sailors with their reservations
LEFT OUTER-JOIN Report all the sailors with their reservations SELECT * FROM Sailors S, Reserves R WHERE S.sid=R.sid SELECT * FROM Sailors S LEFT OUTER JOIN Reserves R WHERE S.sid=R.sid
9
Report all the sailors with their reservations
LEFT OUTER-JOIN Report all the sailors with their reservations SELECT * FROM Sailors S, Reserves R WHERE S.sid=R.sid SELECT * FROM Sailors S NATURAL LEFT OUTER JOIN Reserves R WHERE S.sid=R.sid
10
Report all the boats with their reservations
RIGHT OUTER-JOIN Report all the boats with their reservations Reserves Boats SELECT * FROM Reserves R RIGHT OUTER-JOIN Boats B WHERE R.bid=B.bid
11
Find sailors’ names who’ve reserved at least one boat
Reserves SELECT DISTINCT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid
12
Find names of sailors who’ve reserved a red or a green boat
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ OR B.color=‘green’) SELECT S.sname FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ UNION WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
13
Find names of sailors who’ve reserved a red and a green boat
SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND (B.color=‘red’ AND B.color=‘green’) 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 WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
14
Find names of sailors who’ve reserved a red and a green boat
SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ AND S.sid IN (SELECT S2.sid FROM Sailors S2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’) 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 WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
15
Find names of sailors who’ve reserved a red and not a green boat
SELECT S.sid FROM Sailors S, Boats B, Reserves R WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ AND S.sid NOT IN (SELECT S2.sid FROM Sailors S2, Boats B2, Reserves R2 WHERE S2.sid=R2.sid AND R2.bid=B2.bid AND B2.color=‘green’) SELECT S.sname FROM Sailors S, Reserves R, Boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘red’ EXCEPT WHERE S.sid=R.sid AND R.bid=B.bid AND B.color=‘green’
16
SELF-JOIN
17
VIEW CREATE VIEW Emp_Manager AS SELECT * FROM Emp_Manager
SELECT MANAGER FROM Emp_Manager SELECT MANAGER FROM Emp_Manager WHERE EMPLOYEEID = 123 Security …
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.