Você está na página 1de 6

DBMS LAB MANUAL

COMMON TO BTECH CSE-II AND IT

WEEK 2
SQL> conn Enter user-name: prasad1 Enter password:prasad Connected. Sailors (sid: integer, sname: string, rating: integer, age: real) Boats (bid: integer, bname:string, color:string) Reserves (sid: integer, bid: integer, day: date) sid 22 29 31 32 58 64 71 74 85 95 sname rating Age Dustin 7 45.0 Brutus 1 33.0 Lubber 8 55.5 Andy 8 25.5 Rusty 10 35.0 Horatio 7 35.0 Zorba 10 16.0 Horatio 9 35.0 Art 3 25.5 BoB 3 63.5 Sailors Table bid 101 102 103 104 bname Interlake Interlake Clipper Marine Boats Table sid 22 22 22 22 31 31 31 64 64 74 bid 101 102 103 104 102 103 104 101 102 103 day 10/10/98 10/10/98 10/8/98 10/7/98 11/10/98 11/6/98 11/12/98 9/5/98 9/5/98 9/5/98 color blue red green Red

Reserves Table PRASAD SIR


1

prasad17gumpina@gmail.com

DBMS LAB MANUAL

COMMON TO BTECH CSE-II AND IT

QUERIES: 1. Find the names and sids of sailors who have reserved a red or a green boat? (UNION) 2. Find the names and sids of sailors who have reserved a red and a green boat? (INTERSECT) 3. Find all sids of sailors who have a rating of 10 or reserved boat 104 ? (UNION) 4. Find the names of sailors who have reserved boat 103 ? (IN) 5. Find the names of sailors who have reserved a red boat? (IN) 6. Find the names of sailors who have not reserved a red boat? (NOT IN) 7. Find sailors whose rating is better than some sailor called Horatio? (ANY) 8. Find the sailors with the highest rating? (ALL) 9. Find the names of sailors who have reserved both a red and a green boat? (IN) 10. Find the names of sailors who have reserved boat no 103? (EXISTS) 11. Find the names of sailors who have reserved all boats? (NOT EXISTS)

QUERIE 1: Find the names and sids of sailors who have reserved a red or a green boat? SQL> SELECT FROM WHERE UNION SELECT FROM WHERE S.sname, S.sid sailors S, reserves R, boats B S.sid=R.sid AND R.bid=B.bid AND B.color='red' S1.sname, S1.sid sailors S1, reserves R1, boats B1 S1.sid=R1.sid AND R1.bid=B1.bid AND B1.color='green';

SNAME SID -------------------- ---------Dustin 22 Horatio 64 Horatio 74 Lubber 31

QUERIE 2: Find the names and sids of sailors who have reserved a red and a green boat?

PRASAD SIR
2

prasad17gumpina@gmail.com

DBMS LAB MANUAL

COMMON TO BTECH CSE-II AND IT

SQL> SELECT S.sname, S.sid FROM sailors S, reserves R, boats B WHERE S.sid=R.sid AND R.bid=B.bid AND B.color='red' INTERSECT SELECT S1.sname, S1.sid FROM sailors S1, reserves R1, boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND B1.color='green'; SNAME SID -------------------- ---------Dustin 22 Lubber 31 QUERIE 3: Find all sids of sailors who have a rating of 10 or reserved boat 104? SQL> SELECT FROM WHERE UNION SELECT FROM WHERE SID ---------22 31 58 71 QUERIE 4: Find the names of sailors who have reserved boat 103 ? (IN) SQL> SELECT S.sname FROM sailors S WHERE S.sid IN (SELECT R.sid FROM reserves R WHERE R.bid=103); SNAME -------------------Dustin Lubber Horatio S.sid sailors S S.rating=10 R.sid reserves R R.bid=104;

PRASAD SIR
3

prasad17gumpina@gmail.com

DBMS LAB MANUAL

COMMON TO BTECH CSE-II AND IT

QUERIE 5: Find the names of sailors who have reserved a red boat? SQL> SELECT S.sname FROM sailors S WHERE S.sid IN (SELECT R.sid FROM reserves R WHERE R.bid IN (SELECT B.bid FROM boats B WHERE B.color='red')); SNAME -------------------Dustin Lubber Horatio QUERIE 6: Find the names of sailors who have not reserved a red boat?

SQL> SELECT S.sname FROM sailors S WHERE S.sid NOT IN (SELECT R.sid FROM reserves R WHERE R.bid IN (SELECT B.bid FROM boats B WHERE B.color='red')); SNAME -------------------Brutus Andy Rusty Zorba Horatio Art BOB 7 rows selected. QUERIE 7: Find sailors whose rating is better than some sailor called Horatio? SQL> SELECT S.sid FROM sailors S WHERE S.rating > ANY (SELECT S1.rating FROM sailors S1 WHERE S1.sname='Horatio'); PRASAD SIR
4

prasad17gumpina@gmail.com

DBMS LAB MANUAL

COMMON TO BTECH CSE-II AND IT

SID ---------58 71 74 31 32 QUERIE 8: Find the sailors with the highest rating? SQL> SELECT S.sid FROM sailors S WHERE S.rating >= ALL (SELECT S1.rating FROM sailors S1); SID ---------58 71 QUERIE 9: Find the names of sailors who have reserved both a red and a green boat? SQL> 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 S.sid IN ( SELECT S1.sid FROM sailors S1, reserves R1, boats B1 WHERE S1.sid=R1.sid AND R1.bid=B1.bid AND B1.color='green'); SNAME -------------------Dustin Dustin Lubber Lubber QUERIE 10: Find the names of sailors who have reserved boat no 103? SQL> SELECT S.sname FROM sailors S WHERE EXISTS (SELECT * FROM reserves R PRASAD SIR
5

prasad17gumpina@gmail.com

DBMS LAB MANUAL

COMMON TO BTECH CSE-II AND IT

WHERE R.bid=103 AND R.sid=S.sid); SNAME -------------------Dustin Lubber Horatio

QUERIE 11: Find the names of sailors who have reserved all boats? SQL> SELECT S.sname FROM sailors S WHERE NOT EXISTS (SELECT B.bid FROM boats B WHERE NOT EXISTS (SELECT R.bid FROM reserves R WHERE R.bid=B.bid AND R.sid=S.sid)); SNAME -------------------Dustin

PRASAD SIR
6

prasad17gumpina@gmail.com

Você também pode gostar