0% found this document useful (0 votes)
23 views3 pages

Subqueries Qs

Uploaded by

sreeni.thummana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

Subqueries Qs

Uploaded by

sreeni.thummana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a query to find the salary of employees whose salary is greater than the salary of employee

whose id is 100?
SELECT EMPLOYEE_ID,
SALARY
FROM EMPLOYEES
WHERE SALARY >
(
SELECT SALARY
FROM EMPLOYEES
WHERE EMPLOYEED_ID = 100
)

2. Write a query to find the employees who all are earning the highest salary?
SELECT EMPLOYEE_ID,
SALARY
FROM EMPLOYEES
WHERE SALARY =
(
SELECT MAX(SALARY)
FROM EMPLOYEES
)

3. Write a query to find the departments in which the least salary is greater than the highest
salary in the department of id 200?
SELECT DEPARTMENT_ID,
MIN(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING MIN(SALARY) >
(
SELECT MAX(SALARY)
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 200
)

Multiple Row Subquery Examples

1. Write a query to find the employees whose salary is equal to the salary of at least one
employee in department of id 300?
SELECT EMPLOYEE_ID,
SALARY
FROM EMPLOYEES
WHERE SALARY IN
(
SELECT SALARY
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 300
)

2. Write a query to find the employees whose salary is greater than at least on employee in
department of id 500?
SELECT EMPLOYEE_ID,
SALARY
FROM EMPLOYEES
WHERE SALARY > ANY
(
SELECT SALARY
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 500
)

3. Write a query to find the employees whose salary is less than the salary of all employees in
department of id 100?
SELECT EMPLOYEE_ID,
SALARY
FROM EMPLOYEES
WHERE SALARY < ALL
(
SELECT SALARY
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 100
)

4. Write a query to find the employees whose manager and department should match with the
employee of id 20 or 30?
SELECT EMPLOYEE_ID,
MANAGER_ID,
DEPARTMENT_ID
FROM EMPLOYEES
WHERE (MANAGER_ID,DEPARTMENT_ID) IN
(
SELECT MANAGER_ID,
DEPARTMENT_ID
FROM EMPLOYEES
WHERE EMPLOYEE_ID IN (20,30)
)

5. Write a query to get the department name of an employee?


SELECT EMPLOYEE_ID,
DEPARTMENT_ID,
(SELECT DEPARTMENT_NAME
FROM DEPARTMENTS D
WHERE D.DEPARTMENT_ID = E.DEPARTMENT_ID
)
FROM EMPLOYEES E
Correlated SubQueries Examples

Correlated sub query is used for row by row processing. The sub query is executed for each row
of the main query.

1. Write a query to find the highest earning employee in each department?


SELECT DEPARTMENT_ID,
EMPLOYEE_ID,
SALARY
FROM EMPLOYEES E_0
WHERE 1 =
(
SELECT COUNT(DISTINCT SALARY)
FROM EMPLOYEES E_I
WHERE E_O.DEPARTMENT_ID = E_I.DEPARTMENT_ID
AND E_O.SALARY <= E_I.SALARY
)

2. Write a query to list the department names which have at lease one employee?
SELECT DEPARTMENT_ID,
DEPARTMENT_NAME
FROM DEPARTMENTS D
WHERE EXISTS
(
SELECT 1
FROM EMPLOYEES E
WHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID)

3. Write a query to find the departments which do not have employees at all?
SELECT DEPARTMENT_ID,
DEPARTMENT_NAME
FROM DEPARTMENTS D
WHERE NOT EXISTS
(
SELECT 1
FROM EMPLOYEES E
WHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID)

You might also like