How To Use Nested Queries In Oracle
Description
Oracle has an ability to nest queries within one another. A sub query is a select statement that is nested within another select statement and which returns intermediate results. SQL first evaluates the inner query within the where clause. The inner query generates values that are tested in the predict of the outer query, determining when it will be true. The return value of inner query is then substituted in the condition of the outer query.
- The result of inner query is dynamically substituted in the condition of outer query.
- There is no practical limitation to the level of nesting of queries in Oracle.
- When using relational operators, ensure that the subquery returns a single column output.
- In some cases, the DISTINCT clause can be used to ensure single valued output.
- Select Dept_no from Employee
- where Emp_name=’King’;
Example
Combine the above two queries
- Select Emp_name from Employee
- Where Dept_no=104;
Combine the above two queries
- Select emp_name from employee
- where Dept_no = (select Dept_no from Employee where Emp_name=’King’);
List the name of the employee which is the highest salary
- select emp_name from employee
- where salary = (select max (salary) from employee);
Using Aggregate Function in Subquery
Aggregate function produces single value for any number of rows. We want to see all employee detail salary is greater than average salary of employees whose hire date is before ‘01-SEP-08’.
For this we need to use aggregate function in inner query.
Example
- Select * from Employee
- where salary >
- (select avg(salary) from employee
- where Emp_hiredate< ‘01-SEP-08’);
Subquery with in having clause
You can also use subquery within the having clause.
these subqueries can use their own aggregate functions as long as they do not produce multiple values or use GROUP BY or HAVING themselves.
Example
- select Emp_job, AVG(salary)
- from Employee
- GROUP BY Emp_job
- HAVING AVG(salary) = (select max(AVG(salary)) from employee GROUP BY Emp_job);
Distinct Clause with Subqueries
Distinct clause is used in some cases to force a sub query to generate a single value. Suppose we want to find the details of the employee whose emp_id ‘7005’.
Example
- Select * from employee
- where emp_id = (select distinct emp_id from employee where dept_no = ‘105’);
Correlated subquery
A correlated subquery is a nested subquery which is executed once ‘candidate row’ considered by the main query and which on execution uses a value from a column in the outer query.
In a correlated subquery, the column value used in inner subquery refers to the column value present in the outer query forming a correlated subquery.
Example
- Select Emp_id, Emp_name, salary, dept_no
- from employee
- where salary > (select AVG(salary) from employee where dept_no = Dept_no);
Summary
Thus, we learnt, Oracle has an ability to nest queries within one another. A sub query is a select statement, which is nested within another select statement, which returns the intermediate results. We learnt, how to use the nested queries in Oracle with the examples.