0% found this document useful (0 votes)
10 views27 pages

Unit 2 Subquery

A subquery is a SQL query nested within another query, commonly found in SELECT, FROM, or WHERE clauses. It executes first, allowing its results to be used by the outer query, and can be categorized into single-row, multiple-row, and multiple-column subqueries. Guidelines for using subqueries include enclosing them in parentheses and using appropriate comparison operators based on the expected number of results.

Uploaded by

pandyaj036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views27 pages

Unit 2 Subquery

A subquery is a SQL query nested within another query, commonly found in SELECT, FROM, or WHERE clauses. It executes first, allowing its results to be used by the outer query, and can be categorized into single-row, multiple-row, and multiple-column subqueries. Guidelines for using subqueries include enclosing them in parentheses and using appropriate comparison operators based on the expected number of results.

Uploaded by

pandyaj036
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

2.

2
Subqueries

Prepared By:-Darshana V.Halatwala


1
Subquery

A subquery is a SQL query nested inside a larger


query.
A subquery may occur in :
- A SELECT clause
- A FROM clause
- A WHERE clause
The subquery can be nested inside a SELECT,
INSERT, UPDATE, or DELETE statement or inside
another subquery.

Prepared By:-Darshana V.Halatwala 2


Continue…..
A subquery is usually added within the WHERE Clause of another SQL
SELECT statement.
You can use the comparison operators, such as >, <, or =. The
comparison operator can also be a multiple-row operator, such as IN,
ANY, or ALL.
A subquery is also called an inner query or inner select, while the
statement containing a subquery is also called an outer query or outer
select.
The inner query executes first before its parent query so that the results
of an inner query can be passed to the outer query.
.

Prepared By:-Darshana V.Halatwala 3


Syntax:-

The syntax for a subquery when the embedded SQL


statement is part of the WHERE condition is as follows:

SELECT "column_name1"
FROM "table_name1"
WHERE "column_name2" [Comparison Operator]
(SELECT "column_name3"
FROM "table_name2"
WHERE "condition");

[Comparison Operator] could be equality operators such as =,


>, <, >=, <=. It can also be a text operator such as "LIKE". The
portion in white is considered as the "inner query," while the
portion in yellow is considered as the "outer query."

Prepared By:-Darshana V.Halatwala 4


Continue…
You can use a subquery in a SELECT, DELETE, or UPDATE
statement to perform the following tasks:
Compare an expression to the result of the query.
Determine if an expression is included in the results of the query.
Check whether the query selects any rows.

Syntax :
Select select_list from table where expr opearator
(select select_list from table);

The subquery (inner query) executes once before the main query
(outer query) executes.
The main query (outer query) use the subquery
result.
Prepared By:-Darshana V.Halatwala 5
Using a Subquery
to Solve a Problem
“Who has a salary greater than Jones’?”
Main Query

“Which employees have a salary greater


? than Jones’ salary?”

Subquery

?
“What is Jones’ salary?”

Prepared By:-Darshana V.Halatwala 6


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 (outer query).

Prepared By:-Darshana V.Halatwala 7


Subqueries
A subquery is a SELECT statement that is embedded in a clause
of another SELECT statement.
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 clause
HAVING clause
FROM clause

Prepared By:-Darshana V.Halatwala 8


Using a Subquery
SQL> SELECT ename
2 FROM emp 2975
3 WHERE sal >
4 (SELECT sal
5 FROM emp
6 WHERE empno=7566);

In the example, 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.

ENAME

KING
FORD
SCOTT

Prepared By:-Darshana V.Halatwala 9


Guidelines for Using
Subqueries
⚫ Enclose subqueries in parentheses.
⚫ Place subqueries on the right side of the
comparison operator.
⚫ Do not add an ORDER BY clause to a
subquery.
⚫ Use single-row operators with single-row
subqueries.
⚫ Use multiple-row operators with multiple-
row subqueries.

Prepared By:-Darshana 10
Types of Subqueries
⚫ Single-row subquery : Queries that return only one row from the
inner SELECT statement
Main query
returns
Subquery CLERK
• Multiple-row subquery Queries that return more than one row from the
inner SELECT statement
Main query
returns
CLERK
Subquery
MANAGER
• Multiple-column subqueryQueries that return more than
one column from the inner SELECT statement
Main query
returns CLERK 7900
Subquery
MANAGER 7698
Prepared By:-Darshana V.Halatwala 11
Single-Row Subqueries
⚫ Return only one row
⚫ Use single-row comparison operators

Operator Meaning

= Equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

<> Not equal to

Prepared By:-Darshana V.Halatwala 12


Display the employees whose job title is the same as that of
employee 7369.
S QL > SELECT e n a m e , job
2 FROM emp
3 WHERE j ob =
4 (SELECT job
5 FROM emp
6 WHERE e m p n o = 7369);

ENAME JOB

