0% found this document useful (0 votes)
34 views7 pages

Subquery: Gayathri Dollani

A subquery is a query embedded within another SQL query, used for intermediate calculations or data retrieval. There are four types of subqueries: single-row, multi-row, correlated, and nested, each serving different purposes in SQL operations. Examples illustrate how each type functions in practice.
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)
34 views7 pages

Subquery: Gayathri Dollani

A subquery is a query embedded within another SQL query, used for intermediate calculations or data retrieval. There are four types of subqueries: single-row, multi-row, correlated, and nested, each serving different purposes in SQL operations. Examples illustrate how each type functions in practice.
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/ 7

Gayathri Dollani

SUBQUERY

www.linkedin.com/in/gayathri--dollani
02

What is a
Subquery?
-Subquery is a query embedded within another
SQL query.
- Used for intermediate calculations or retrieving
data for the main query

www.linkedin.com/in/gayathri--dollani
03

Types of
Subqueries
1. SINGLE-ROW SUBQUERIES:
- Returns one value.

- Example:
SELECT name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM
employees)

www.linkedin.com/in/gayathri--dollani
04

2. MULTI-ROW SUBQUERIES:
- Returns multiple rows.

- Example with IN:


SELECT name
FROM employees
WHERE department_id IN (SELECT department_id
FROM departments WHERE location = 'New York’

www.linkedin.com/in/gayathri--dollani
05
3. CORRELATED SUBQUERIES:
- Depends on outer query values.
- Executes once per outer row.

- Example:
SELECT Name, Department, Salary
FROM Employees e1
WHERE Salary > (
SELECT AVG(Salary)
FROM Employees e2
WHERE e1.Department = e2.Department
)

www.linkedin.com/in/gayathri--dollani
06
4. NESTED SUBQUERIES
Subquery inside another subquery.

- Example:
SELECT name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees
WHERE department_id IN (SELECT
department_id FROM departments WHERE location =
'New York'))
)
-

www.linkedin.com/in/gayathri--dollani
DONT FORGET
TO LIKE, SHARE
AND SAVE IF
YOU LIKE THIS
POST

www.linkedin.com/in/gayathri--dollani

You might also like