Subquerylist
Subquerylist
usually added in the WHERE Clause of sql statement. Most of the time, a subquery is
used when you now how to search for a value using a SELECT statement, but do not
know the exact value.
A query result can be used in a condition of a Where clause. In such case, a query
is called a subquery and complete SELECT statement is called a nested query. We can
also used subquery can also be placed within HAVING clause. But subquery cannot be
used with ORDERBY clause.
Subqueries are queries nested inside other queries, marked off with parentheses,
and sometimes referred to as "inner" queries within "outer" queries. Most often,
you see subqueries in WHERE or HAVING clauses.
There are basically three types of subqueries are:
Things to Remember:
• Subqueries are queries nested inside other queries, marked off with parentheses.
• The result of inner query will pass to outer query for the preparation of final
result.
• Subqueries will always return only a single value for the outer query.
• A sub query must be put in the right hand of the comparison operator.
The following statement returns all departments in where the minimum salary is more
than the minimum salary in the department 20.
SQL> SELECT DEPTNO, MIN (SAL) FROM EMP GROUP BY DEPTNO HAVING MIN (SAL)> (SELECT
MIN (SAL)FROM EMP WHERE DEPTNO=20);
Display the detail of department whose manager Ecode='7698'.
SQL> SELECT * FROM DEPT WHERE DEPTNO = (SELECT DISTINCT DEPTNO FROM EMP WHERE
MGR=7698);
On execution the inner query will give the deptno whose manager's ecode='7698'.
Without distinct clause the inner query would have return more than one row as
there are number of employees whose manager ecode='7698'.
Multiple-Row Subqueries
Multiple-row subqueries return sets of rows. These queries are commonly used to
generate result sets that will be passed to a DML or SELECT statement for further
processing. Both single-row and multiple-row subqueries will be evaluated once,
before the parent query is run.
Since it returns multiple values, the query must use the set comparison operators
(IN, ALL, ANY). If you use a multi row sub query with the equals comparison
operators, the database will return an error if more than one row is returned. The
operators in the following table can use multiple-row subqueries:
IN Operator
The IN operator return TRUE, if the comparison value is contained in the list; in
this case, the results of the subquery.
The following statement finds the employees whose salary is the same as the minimum
salary of the employees in some department.
SQL> SELECT ENAME,SAL FROM EMP WHERE SAL IN (SELECT MIN (SAL) FROM EMP GROUP BY
DEPTNO);
On execution first of all the inner query will return the minimum salary of all the
departments and return to outer query. After getting the result from inner query it
will compare the result of inner query and return the number of employees
• List all the employees Name and Sal working at the location 'DALLAS'.
SQL> SELECT ENAME, SAL, JOB FROM EMP WHERE DEPTNO IN (SELECT DEPTNO FROM DEPT WHERE
LOC ='DAllAS');
Any Operator
The ANY operators work with the equal operators. The ANY operator returns TRUE if
the comparison value matches any of the values in the list.
• Display the employees whose salary is more than the maximum salary of the
employees in any department.
SQL> SELECT ENAME, SAL FROM EMP WHERE SAL) ANY (SELECT MAX (SAL) FROM EMP GROUP BY
DEPTNO);
Display the employees whose salary is greater than any 'manager' and he is not a
'manager'.
SQL> SELECT ENAME, JOB, SAL FROM EMP WHERE SAL> ANY (SELECT SAL FROM EMP WHERE
JOB='MANAGER') AND JOB <>'MANAGER';
All Operator
The ALL operator returns TRUE only if the comparison value matches all the values
in the list. It compares a value to every value returned by a query. The condition
evaluates to true if subquery doesn't yield any row.
Display the employee detail with salary less than those whose job is 'manager'.
SQL> SELECT empno, ename, job, sal FROM EMP WHERE sal < all (SELECT sal FROM EMP
WHERE job = 'MANAGER') AND job <> 'MANAGER';
On execution the query will first select the salary of employees who are
'managers', then it give the result to outer query and it will display the empno,
ename, job, salary of those employees who have less salary than any 'manager'.
Multiple – Column Subquery
A subquery that compares more than one column between the parent query and subquery
is called the multiple column subqueries. In multiple-column subqueries, rows in
the subquery results are evaluated in the main query in pair-wise comparison. That
is, column-to-column comparison and row-to-row comparison.
• List the employees that makes the same salary as other employee with empno=752l
with the same job also.
SQL> SELECT ENAME, JOB, SAL, EMPNO FROM EMP WHERE (JOB, SAL) IN (SELECT JOB, SAL
FROM EMP WHERE EMPNO=7521);
On execution it is clear that first of all it will select the job, sal from
employee table whose empno is 7521. then it handover the result to outer query and
because now we will use two columns for comparison so after comparing each row with
the empno 7521 it will return the desired result.
Correlated Subquery
A correlated subquery has a more complex method of execution than single- and
multiple-row subqueries and is potentially much more powerful. If a subquery
references columns in the parent query, then its result will be dependent on the
parent query. This makes it impossible to evaluate the subquery before evaluating
the parent query.
Consider this statement, which lists all employees who earn less than the average
salary:
SQL> Select Last_name from employees Where salary < (select avg (salary) from
employees);
The single-row subquery need only be executed once, and its result substituted into
the parent query.
Select the highest paid employee from the list of each department.
SQL> SELECT ENAME, SAL, DEPTNO FROM EMP E1 WHERE SAL = (SELECT MAX (SAL) FROM EMP
WHERE DEPTNO=E1.DEPTNO) ORDER BY DEPTNO;
The subquery will get executed for each row returned in the parent key. We used the
alias for the name inside the subquery.
The flow of execution is as follows:
• Start at the first row of the EMPLOYEES table.
• Read the DEPARTMENT_ID and SALARY of the current row.
• Run the subquery using the DEPARTMENT_ID from step 2.
• Compare the result of step 3 with the SALARY from step 2, and return the row if
the SALARY is less than the result.
• Advance to the next row in the EMPLOYEES table.
• Repeat from step 2.
A single-row or multiple-row subquery is evaluated once, before evaluating the
outer query; a correlated subquery must be evaluated once for every row in the
outer query. A correlated subquery can be single- or multiple-row, if the
comparison operator is appropriate.
Subqueries
Chapter 6
Objectives
After completing this lesson, you should be able to do the follovving:
• Describe the types of problems that subqueries can solve • Define subqueries
• List the types of subqueries
• Write single-row and multiple-row subqueries
Subqueries
SELECT select_list
FROM table
WHERE expr operator
( SELECT select_List
FROM table );
• The subquery (inner query) executes once before the main query.
• The result of the subquery is used by the main query (outerquery).
SELECT ename, sal, deptno, job FROM EMP WHERE job = ( SELECT job FROM emp WHERE
empno=7369);
SELECT ename, sal, deptno FROM EMP WHERE sal IN ( SELECT MIN(sal) FROM emp
GROUP BY deptno );
SELECT empno, ename, job FROM emp WHERE sal < ANY ( SELECT sal FROM emp
WHERE job = 'CLERK' );
SELECT empno, ename, job FROM emp WHERE sal < ANY ( SELECT sal FROM emp WHERE
job = 'CLERK' ) AND job <> 'CLERK' ;
SELECT empno, ename, job FROM emp WHERE sal > ALL ( SELECT sal FROM emp WHERE
job = 'CLERK' ) AND job <> 'CLERK' ;
SELECT empno, ename, job FROM emp WHERE sal > ALL ( SELECT AVG(sal) FROM emp
GROUP BY deptno ) ;
SELECT ename, sal, deptno FROM EMP WHERE sal IN ( SELECT MIN(sal) FROM emp GROUP
BY deptno ) ;
SELECT ename, sal, job FROM emp WHERE sal < ANY ( SELECT sal FROM emp
WHERE job = 'CLERK' ) AND job <> 'CLERK' ;
Using ALL Operator in Multiple-Row Subqueries
SELECT ename, sal, job FROM emp WHERE sal > ALL ( SELECT AVG(sal) FROM
emp GROUP BY deptno );
1. Write a query to display the employee name and hiredate for all employees in
the same department as Blake. Exclude Blake.
SELECT ename, hiredate FROM emp WHERE deptno = ( SELECT deptno FROM emp
WHERE ename = 'BLAKE') AND ename <> 'BLAKE';
Create a query to display the employee number and name for all employees who earn
more than the average salary. Sort the results in descending order of salary.
SELECT empno, ename FROM emp WHERE sal > ( SELECT AVG(sal) FROM emp );
Write a query to display the employee number and name for all employees who work in
a department with any employee whose name contains a T.
SELECT empno, ename FROM emp WHERE deptno IN ( SELECT deptno FROM emp
WHERE ename LIKE '%T%' );
4. Display the employee name, department number, and job title for all
employees whose department location is Dallas.
Solution with subquery:
SELECT ename, empno, job FROM emp WHERE deptno = (SELECT deptno FROM dept
WHERE loc ='DALLAS') ;
5. Display the employee name and salary of all employees who report to King.
Solution with subquery:
SELECT ename, sal FROM emp WHERE mgr = (SELECT empno
FROM emp WHERE ename = 'KING' );
6. Display the department number, name,, and job for all employees in the Sales
department.
SELECT e.deptno, e.ename, e.job , d.dname FROM emp e , dept d
WHERE e.deptno = d.deptno AND d.dname = 'SALES'
7. to display the employee number, name, and salary for all employees who earn
more than the average salary and who work in a department with any employee with a
T in their name. Rerun your query.
SELECT empno, ename , sal FROM emp WHERE sal > (SELECT AVG (sal) FROM
emp ) AND deptno IN ( SELECT deptno FROM emp
WHERE ename LIKE '%T%');