0% found this document useful (0 votes)
53 views6 pages

S10 Correlated SubQuery

1) Correlated sub-queries depend on the outer query for values and are executed once for each row selected by the outer query. 2) Correlated sub-queries have an execution structure similar to nested loops, where the inner query is executed once for each row returned by the outer query. 3) Sample correlated sub-queries are shown that retrieve products sold over the average sales and employees earning more than the average salary of their department.

Uploaded by

Avudaiappan Ram
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)
53 views6 pages

S10 Correlated SubQuery

1) Correlated sub-queries depend on the outer query for values and are executed once for each row selected by the outer query. 2) Correlated sub-queries have an execution structure similar to nested loops, where the inner query is executed once for each row returned by the outer query. 3) Sample correlated sub-queries are shown that retrieve products sold over the average sales and employees earning more than the average salary of their department.

Uploaded by

Avudaiappan Ram
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/ 6

9/1/2015

Information Management

Session 10
Correlated Sub-Query

PGCBA | IM | CJK | 4

Objectives
After completing this session you will be able to
Define Correlated Sub-Query
Understand the execution structure of correlated sub-query

PGCBA | IM | JK | 4

9/1/2015

Review
Sub-Query
A select query that is used inside another query is called as a subquery
Sub-query usually appears inside the where clause or in the
select clause
Executes only once
Also known as inner query / nested query
Can be nested till 32 levels

PGCBA | IM | JK | 4

Correlated SubQuery
Definition
If the sub-query depends on the outer query for its
values, then it is called as a correlated sub-query
The correlated sub-query is executed once for every row
selected by the outer query
Correlated sub-query cannot be executed independently
without the outer query

PGCBA | IM | JK | 4

9/1/2015

Execution Structure
The query execution is similar to the nested loop
structure used in programming language
For I in 1 to 10 do
For J in 1 to 5 do
<instructions>
End for J
End for I

PGCBA | IM | JK | 4

Sample Tables
SALES

PRODUCT

PID

SOLD

PID

Name

Price

P99

P99

Plate

80

P77

P98

Cup

45

P99

P77

Bowl

60

P77

P33

Fork

20

P33

PGCBA | IM | JK | 4

9/1/2015

Simple Sub-query
1. Find the product that has not been sold
select pid, name, price from product where pid not in
(select distinct pid from sales)

PGCBA | IM | JK | 4

Correlated Sub-query in Select Clause


1. List the products sold and the total quantity sold
select pid, (select sum(sold) from sales where
sales.pid=product.pid) from product

Structure of Execution
for each pid from product select pid,
for all matching pids in sales
sum(sold) for the product
end sales
end product

PGCBA | IM | JK | 4

9/1/2015

Correlated Sub-query in Where Clause


2. List the products sold that have the sales value more than
the average sales value of products
SELECT pid, sold FROM sales s WHERE s.sold >
(SELECT avg(sold) FROM sales s1 WHERE s.pid = s1.pid)
ORDER BY s.pid, s.sold

PGCBA | IM | JK | 4

Correlated Sub-query in Where Clause


2. Retrieve the employees who earn more than the
average salary of employees in their departments
SELECT firstnme , salary FROM employee X WHERE
salary >
( SELECT avg( salary ) FROM employee Y WHERE
X.workdept = Y.workdept ) ORDER BY X.workdept,
X.salary)

PGCBA | IM | JK | 4

9/1/2015

Structure of Correlated sub-query Execution


for each employee in X select firstname, salary
for each employee in employee Y
where X.salary > avg(Y.salary) for same
department
end employee Y
end employee X

PGCBA | IM | JK | 4

Test Your Understanding


State if True or False.
1. The Inner query in a correlated sub-query executes
exactly once
2. Verify the syntactical & logical correctness of the query
SELECT s.pid, sold*price FROM sales s, product WHERE
s.sold >(SELECT avg(price) FROM product where
pid=s.pid)

PGCBA | IM | JK | 4

You might also like