SQL Subqueries
SQL Subqueries
Made Simple
Anish Chakravorty
Follow me on LinkedIn
What is a Subquery?
A subquery is simply a query within another
query.
It helps retrieve intermediate results to be
used in the main query.
1 Rohit HR 50,000
3 Vikram IT 70,000
5 Sunita HR 65,000
6 Aman IT 80,000
Single Row Subquery
A subquery that returns only one row as a
result.
Example:
Find the employee with the highest salary:
Result:
Example:
Find employees who work in the same
department as ‘Rohit’:
Result:
Example:
Find employees whose salary is above the
average salary of their department:
Result:
Example:
Find the employee with the second-highest
salary:
Result:
Example:
Display each employee's salary alongside the
company’s average salary:
Result:
Company
Name Salary
AvgSalary