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