Chapter (7): Advanced SQL SELECT Statement Chapter (7): Advanced SQL
Find names of sailors who’ve reserved boat #103 Reserves SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103
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
CROSS JOIN Sailors Reserves SELECT * FROM Sailors S, Reserves R FROM Sailors S CROSS JOIN Reserves R
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
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
NATURAL-JOIN SELECT * FROM Sailors S INNER JOIN Reserves R USING sid FROM Sailors S NATURAL JOIN Reserves R USING sid
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
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
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
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
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’
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’
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’
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’
SELF-JOIN
VIEW CREATE VIEW Emp_Manager AS SELECT * FROM Emp_Manager SELECT MANAGER FROM Emp_Manager SELECT MANAGER FROM Emp_Manager WHERE EMPLOYEEID = 123 Security …