DBMS LAB Exp 6
DBMS LAB Exp 6
Querying (using ANY, ALL, IN, Exists, NOT EXISTS, UNION, INTERSECT, Constraintsetc.)
Following Tables (Relations) are considered for the lab experiment purpose.
• SAILORS (SID:INTEGER, SNAME:STRING, RATING:INTEGER, AGE:REAL)
• BOATS (BID:INTEGER, BNAME:STRING, COLOR:STRING)
• RESERVES (SID:INTEGER, BID:INTEGER, DAY:DATE)
By using one of these methods we can add records or data to Sailors, Boats as well as Reserves
Table.
Insert data in SAILORS Table
INSERT INTO SAILORS VALUES(1,'VIJAY', 9, 36);
INSERT INTO SAILORS VALUES(2,'RAJESH', 10, 25);
INSERT INTO SAILORS VALUES(3,'MOHAN', 8, 23);
INSERT INTO SAILORS VALUES(4,'KUMAR', 7, 28);
INSERT INTO SAILORS VALUES(5,'SAGAR', 9, 21);
INSERT INTO SAILORS VALUES(6,'MAHESH', 9, 36);
DISTINCT Keyword :- The DISTINCT keyword eliminates the duplicate tuples from the result
records set.
Ex:- Find the Names and Ages of all sailors.
SELECT DISTINCT S.SNAME, S.AGE FROM SAILORS S;
Output :
The answer is a set of rows, each of which is a pair (sname, age). If two or more sailors have the
same name and age, the answer still contains just one pair with that name and age.
Find all sids of sailors who have a rating of 10 or reserved boat number 1.
SELECT S.SID FROM SAILORS S WHERE S.RATING = 10
UNION
SELECT R.SID FROM RESERVES R WHERE R.BID = 1;
Output :
INTERSECT :- It is a set operator used as alternative to AND query. Here is an example of Query
using AND.
Ex:- Find the names of sailor's who have reserved both a red and a green boat.
SELECT S.SNAME FROM SAILORS S, RESERVES R1, BOATS B1,
RESERVES R2, BOATS B2 WHERE S.SID = R1.SID AND R1.BID =
B1.BID AND S.SID = R2.SID AND R2.BID = B2.BID AND
B1.COLOR='RED' AND B2.COLOR = 'GREEN';
Output :
IN Operator :- The IN operator allows us to test whether a value is in a given set of elements; an
SQL query is used to generate the set to be tested.
Ex:- Find the names of sailors who have reserved boat 103 using IN Operator.
SELECT S.SNAME FROM SAILORS S WHERE S.SID
IN (SELECT R.SID FROM RESERVES R WHERE R.BID = 103 );
Output :
EXISTS Operator :- This is a Correlated Nested Queries operator. The EXISTS operator is another
set comparison operator, such as IN. It allows us to test whether a set is nonempty, an implicit
comparison with the empty set.
Ex:- Find the names of sailors who have reserved boat number 103 using EXISTS Operator.
SELECT S.SNAME FROM SAILORS S WHERE
EXISTS (SELECT * FROM RESERVES R WHERE R.BID = 103 AND R.SID = S.SID );
Output :
NOT EXISTS Operator :- The NOT EXISTS is used in a opposite manner to EXISTS. Ex:- Find
the names of sailors who have not reserved boat number 103 using NOT EXISTS Operator.
op ANY Operator :- It is a comparison operator. It is used to compare a value with any of element
in a given set.
Ex:- Find sailors whose rating is better than some sailor called Rajesh using ANY Operator.
SELECT S.SID FROM SAILORS S WHERE S.RATING > ANY (SELECT S2.RATING
FROM SAILORS S2 WHERE S2.SNAME = ' RAJESH ' );
Note that IN and NOT IN are equivalent to = ANY and <> ALL, respectively.
Output :
op ALL Operator :- It is a comparison operator. It is used to compare a value with all the elements
in a given set.
Ex:- Find the sailor's with the highest rating using ALL Operator.
SELECT S.SID FROM SAILORS S WHERE S.RATING >= ALL ( SELECT S2.RATING
FROM SAILORS S2 )
Output :