JAMES CLERK
SMITH CLERK
ADAMS CLERK
MILLER CLERK
Prepared By:-Darshana V.Halatwala 13
Example:-
Find the employee whose salary is
second highest
Select max(sal) from emp where
sal<(select max(Sal) from emp);
Third highest:-
Select max(sal) from emp where sal<(
select max(sal) from emp where
sal<(select max(Sal) from emp));

Prepared By:-Darshana V.Halatwala 14


Executing Single-Row Subqueries
The example on the slide displays employees whose
job title is the same as that of employee 7369 and
whose salary is greater than that of employee
7876.
SQL> SELECT ename, job
2 FROM emp
3 WHERE job = CLERK
4 (SELECT job
5 FROM emp
6 WHERE empno = 7369)
7 AND sal > 1100
8 (SELECT sal
9 FROM emp
10 WHERE empno = 7876);

ENAME JOB

MILLER CLERK
Prepared By:-Darshana V.Halatwala 15
Using Group Functions
in a Subquery
Display the employee name, job title, and salary of all employees
whose salary is equal to the minimum salary.

SQL> SELECT ename, job, sal


2 FROM emp 800
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp);

ENAME JOB SAL

SMITH CLERK 800

Prepared By:-Darshana V.Halatwala 16


Subqueries

The SQL statement on the slide displays all the departments that
have a minimum salary greater than that of department 20.

SELECT * FROM departments


WHERE salary
> (SELECT min(salary) FROM departments
WHERE dep_id = 20);

Prepared By:-Darshana V.Halatwala 17


What Is Wrong
with This Statement?
SQL> SELECT empno, ename
2 FROM emp
3 WHERE sal =
4 (SELECT MIN(sal)
5 FROM emp
6 GROUP BY deptno);

ERROR:
ORA-01427: single-row subquery returns more than
one row

no rows selected

Prepared By:-Darshana V.Halatwala 18


Errors with Subqueries
One common error with subqueries is more than one row
returned for a single-row subquery.
In the SQL statement on the slide, the subquery contains a
GROUP BY (deptno) clause, which implies that the
subquery will return multiple rows, one for each group it
finds. In this case, the result of the subquery will be 800,
1300, and 950.
The outer query takes the results of the subquery (800, 950,
1300) and uses these results in its WHERE clause. The
WHERE clause contains an equal (=) operator, a single-
row comparison operator expecting only one value. The
= operator cannot accept more than one value from the
subquery and hence generates the error.
Prepared By:-Darshana 19
To correct this error, change the = operator to IN.
Multiple-Row Subqueries
⚫ Return more than one row
⚫ Use multiple-row comparison operators

Operator Meaning

IN Equal to any member in the list

ANY Compare value to each value returned by


the subquery

Compare value to every value returned by


ALL
the subquery

Prepared By:-Darshana V.Halatwala 20


IN Opeartor

Syntax:-
SELECT column
FROM table1
WHERE column IN (
SELECT column
FROM table2
);

Prepared By:-Darshana V.Halatwala 21


Example IN Operator

Table 1:- Emp


Eid(1,2,3,4,5),Ename,Address
Table 2:-project
Eid(1,5,3,4),pid(p1,p2,p3,p4),pname,locat ion

Query:-
Find the name of employees who are working on a project?
Select ename from emp where Eid IN (select Distinct(Eid)
from project);
Prepared By:-Darshana V.Halatwala 22
ANY Operator
Syntax:-
SELECT column
FROM table1
WHERE column ANY (
SELECT column
FROM table2
);

Prepared By:-Darshana V.Halatwala 23


Using ANY Operator in Multiple-Row Subqueries
Display the employees whose salary is less
than any clerk and who are not clerks.
SQL> SELECT empno, ename, job 1300
2 FROM emp 1100
800
3 WHERE sal < ANY 950
4 (SELECT sal
5 FROM emp
6 WHERE job = 'CLERK')
7 AND job <> 'CLERK';

EMPNO ENAME JOB

7654 MARTIN SALESMAN


7521 WARD SALESMAN

<ANY means less than the maximum. >ANY means more


than the minimum. =ANY is equivalent to IN.
Prepared By:-Darshana V.Halatwala 24
ALL Operator
Synatax:-
SELECT column
FROM table1
WHERE column ALL (
SELECT column
FROM table2
);

Prepared By:-Darshana V.Halatwala


25
Using ALL Operator
in Multiple-Row Subqueries
Display the employees whose salary is greater than
the average salaries of all the departments.
SQL> SELECT empno, ename, job 1566.6667
2 FROM emp 2175

3 WHERE sal > ALL 2916.6667

4 (SELECT avg(sal)
5 FROM emp
6 GROUP BY deptno);

EMPNO ENAME JOB

7839 KING PRESIDENT


7566 JONES MANAGER
7902 FORD ANALYST
7788 SCOTT ANALYST
>ALL means more than the maximum and <ALL means less than the
minimum.The NOT operator can be used with IN, ANY, and ALL operators.
Prepared By:-Darshana V.Halatwala 26
Summary
Subqueries are useful when a query
is based on unknown values.
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);

Prepared By:-Darshana V.Halatwala 27

You might also like