Skip to content

Subqueries

SUBQUERIES :

A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within the WHERE clause.

A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be retrieved.

EX :

1
2
select * from emp where dept_no in (
select distinct deptno from dept) 

CORELATED SUBQUERIES :

If the subquery depends on the outer query for its values is calles as corelated subqueries.

Corelated subquery can not be executed independently of the outer query.

ex :

1
2
3
select ename,job from emp where dept_no in(
    select distinct deptno from dept where dept.deptno=emp.dept_no
)