SQL Subqueries
SQL Subqueries
in
SQL
with
examples
@Mithilesh
Subquery is simply a query within another query
1 Rohit HR 50,000
3 Vikram IT 70,000
5 Sunita HR 65,000
6 Ramesh IT 80,000
@Mithilesh
Single Row Subquery
Example:
Find the employee who has the highest salary.
Result: Name
Ramesh
@Mithilesh
Multi-Row Subquery
Example:
Find the employees who work in the same department
as 'Rajesh'.
Result: Name
Rajesh
Sunita
@Mithilesh
Correlated Subquery
Example:
Find employees whose salary is above the average salary
of their department.
Sunita 65,000
Meera 60,000
Ramesh 80,000
@Mithilesh
Nested Subquery
Example:
Find the name of the employee with the second-highest
salary.
Result: Name
Vikram
@Mithilesh
Scalar Subquery
Example:
Display the salary and the average salary in the company
for each employee.
Result:
@Mithilesh
Found it interesting?
SELECT ❤ FROM ( Your Heart )
@Mithilesh