Sub Queries
What is a Sub-query?
 A Sub-query is a query within another SQL query
embedded within the WHERE clause.
 The sub-query (inner query) executes once before the main
query.
 The result of the sub-query is used by the main query
(outer query).
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR (SELECT column_name [, column_name ]
FROM table1 [, table2 ] [WHERE] )
Syntax
Inner query
Outer query
Using Sub-query to Solve a Problem
“Which section has more capacity than 385 sec 2?”
Main Query
“Which section has more capacity than
385 sec 2?”
Sub-query
“What is the capacity of 385 sec 2?”
Using Sub-query to Solve a Problem
Examples:
“Who has a salary greater than Jones’?”
“Which employees have a salary greater than
Jones’ salary?
“What is Jones’ salary?”
Main Query
Sub-query
Using a Sub-query
The inner query determines the salary of employee 7566.
The outer query takes the result of the inner query and
uses this result to display all the employees who earn
more than this amount.
SQL> SELECT ename
2 FROM emp
3 WHERE sal > (SELECT sal
4 FROM emp
5 WHERE empno=7566);
ENAME
----------
SCOTT
KING
FORD
2975
Guidelines for Using Sub-queries
 Enclose sub-queries in parentheses.
 Place sub-queries on the right side of the comparison
operator.
 Do not add an ORDER BY clause to a Sub-query.
 Use single-row operators with single-row sub-queries.
 Use multiple-row operators with multiple-row
sub-queries.
Types of Sub-queries
 Single-row subquery
 Multiple-row subquery
 Multiple-column subquery
Main query
Subquery
returns
Main query
Subquery
returns
Main query
Subquery
returns
AHMED
AHMED
ISA
AHMED 24000
ISA 15000
Single-Row Sub-queries
 Returns only one row.
 Use single-row comparison operators
Meaning
Operator
Equal to
=
Greater than
>
Greater than or
equal
>=
Less than
<
Less than or equal
<=
Not equal to
<>
Executing Single-Row Subqueries
 Example:
Display employees whose job title is the same as that
of employee 7369 and whose salary is greater than
that of employee 7876.
Answer
CLERK
1100
Using Group Functions in a Sub-query
Display the employee name and job of the
employee who earns the minimum salary.
SQL> SELECT ename, job, sal
FROM emp
WHERE sal =
(SELECT MIN(sal)
FROM emp);
ENAME JOB SAL
-------- -------- ----------
SMITH CLERK 800
800
HAVING Clause with Sub-queries
 The Oracle Server executes sub-queries first.
 The Oracle Server returns results into the HAVING
clause of the main query.
 We can use sub-queries not only in the WHERE
clause, but also in the HAVING clause.
HAVING Clause with Sub-queries
 Find the Job with the lowest average salary.
What is Wrong
with This Statement?
 Display the employees’ numbers and names for
those who earn the lowest salary in each
department.
Multiple-Row Sub-queries
 Return more than one row
 Use multiple-row comparison operators
Meaning
Operator
Equal to any member in the list
IN
Compare value to each value returned by
the sub-query
ANY
Compare value to every value returned by
the sub-query
ALL
Multiple-Row Sub-queries
Using ANY Operator
in Multiple-Row Sub-queries
SQL> SELECT empno, ename, job
2 FROM emp
3 WHERE sal < ANY
4 (SELECT sal
5 FROM emp
6 WHERE job = ‘CLERK’)
7 AND job <> ‘CLERK’;
EMPNO ENAME JOB
----- ---------- -----------
7654 MARTIN SALESMAN
7521 WARD SALESMAN
The example displays employees whose salary is less than any clerk and
who are not clerks.
The maximum salary that a clerk earns is $1300. The SQL statement
displays the employees who are not clerks but earn less than $1300.
(1300, 1100, 800, 950)
Multiple-Row Subqueries
Example:
Display employees (number, name, and job) whose
salary is greater than the average salaries of all the
departments.
(1566.667, 2175, 2916.667)
Summary
Sub-queries are useful when a query is Based on
unknown values.
SELECT select_list
FROM table
WHERE expression operator
(SELECT select_list
FROM table);

