You are on page 1of 16

EX.

NO:6
USING SUBQUERIES TO SOLVE QUERIES
DATE:

Aim:

To Retrieve Using Subqueries To Solve Queries

Queries:

1. Display jobs where the minimum salary is less than salary of


employee 105.

Query:
select job_id from hr.jobs where min_salary<(select salary from
hr.employees where employee_id=105);

Output:

717822L602
2. Find those employees who receive a higher salary than the employee with
ID 163.

Query:
Select first_name, last_name ,employee_id,salary From hr.employees
WHERE salary > ( select SALARY
FROM HR.employees WHERE employee_id=163);

Output:

3. Display details of departments managed by ‘Alexander’.

Query:
select * from hr.departments where manager_id IN(select manager_id
from hr.employees where first_name='Alexander');

Output:

717822L602
4. Display departments where the name of the manager is MICHAEL.

Query: select department_name from hr.departments where


manager_id IN(select manager_id from hr.employees where
first_name='Michael');

Output:

5. Write a query to find the salary of employees whose salary is greater


than the salary of employee whose id is 103?
Query:
SELECT first_name, last_name ,salary,employee_idFROM hr.employees WHERE
salary > ( SELECT salary FROM hr.employees WHERE employee_id=163);
Output:

6. Display the department name and Id for all departments where they
located, that Id is equal to the Id for the location where department
number 30 is located.
Query : select department_name,department_id from hr.departments where
location_id IN(select location_id from hr.departments where department_id=30);

717822L602
Output:

7. Display the full name, email, and hire date for all those employees
who was hired after the employee whose ID is 165.
Query:
SELECT first_name ||' '|| last_name AS Full_Name , hire_date
,email,employee_id
FROM hr. employees WHERE hire_date > (SELECT hire_date FROM
hr.employees WHERE employee_id = 165);
Output:

8. Get the details of employees who are managers.

Query:
select * from hr.employees where manager_id IN (select manager_id from
hr.employees where manager_id is not null);

717822L602
Output:

9. Display the department code and name for all departments which
located in the city London.

Query:
select department_id,department_name from hr.departments where
location_id = (select location_id from hr.locations where city ='London');
Output:

10. Display the first and last name, salary, and department ID for all
those employees who earn more than the average salary and arrange the
list in descending order on salary.

Query:
select first_name||last_name fullname,salary,department_id from
hr.employees where salary>(select avg(salary) from hr.employees) order by
salary desc
Output:

717822L602
11. Display the first and last name, salary, and department ID for those
employees who earn more than the maximum salary of a department
which ID is 40.

Query:
select first_name||last_name fullname,salary,department_id from
hr.employees where salary>(select MAX(salary) from hr.employees where
department_id=40);
Output:

12. Display the first and last name, salary, and department ID for those
employees whose salary is equal to the salary of the employee who
works in that department which ID is 40.
Query:
select first_name||last_name fullname,salary,department_id from
hr.employees where salary=(select salary from hr.employees where
department_id=40);
Output:

717822L602
13. Find the employees who all are earning the highest salary?
Query:
select first_name||last_name fullname from hr.employees where
salary=(select max(salary) from hr.employees);
Output:

14. Display the first and last name, and department code for all
employees who work in the department Marketing.
Query:
select first_name, last_name, department_id FROM hr.employees
where department_id = (SELECT department_id FROM hr.departments
WHERE department_name = 'Marketing');
Output:

15. Find those employees who earn more than the average salary. Return
employee ID, first name, last name.

Query:
select first_name||last_name fullname,employee_id from hr.employees
where salary>(select avg(salary) from hr.employees);

717822L602
Output:

16. Display the employee name (first name and last name) and hire date
for all employees in the same department as Clara. Exclude Clara.

Query:
select first_name||last_name fullname,hire_date from hr.employees where department_id=(select
department_id from hr.employees where first_name='Clara') and first_name != 'Clara';

Output:

17. Display the employee number and name (first name and last name)
for all employees who work in a department with any employee whose name contains a T.

Query:
select first_name||last_name fullname,employee_id from hr.employees where
department_id IN(select department_id from hr.employees where
first_name||last_name LIKE '%T%');

717822L602
Output:

18. Display details of departments in which the maximum salary is more


than 10000.

Query:

select * from hr.departments where department_id IN(select


department_id from hr.job_history where job_id IN (select job_id from
hr.jobs where max_salary>10000))

Output:

19. Find the employees whose salary is equal to the salary of at least one employee in
department of id 300?

Query:

select first_name from hr.employees where salary = (select MIN(salary) from


hr.employees where department_id=100);

717822L602
Output:

20. Find the employees whose salary is greater than at least on employee
in department of id 100?

Query:
select first_name from hr.employees where salary > (select MIN(salary) from
hr.employees where department_id=100);
Output:

21. Display the employee name (first name and last name), employee id,
and job title for all employees whose department location is Toronto.

Query:
select first_name||last_name fullname ,employee_id,job_id from
hr.employees where department_id IN (select department_id from
hr.departments where location_id=(select location_id from hr.locations where
city='Toronto'));

717822L602
Output:

22. Display details of manager who manages more than 5 employees.


Query:
SELECT first_name || last_name AS Manager_name,department_id FROM
hr.employees WHERE employee_id IN (SELECT manager_id FROM
hr.employees GROUP BY manager_id HAVING COUNT(*)>=5);
Output:

23. Display the city of employee whose employee ID is 105.

Query:
SELECT city FROM hr.locations WHERE location_id = (SELECT
location_id FROM hr.departments WHERE department_id = (SELECT
department_id FROM hr.employees WHERE employee_id=105));
Output:

717822L602
24. Display employees who did not do any job in the past.
Query:

SELECT * FROM hr.employees WHERE employee_id not in (select


employee_id from hr.job_history);

Output:

25. Find those employees who get second-highest salary. Return


all the fields of the employees.

Query:
Select *from hr.employees where employee_id IN(select employee_id
from hr.employees where salary =(select max (salary) from
hr.employees where salary <(select max (salary) from hr.employees)));
Output:

717822L602
26. Display the employee number, name (first name and last name) and
job title for all employees whose salary is smaller than any salary of
those employees whose job title is MK_MAN.
Query:
select employee_id,first_name,last_name, job_id from hr.employees
where salary < any ( select salary from hr.employees where job_id =
'MK_MAN' ) and job_id <> 'MK_MAN';
Output:

27. Write a query to determine who earns more than Mr. Ozer.

Query:

SELECT first_name, last_name, salary from hr.employees where salary


> (select salary from HR.employees where last_name='Ozer') Order by
last_name;

717822L602
Output:

28. Identify all the employees who earn more than the average and who
work in any of the IT departments.

Query:

select last_name from hr.employees where department_id in (select


department_id from hr.departments where department_name LIKE
'IT%') and salary > (select avg(salary) from hr.employees);

Output:

RESULT:
Thus, the SQL select statements Using Subqueries To Solve
Queries is verified

717822L602
RESULT:
Thus, the SQL select statements Using Subqueries To Solve Queries is
verified.

717822L602
717822L602

You might also like