0% found this document useful (0 votes)
167 views7 pages

DBMS LAB Exp 6

This document describes an experiment involving querying tables in a database using various operators and techniques. It includes: - Creating three tables: SAILORS, BOATS, and RESERVES with sample data - Examples of queries using DISTINCT, UNION, INTERSECT, EXCEPT, IN, EXISTS, NOT EXISTS, and nested queries - Examples comparing values using ANY, ALL, and set comparison operators

Uploaded by

sri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views7 pages

DBMS LAB Exp 6

This document describes an experiment involving querying tables in a database using various operators and techniques. It includes: - Creating three tables: SAILORS, BOATS, and RESERVES with sample data - Examples of queries using DISTINCT, UNION, INTERSECT, EXCEPT, IN, EXISTS, NOT EXISTS, and nested queries - Examples comparing values using ANY, ALL, and set comparison operators

Uploaded by

sri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DBMS LAB :: EXPERIMENT 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)

• CREATE TABLE SAILORS ( SID NUMBER(5), SNAME VARCHAR2(30),


RATING NUMBER(5), AGE NUMBER(4,2), PRIMARY KEY(SID) );
• CREATE TABLE BOATS( BID NUMBER(3) PRIMARY KEY, BNAME
VARCHAR(20), COLOR VARCHAR(20))

• CREATE TABLE RESERVES (SID NUMBER(3), BID NUMBER(3), DAY DATE,


PRIMARY KEY(SID,BID,DAY), FOREIGN KEY(SID) REFERENCES SAILORS, FOREIGN
KEY(BID) REFERENCES BOATS)

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);

Insert data in BOATS Table


INSERT INTO BOATS VALUES(1,'GANGA', 'RED');
INSERT INTO BOATS VALUES(2,'JAMUNA', 'GREEN');
INSERT INTO BOATS VALUES(3,'KAVERI', 'PINK');
INSERT INTO BOATS VALUES(4,'GODAVARI', 'RED');
INSERT INTO BOATS VALUES(5,'KRISHNA', 'BLUE' );

Insert data in RESERVES Table


INSERT INTO RESERVES VALUES(1,1,'12-FEB-2017');
INSERT INTO RESERVES VALUES(1,2, '12-FEB-2017');
INSERT INTO RESERVES VALUES(2,1,'13-FEB-2017‘);
INSERT INTO RESERVES VALUES(3,2, '14-FEB-2017');
INSERT INTO RESERVES VALUES(3,3,'14-FEB-2017');
Experiment 3: Working with Queries and Nested QUERIES
Queries (along with sub Queries) using ANY, ALL, IN, EXISTS, NOTEXISTS, UNION,
INTERSET, MINUS.

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.

UNION, INTERSECT, EXCEPT (MINUS) :- SQL provides three set-manipulation constructs


that extend the basic query form. Since the answer to a query is a multiset of rows, it is natural to
consider the use of operations such as union, intersection, and difference.
SQL supports these operations under the names UNION, INTERSECT and MINUS.
Note : UNION, INTERSECT, and MINUS can be used on any two tables that are Union-
Compatible, that is, have the same number of columns and the columns, taken in order, have the
same data types.

UNION :- It is a set operator used as alternative to OR query.


Here is an example of Query using OR.
Ex:- Find the names of sailors who have 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');
Output :

Same query can be written using UNION as follows.


SELECT S.SNAME FROM SAILORS S, RESERVES R, BOATS B WHERE S.SID = R.SID
AND R.BID = B.BID AND B.COLOR = 'RED'
UNION
SELECT S2.SNAME FROM SAILORS S2, BOATS B2, RESERVES R2
WHERE S2.SID = R2.SID AND R2.BID = B2.BID AND B2.COLOR =
'GREEN';
This query says that we want the union of the set of sailors who have reserved red boats and the set
of sailors who have reserved green boats.
Output :

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 :

Same query can be written using INTERSECT as follows.


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 S2.SNAME FROM SAILORS S2, BOATS B2, RESERVES R2 WHERE S2.SID =
R2.SID AND R2.BID = B2.BID AND B2.COLOR = 'GREEN';
Output :
EXCEPT (MINUS) :- It is a set operator used as set-difference. Our next query illustrates the set-
difference operation.
Ex:- Find the SID of all sailors who have reserved red boats but not green boats.
SELECT R1.SID FROM BOATS B1, RESERVES R1 WHERE R1.BID = B1.BID AND
B1.COLOR = 'RED'
MINUS
SELECT R2.SID FROM BOATS B2, RESERVES R2 WHERE R2.BID = B2.BID AND
B2.COLOR = ‘GREEN’;
Output :
NESTED QUERIES:-
For retrieving data from the tables we have seen the simple & basic queries. These queries extract
the data from one or more tables. Here we are going to see some complex & powerful queries that
enables us to retrieve the data in desired manner. One of the most powerful features of SQL is
nested queries. A nested query is a query that has another query embedded within it; the embedded
query is called a subquery.

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 :

NOT IN Operator :- The NOT IN is used in a opposite manner to IN.


Ex:- Find the names of sailors who have not reserved boat 103 using NOT IN Operator.
SELECT S.SNAME FROM SAILORS S WHERE S.SID
NOT 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.

SELECT S.SNAME FROM SAILORS S WHERE NOT EXISTS


(SELECT * FROM RESERVES R WHERE R.BID = 103 AND R.SID =
S.SID); Output :

Set-Comparison Operators:- We have already seen the set-comparison operators EXISTS, IN


along with their negated versions. SQL also supports op ANY and op ALL, where op is one of the
arithmetic comparison operators {<, <=, =, <>, >=, >}. Following are the example which illustrates
the use of these Set-Comparison Operators.

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 :

You might also like