You are on page 1of 5

NESTED QUERIES

AIM:
To create a table and perform nested queries using Oracle 8.0.

NESTED QUERIES:
Query inside another query is called as nested queries.
Syntax:
Select column 1, column 2 from table 1 where column 2 = (select column 2 from table 2);

NESTED SUB-QUERIES:
The sub query is a select from where expression (i.e.) nested within another query.
There are four categories.
1. Set membership
2. Set comparison
3. Test for empty relations
4. Test for absence of duplicate tuples

OUTPUT:
SQL> create table stud50(name varchar2(10), rollno number(10));
Table created.
SQL> select * from stud50;
NAME

ROLLNO

----------

---------

annanya

103

bhavna

107

chinmaye

108

tapsee

109

SQL> create table stud51(name varchar2(10),mark number(10));


Table created.
SQL> select * from stud51;
NAME

MARK

----------

---------

chinmayee

80

maya

75

annanya

62

siddhu

88

SQL> select name from stud50 where rollno in(select rollno from stud51 where mark>=75);
NAME
---------annanya
bhavna
chinmayee
tapsee

SQL> select rollno from stud50 where name not in(select name from stud51 where mark>=75);
ROLLNO
--------103
107
109
SQL> select name from stud51 where mark>all(select mark from stud51 where mark=80);
NAME
---------Siddhu
SQL> select name from stud51 where mark>some(select mark from stud51 where mark=62);
NAME
---------chinmayee
maya
siddhu
SQL> select rollno from stud50 where name not in(select name from stud51 where mark>=75);
ROLLNO
--------103
107
109

SQL> select name,rollno from stud50 where name=(select name from stud51 where mark<75);
NAME

ROLLNO

----------

---------

annanya

103

SQL> select * from stud50 where exists(select name from stud51 where mark=80);
NAME

ROLLNO

----------

---------

annanya

103

bhavna

107

chinmayee

108

tapsee

109

SQL> select * from stud50 where not exists(select name from stud51 where mark>90);
NAME

ROLLNO

----------

---------

annanya

103

bhavna

107

chinmayee

108

tapsee

109

RESULT:
Thus the nested query operation was executed successfully.

You might also like