0% found this document useful (0 votes)
11 views11 pages

21 Nested Querypdf Lyst1722265628958

Uploaded by

gaurav kumar
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)
11 views11 pages

21 Nested Querypdf Lyst1722265628958

Uploaded by

gaurav kumar
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/ 11

SQL

Nested Query

-
Nested Query

Nested queries are often used to

perform complex queries by


embedding are
query within

another.

The outer query can apply some


conditions on the result of the
inner query
.
Scemes

Student ( rollno , name email mobile


- , ,

course ( consid cousename duration in


, ,

months
-

Batch ,
(batchid course id start date
,
-
----------- ,

time
,
days ,
sizes

Student-Batch (id
-
, Vollno bathid, joindate)
,
Populate names and email ids from
Student table where the roll no
of
student exists in student-batch
table where the batchid =

SELECT name email Student


From
,

WHERE Vallno IN

rollno from Student-Batch


(SELECT
WHERE batchid =
1)

Outer quely Inner Query


The outer query will use the
result of the inner query
.

SELECT name email Student


From
,

WHERE rollno In
(1 , 2
, 3) ;

vollnoWHERE batchid
from

=
Student-Batch
1)
Two Types of Nested Queries

Independent Nested
·
Quely
Corelated Nested
·
Query
Independent Nested Query
-
An outer quely won't be executed
until its inner query completes its

execution
.

- The outer quely uses the result of


the inner quely

-
various operations can be used in
independent nested queries :

EN ,
NOT IN ALL ,
ANY
,
SELECT column name

FROM tablename

WHERE column-name OPERATOR

C
SELECT column-name

FROM table name


WHERE conditio

C ;
SELECT name email Student
From
,

WHERE rollno IN

Vallno From Student-Batch


(SELECT
WHERE batchid =
1)
correlated Nested Query
The inner quely uses the values
from outer query to execute

The inner query for every now


processed by the outer avery
.

correlated queries run slowly because


the inner query is executed for every
row of the outer query's result
.
Select all the employees from
Employee table where salaries

are above the average salary of


employees in the same department
.

SELECT * FROM Employee el

WHERE Salary 7

C SELECT AVG (salary)


e2
FROM Employee
WHERE el .

department-e2
.
department
D ;

You might also like