0% found this document useful (0 votes)
8 views

SQL Subqueries

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SQL Subqueries

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Subquery

in

SQL
with
examples

@Mithilesh
Subquery is simply a query within another query

Let’s understand it using ‘Employees’ table

EmployeeID Name Department Salary

1 Rohit HR 50,000

2 Meera Sales 60,000

3 Vikram IT 70,000

4 Anjali Sales 55,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.

Result: Name Salary

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:

Name Salary CompanyAvgSalary

Rohit 50,000 63,333

Meera 60,000 63,333

Vikram 70,000 63,333

Anjali 55,000 63,333

Sunita 65,000 63,333

Ramesh 80,000 63,333

@Mithilesh
Found it interesting?
SELECT ❤ FROM ( Your Heart )

@Mithilesh

You might also like