Presentation is loading. Please wait.

Presentation is loading. Please wait.

SELECT Statement Chapter (7): Advanced SQL. ReservesSailors Find names of sailors who’ve reserved boat #103 SELECT sname FROM Sailors, Reserves WHERE.

Similar presentations


Presentation on theme: "SELECT Statement Chapter (7): Advanced SQL. ReservesSailors Find names of sailors who’ve reserved boat #103 SELECT sname FROM Sailors, Reserves WHERE."— Presentation transcript:

1 SELECT Statement Chapter (7): Advanced SQL

2 ReservesSailors Find names of sailors who’ve reserved boat #103 SELECT sname FROM Sailors, Reserves WHERE Sailors.sid=Reserves.sid AND bid=103

3 ReservesSailors Find names of sailors who’ve reserved boat #103 SELECT S.sname FROM Sailors S, Reserves R WHERE S.sid=R.sid AND bid=103

4 ReservesSailors CROSS JOIN SELECT * FROM Sailors S, Reserves R SELECT * FROM Sailors S CROSS JOIN Reserves R

5 ReservesSailors INNER-JOIN 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 Reserves Sailors EQUI-JOIN 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 SELECT * FROM Sailors S NATURAL JOIN Reserves R USING sid

8 Report all the sailors with their reservations LEFT OUTER-JOIN 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 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 SELECT * FROM Reserves R RIGHT OUTER-JOIN Boats B WHERE R.bid=B.bid ReservesBoats

11 Reserves Sailors Find sailors’ names who’ve reserved at least one boat 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 SELECT S.sname FROM Sailors S, Boats B, Reserves R 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 SELECT S.sname FROM Sailors S, Reserves R, Boats B 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 SELECT S.sname FROM Sailors S, Reserves R, Boats B 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 SELECT S.sname FROM Sailors S, Reserves R, Boats B 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 …


Download ppt "SELECT Statement Chapter (7): Advanced SQL. ReservesSailors Find names of sailors who’ve reserved boat #103 SELECT sname FROM Sailors, Reserves WHERE."

Similar presentations


Ads by Google