Lab5 sub query

  • 1.
  • 2.
    What is aSub-query?  A Sub-query is a query within another SQL query embedded within the WHERE clause.  The sub-query (inner query) executes once before the main query.  The result of the sub-query is used by the main query (outer query). SELECT column_name [, column_name ] FROM table1 [, table2 ] WHERE column_name OPERATOR (SELECT column_name [, column_name ] FROM table1 [, table2 ] [WHERE] ) Syntax Inner query Outer query
  • 3.
    Using Sub-query toSolve a Problem “Which section has more capacity than 385 sec 2?” Main Query “Which section has more capacity than 385 sec 2?” Sub-query “What is the capacity of 385 sec 2?”
  • 4.
    Using Sub-query toSolve a Problem Examples: “Who has a salary greater than Jones’?” “Which employees have a salary greater than Jones’ salary? “What is Jones’ salary?” Main Query Sub-query
  • 5.
    Using a Sub-query Theinner query determines the salary of employee 7566. The outer query takes the result of the inner query and uses this result to display all the employees who earn more than this amount. SQL> SELECT ename 2 FROM emp 3 WHERE sal > (SELECT sal 4 FROM emp 5 WHERE empno=7566); ENAME ---------- SCOTT KING FORD 2975
  • 6.
    Guidelines for UsingSub-queries  Enclose sub-queries in parentheses.  Place sub-queries on the right side of the comparison operator.  Do not add an ORDER BY clause to a Sub-query.  Use single-row operators with single-row sub-queries.  Use multiple-row operators with multiple-row sub-queries.
  • 7.
    Types of Sub-queries Single-row subquery  Multiple-row subquery  Multiple-column subquery Main query Subquery returns Main query Subquery returns Main query Subquery returns AHMED AHMED ISA AHMED 24000 ISA 15000
  • 8.
    Single-Row Sub-queries  Returnsonly one row.  Use single-row comparison operators Meaning Operator Equal to = Greater than > Greater than or equal >= Less than < Less than or equal <= Not equal to <>
  • 9.
    Executing Single-Row Subqueries Example: Display employees whose job title is the same as that of employee 7369 and whose salary is greater than that of employee 7876.
  • 10.
  • 11.
    Using Group Functionsin a Sub-query Display the employee name and job of the employee who earns the minimum salary. SQL> SELECT ename, job, sal FROM emp WHERE sal = (SELECT MIN(sal) FROM emp); ENAME JOB SAL -------- -------- ---------- SMITH CLERK 800 800
  • 12.
    HAVING Clause withSub-queries  The Oracle Server executes sub-queries first.  The Oracle Server returns results into the HAVING clause of the main query.  We can use sub-queries not only in the WHERE clause, but also in the HAVING clause.
  • 13.
    HAVING Clause withSub-queries  Find the Job with the lowest average salary.
  • 14.
    What is Wrong withThis Statement?  Display the employees’ numbers and names for those who earn the lowest salary in each department.
  • 15.
    Multiple-Row Sub-queries  Returnmore than one row  Use multiple-row comparison operators Meaning Operator Equal to any member in the list IN Compare value to each value returned by the sub-query ANY Compare value to every value returned by the sub-query ALL
  • 16.
  • 17.
    Using ANY Operator inMultiple-Row Sub-queries SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal < ANY 4 (SELECT sal 5 FROM emp 6 WHERE job = ‘CLERK’) 7 AND job <> ‘CLERK’; EMPNO ENAME JOB ----- ---------- ----------- 7654 MARTIN SALESMAN 7521 WARD SALESMAN The example displays employees whose salary is less than any clerk and who are not clerks. The maximum salary that a clerk earns is $1300. The SQL statement displays the employees who are not clerks but earn less than $1300. (1300, 1100, 800, 950)
  • 18.
    Multiple-Row Subqueries Example: Display employees(number, name, and job) whose salary is greater than the average salaries of all the departments. (1566.667, 2175, 2916.667)
  • 19.
    Summary Sub-queries are usefulwhen a query is Based on unknown values. SELECT select_list FROM table WHERE expression operator (SELECT select_list FROM table);