0% found this document useful (0 votes)
64 views

Subquerylist

Subqueries, also known as inner queries or nested queries, allow querying results from one SQL statement inside another. There are three main types: single-row, multiple-row, and correlated. Single-row subqueries return one row and can be used with operators like =, <, etc. Multiple-row subqueries return multiple rows and use operators like IN, ANY, ALL. Correlated subqueries reference columns from the outer query and are re-evaluated for each row. Subqueries help solve problems like finding employees whose salary is higher than a specific employee's salary.

Uploaded by

Smily Smily
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Subquerylist

Subqueries, also known as inner queries or nested queries, allow querying results from one SQL statement inside another. There are three main types: single-row, multiple-row, and correlated. Single-row subqueries return one row and can be used with operators like =, <, etc. Multiple-row subqueries return multiple rows and use operators like IN, ANY, ALL. Correlated subqueries reference columns from the outer query and are re-evaluated for each row. Subqueries help solve problems like finding employees whose salary is higher than a specific employee's salary.

Uploaded by

Smily Smily
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Subquery or Inner Query or Nested Query is a query in a quary, A subquery is

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:

•Single Row Subqueries


•Multiple Row Subqueries
•Multiple Column Subqueries

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.

• ORDER BY clause is not supported for Nested Queries.

• You cannot use Between Operator.

• 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.

• A query can contain more than one sub-query.


Syntax:
SELECT <column, ..FROM <table> WHERE expression operator(SELECT <column, ...
FROM <table> WHERE <condition)
Single Row Subqueries
The single-row subquery returns one row. A special case is the scalar subquery,
which returns a single row with one column. Scalar subqueries are acceptable (and
often very useful) in virtually any situation where you could use a literal value,
a constant, or an expression.
The single row query uses any operator in the query .i.e. (=, <=, >= <>, <, >). If
any of the operators in the preceding table are used with a subquery that returns
more than one row, the query will fail.
🡪Suppose you want to find out the ename, job, sal of the employees whose salaries
are less than that of an employee whose empno= 7369 from obviously EMP table. Now
you need to perform two queries in order to get the desired result. In the first
query:
1) We will find out the salary of the employee whose empno=7369. The query is as
under:
SQL> SELECT SAL FROM EMP WHERE EMPNO=7876;
After execution it will return the salary of a given empno and it will be 1100.
2) Now in the second query we will apply the condition from which we will find the
ename, job, and sal. We will use the query as under
SQL> SELECT ENAME, JOB, SAL FROM EMP WHERE SAL<1100;
After execution we will get two records. The above two queries can be used as a
single query by using the concept of SUBQUERY. It will combine the result into a
single query as under:
SQL> SELECT ENAME, JOB, SAL FROM EMP WHERE SAL < (SELECT SAL FROM EMP WHERE
EMPNO=7876);
After execution you can see that the output will remain the same. First of all the
inner query will be performed and the result returned by it will be used by the
outer query to return the final result.
🡪Display the Ename, Job, Sal from EMP working in the LOC 'CHICAGO' and salary is
less than the salary of whose empno=7876.
SQL> SELECT ENAME, JOB, SAL FROM EMP WHERE DEPINO = (SELECT DEPTNO FROM DEPT WHERE
LOC='CHICAGO') AND SAL < (SELECT SAL FROM EMP WHERE EMPNO=7876);
Group Functions in the Subquery
To retrieve the details of the employee holding the minimum salary.
SQL> SELECT ENAME,JOB,SAL FROM EMP WHERE SAL=(SELECT MIN(SAL) FROM EMP;
To retrieve the details of employees whose salary is greater than the minimum
salary of the employees working in dept number 20 and also he is not working in
dept number 20.
SQL> SELECT ENAME, SAL FROM EMP WHERE SAL>(SELECT MIN(SAL) FROM EMP WHERE
DEPTNO=30)AND DEPTNO<>30;

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

Using a Subquery to Solve a Problem

"Who has a salary greater than Jones'?"

"Which employees have a salary greater


than Jones' salary?"

"What is Jones' salary?"

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).

A subquery is a SELECT statement that is embedded in a clause of another SELECT


statement. You can build powerful statements out of simple ones by using
subqueries. They can be very useful when you need to select rows from a table with
a condition that depends on the data in the table itself.
You can place the subquery in a number of SQL clauses
• WHERE clauss
• HAVING clause
• FROM clause
ln the syntax;
operator includes a comparison operatör such as >, =, or IN
Note: Comparison operutors fall iııto two classes: single-row operators
( > , = , >= , < , < > , <= )
and multiple-row operators ( IN , ANY , ALL ).

SELECT ename FROM EMP WHERE sal > ( SELECT sal


FROM emp WHERE empno=7566);

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 job, AVG(sal) FROM emp GROUP BY job HAVING AVG(sal) =


( SELECT MIN(AVG(sal)) FROM emp GROUP BY job );

SELECT ename, sal, deptno FROM emp WHERE sal IN (SELECT


MIN(sal) FROM emp GROUP BY deptno);
Using ANY Operator in Multiple-Row Subqueries

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%');

You might also like