Exercise - Hr Database
Exercise - Hr Database
2. Write a SQL query to find out which employees have the same designation as the
employee whose ID is 169. Return first name, last name, department ID and job ID.
Ans SELECT first_name, last_name, salary, department_id, job_id FROM hr_employees
WHERE job_id = ( SELECT job_id FROM employees WHERE employee_id = 169 );
3. Write a SQL query to find those employees whose salary matches the lowest salary
of any of the departments. Return first name, last name and department ID.
Ans
4. Write a SQL query to find those employees who earn more than the average salary.
Return employee ID, first name, last name.
Ans select Employee_Id,first_name,last_name from hr_employees where salary >
(select avg(salary) from hr_employees);
5. Write a SQL query to find those employees who report to that manager whose first
name is ‘Payam’. Return first name, last name, eEdit inline
Ans select Employee_Id,first_name,last_name,Salary from hr_employees where
Manager_Id= (select First_Name from hr_employees where First_Name like
'Payam');
6. Write a SQL query to find the employee whose salary is 3000 and reporting person’s
ID is 121. Return all fields.
ANS SELECT * FROM hr_employees WHERE (salary, manager_id) =(SELECT 3000,
121);
7. Write a SQL query to find those employees whose salary falls within the range of
the smallest salary and 2500. Return all the fields
ANS. SELECT * FROM hr_employees WHERE salary BETWEEN (SELECT 1000) AND
3000;
8. ANS Write a SQL query to find those employees who do not work in the departments
where managers’ IDs are between 100 and 200 (Begin and end values are
included.). Return all the fields of the employees.
Ans SELECT * FROM hr_mployees WHERE department_id NOT IN (SELECT
department_id FROM hr_departments WHERE manager_id BETWEEN 100 AND 200);
9. Write a SQL query to find those employees who get second-highest salary. Return
all the fields of the employees.
ANS 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)) );
10. Write a SQL query to find those employees who work in the same department as
‘Clara’. Exclude all those records where first name is ‘Clara’. Return first name, last
name and hire date.
ANS SELECT first_name, last_name, hire_date FROM hr_employees WHERE department_id =
(SELECT department_id FROM hr_employees WHERE first_name = 'Clara');
11. Write a SQL query to find those employees who work in a department where the
employee’s first name contains the letter 'T'. Return employee ID, first name and
last name.
ANS SELECT first_name, last_name, Employee_Id FROM hr_employees WHERE department_id
in (SELECT Department_Id FROM hr_employees WHERE first_name like '%t%');
12. Write a SQL query to find those employees who earn more than the average
salary and work in the same department as an employee whose first name contains
the letter 'J'. Return employee ID, first name and salary.
ANS SELECT first_name, salary,Employee_Id from hr_employees where salary >(select
avg(Salary)from hr_employees);
13. Write a SQL query to find those employees whose department is located at
‘Toronto’. Return first name, last name, employee ID, job ID.
ANS SELECT Employee_Id,First_Name,Last_Name,Job_id from hr_employees where
Department_Id=(select Department_Id from hr_department where Location_id=(select
location_id from hr_locations where city='toronto') );
14. Write a SQL query to find those employees whose salary is lower than that of
employees whose job title is ‘MK_MAN’. Return employee ID, first name, last name,
job ID.
ANS 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' );
15. Write a SQL query to find those employees whose salary is lower than that of
employees whose job title is "MK_MAN". Exclude employees of Job title ‘MK_MAN’.
Return employee ID, first name, last name, job ID.
ANS 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';
16. Write a SQL query to find those employees whose salary exceeds the salary of all
those employees whose job title is "PU_MAN". Exclude job title ‘PU_MAN’. Return
employee ID, first name, last name, job ID.
ANS SELECT Employee_Id,First_Name,Last_Name,Job_id from hr_employees where
salary < ALL (select Salary from hr_employees where job_id='PU_MAN' ) and
job_id<>'PU_MAN';
17. Write a SQL query to find those employees whose salaries are higher than the
average for all departments. Return employee ID, first name, last name, job ID.
ANS SELECT Employee_Id,First_Name,Last_Name,Job_id from hr_employees where
salary>all(select avg(Salary) FROM hr_employees group by Department_Id);
18. Write a SQL query to check whether there are any employees with salaries
exceeding 3700. Return first name, last name and department ID.
ANS SELECT first_name,last_name,department_id FROM employees WHERE exists (SELECT *
FROM employees WHERE salary>3700);
19. Write a SQL query to find all those departments where at least one employee is
employed. Return department name.
ANS SELECT department_name from hr_department where Department_Id in(SELECT
DISTINCT(Department_Id) from hr_department);
20. Write a SQL query to find employees who work in departments located in the
United Kingdom. Return first name.
ANS SELECT first_name FROM hr_employees WHERE department_id IN (SELECT
department_id FROM hr_department WHERE location_id IN (SELECT location_id FROM
hr_locations WHERE country_id = (SELECT country_id FROM hr_countries WHERE
country_name='United Kingdom')));
21. Write a SQL query to find out which employees are earning more than the
average salary and who work in any of the IT departments. Return last name.
ANS SELECT last_name FROM hr_employees WHERE department_id IN (SELECT
department_id FROM hr_department WHERE department_name LIKE 'IT%') AND salary
> (SELECT avg(salary) FROM hr_employees);
22. Write a SQL query to find all those employees who earn more than an employee
whose last name is 'Ozer'. Sort the result in ascending order by last name. Return
first name, last name and salary.
ANS SELECT First_Name,Last_Name,Salary from hr_employees where Salary > (SELECT
salary FROM hr_employees WHERE last_name='Ozer')ORDER BY last_name;
23. Write a SQL query find the employees who report to a manager based in the
United States. Return first name, last name.
ANS SELECT First_Name,Last_Name from hr_employees WHERE manager_id IN
(SELECT employee_id FROM hr_employees WHERE department_id IN (SELECT
department_id FROM hr_department WHERE location_id IN (SELECT location_id FROM
hr_locations WHERE country_id='US') ));
24. Write a SQL query to find those employees who are managers. Return all the
fields of employee’s table
ANS. select * from hr_employees where Employee_Id in ( select DISTINCT Manager_Id
from hr_employees) ;
25. Write a SQL query to find those employees who manage a department. Return all
the fields of employee’s table.
ANS SELECT *FROM hr_employees WHERE employee_id=ANY(SELECT manager_id
FROM hr_departments);
26. Write a SQL query to search for employees who receive such a salary, which is
the maximum salary for salaried employees, hired between January 1st, 2002 and
December 31st, 2003. Return employee ID, first name, last name, salary,
department name and city
ANS SELECT a.employee_id, a.first_name, a.last_name, a.salary, b.department_name,
c.city
FROM hr_employees a,‘hr_departments b, hr_locations c
WHERE a.salary =
(SELECT MAX(salary)
FROM hr_employees
WHERE hire_date BETWEEN '01/01/2002' AND '12/31/2003')
AND a.department_id=b.department_id
AND b.location_id=c.location_id;
27. Write a SQL query to find those departments that are located in the city of
London. Return department ID, department name.
ANS SELECT department_id,department_name FROM hr_departments WHERE
location_id=(SELECT location_id from hr_ locations WHERE city='London');
28. Write a SQL query to find those employees who earn more than the average
salary. Sort the result-set in descending order by salary. Return first name, last
name, salary, and department ID.
ANS
29. Write a SQL query to find those employees who earn more than the maximum
salary for a department of ID 40. Return first name, last name and department ID.
ANS SELECT first_name, last_name , salary, department_id
FROM hr_employees
WHERE salary > (
SELECT AVG(salary)
FROM hr_employees )
ORDER BY salary DESC;
30. Write a SQL query to find departments for a particular location. The location
matches the location of the department of ID 30. Return department name and
department ID.
ANS SELECT first_name,last_name,department_id FROM hr_employees WHERE
salary>All(SELECT salary from hr_employees WHERE department_id=30);
31. Write a SQL query to find employees who work for the department in which
employee ID 201 is employed. Return first name, last name, salary, and department
ID.
ANS SELECT first_name,last_name,salary,department_id FROM hr_employees WHERE
department_id=(SELECT department_id from hr_employees WHERE
employee_id=201);
32. Write a SQL query to find those employees whose salary matches that of the
employee who works in department ID 40. Return first name, last name, salary, and
department ID.
ANS SELECT first_name,last_name,salary,department_id FROM hr_employees WHERE
salary IN (SELECT salary FROM hr_employees WHERE department_id=40);
33. Write a SQL query to find those employees who work in the department
'Marketing'. Return first name, last name and department ID.
ANS SELECT first_name,last_name,department_id FROM hr_employees WHERE
department_id=(SELECT department_id FROM hr_departments WHERE
department_name='Marketing');
34. Write a SQL query to find those employees who earn more than the minimum
salary of a department of ID 40. Return first name, last name, salary, and
department ID
ANS SELECT first_name,last_name,salary,department_id FROM hr_employees WHERE
salary>Any(SELECT salary FROM hr_employees WHERE department_id=40);
35. Write a SQL query to find those employees who joined after the employee whose
ID is 165. Return first name, last name and hire date.
ANS SELECT first_name,last_name,hire_date FROM hr_employees WHERE
hire_date>(SELECT hire_date FROM hr_employees employee_id=165);
36. Write a SQL query to find those employees who earn less than the minimum
salary of a department of ID 70. Return first name, last name, salary, and
department ID.
ANS
37. Write a SQL query to find those employees who earn less than the average salary
and work at the department where Laura (first name) is employed. Return first
name, last name, salary, and department ID.
ANSSELECT first_name, last_name, salary, department_id
FROM hr_employees
WHERE salary <
(SELECT AVG(salary)
FROM hr_employees )
AND department_id =
(SELECT department_id
FROM hr_employees
WHERE first_name = 'Laura');
SELECT first_name,last_name,salary,department_id FROM hr_employees WHERE
department_id IN(SELECT department_id FROM hr_department WHERE
location_id=(SELECT location_id FROM hr_employees WHERE
location_name='London'));
38. Write a SQL query to find all employees whose department is located in London.
Return first name, last name, salary, and department ID.
ANS SELECT first_name,last_name,salary,department_id FROM hr_employees WHERE department_id
IN(SELECT department_id FROM hr_department WHERE location_id=(SELECT location_id FROM
hr_employees WHERE location_name='London'));
39. Write a SQL query to find the city of the employee of ID 134. Return city.
ANS 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=134));
40. Write a SQL query to find those departments where maximum salary is 7000 and
above. The employees worked in those departments have already completed one or
more jobs. Return all the fields of the departments.
ANS SELECT *FROM hr_departments
WHERE DEPARTMENT_ID IN
(SELECT DEPARTMENT_ID
FROM hr_employees
WHERE EMPLOYEE_ID IN
(SELECT EMPLOYEE_ID
FROM job_history
GROUP BY EMPLOYEE_ID
HAVING COUNT(EMPLOYEE_ID) > 1)
GROUP BY DEPARTMENT_ID
HAVING MAX(SALARY) > 7000);
41. Write a SQL query to find those departments where the starting salary is at least
8000. Return all the fields of departments.
ANS SELECT * FROM hr_departments
WHERE department_id IN
( SELECT department_id
FROM hr_employees
GROUP BY department_id
HAVING MIN(salary)>=8000);
42. Write a SQL query to find those managers who supervise four or more employees.
Return manager name, department ID.
ANS SELECT first_name ,last_name AS manager_name,department_id FROM
hr_employees WHERE employees_is IN (SELECT manager_id FROM hr_employees
GROUP BY manager_id Having count(*)>4);
43. Write a SQL query to find employees who have previously worked as 'Sales
Representatives'. Return all the fields of jobs.
ANS SELECT *
FROM hr_jobs
WHERE job_id IN
(SELECT job_id
FROM hr_employees
WHERE employee_id IN
(SELECT employee_id
FROM hr_jobhistories
WHERE job_id='SA_REP'));
44. Write a SQL query to find those employees who earn the second-lowest salary of
all the employees. Return all the fields of employees.
ANS SELECT *
FROM hr_employees m
WHERE 2 = (SELECT COUNT(DISTINCT salary )
FROM hr_employees
WHERE salary <= m.salary);
45. Write a SQL query to find the departments managed by Susan. Return all the
fields of departments.
ANS SELECT *
FROM hr_departments
WHERE manager_id IN
(SELECT employee_id
FROM hr_employees
WHERE first_name='Susan');
46. Write a SQL query to find those employees who earn the highest salary in a
department. Return department ID, employee name, and salary.
ANS SELECT department_id, concat(first_name,’’,last_name) AS Employee_name, salaryFROM
hr_employees a WHERE salary =(SELECT MAX(salary)FROM hr_employeesWHERE department_id =
a.department_id);
47. Write a SQL query to find those employees who have not had a job in the past.
Return all the fields of employees.
ANS SELECT * FROM hr_employees WHERE employee_id NOT IN (SELECT employee_id FROM
hr_jobhistory);
C. JOINS
1. Write a SQL query to find all those employees who work in the Finance department.
Return department ID, name (first name), job ID and department name.
Ans SELECT e.department_id, e.first_name, e.job_id, d.department_name
FROM hr_employees e, hr_departments d
WHERE e.department_id = d.department_id
AND d.department_name = 'Finance';
2. Write a SQL query to find the first name, last name, department number, and
department name for each employee.
ANS SELECT e.first_name, e.last_name,
d.department_name,
l.city,
l.state_province
FROM hr_employees e
INNER JOIN hr_departments d
ON e.department_id = d.department_id
INNER JOIN hr_locations l
ON d.location_id = l.location_id;
3. Write a SQL query to find the first name, last name, department, city, and state
province for each employee.
ANS SELECT e.first_name, e.last_name,d.department_name, l.city, l.state_province
FROM hr_employees e INNER JOIN hr_departments d ON e.department_id =
d.department_id
INNER JOIN locations l ON d.location_id = l.location_id;
4. Write a query in SQL to display the first name, last name, salary, and job grade for
all employees.
ANS SELECT E.first_name, E.last_name, E.salary, J.grade_level FROM hr_employees
E JOIN hr_job J ON E.salary BETWEEN J.lowest_sal AND J.highest_sal;
5. Write a SQL query to find all those employees who work in department ID 80 or 40.
Return first name, last name, department number and department name.
ANS SELECT e.first_name, e.last_name,d.department_id, d.department_name
FROM hr_employees e INNER JOIN hr_departments d ON
e.department_id = d.department_id AND d.department_id IN (80, 40)
ORDER BY e.last_name;
6. Write a SQL query to find those employees whose first name contains the letter ‘z’.
Return first name, last name, department, city, and state province.
ANS SELECT e.first_name, e.last_name, d.department_name,
l.city, l.state_province FROM hr_employees e
INNER JOIN hr_departments d ON e.department_id = d.department_id INNER JOIN locations l
ON d.location_id = l.location_id WHERE e.first_name LIKE '%z%';
7. Write a SQL query to find all departments, including those without employees.
Return first name, last name, department ID, department name.
ANS SELECT e.first_name,
e.last_name,
d.department_id,
d.department_name
FROM hr_departments d LEFT JOIN hr_employees e
ON d.department_id = e.department_id;
8. Write a SQL query to find the employees who earn less than the employee of ID
182. Return first name, last name and salary.
ANS SELECT e1.first_name,e1.last_name, e1.salary FROM hr_employees e1
INNER JOIN hr_employees e2 ON e1.salary < e2.salary AND e2.employee_id = 182;
9. Write a SQL query to find the employees and their managers. Return the first name
of the employee and manager.
ANS SELECT e1.first_name AS "employee_name", e2.first_name AS "manager_name"
FROM hr_employees e1 INNER JOIN hr_employees e2 ON e1.manager_id =
e2.employee_id;
10. Write a SQL query to display the department name, city, and state province for
each department.
ANS SELECT d.department_name,l.city,l.state_province FROM hr_departments d INNER JOIN
hr_locations l ON d.location_id = l.location_id;
11. Write a SQL query to find out which employees have or do not have a
department. Return first name, last name, department ID, department name.
ANS SELECT e.first_name,e.last_name, d.department_id, d.department_name
FROM hr_employees e LEFT JOIN hr_departments d ON e.department_id = d.department_id;
12. Write a SQL query to find the employees and their managers. Those managers do
not work under any manager also appear in the list. Return the first name of the
employee and manager.
ANS SELECT e1.first_name AS "employee_name", e2.first_name AS "manager_name" FROM
hr_employees e1 LEFT JOIN hr_employees e2 ON e1.manager_id = e2.employee_id;
13. Write a SQL query to find the employees who work in the same department as
the employee with the last name Taylor. Return first name, last name and
department ID.
ANS SELECT e1.first_name, e1.last_name, e1.department_id FROM hr_employees e1
INNER JOIN hr_employees e2 ON e1.department_id = e2.department_id
AND e2.last_name = 'Taylor';
14. Write a query in SQL to display the job title, department name, full name (first
and last name ) of employee, and starting date for all the jobs which started on or
after 1st January, 1993 and ending with on or before 31 August, 1997.
ANSSELECT j.job_title, d.department_name, CONCAT(e.first_name, ' ', e.last_name) AS
full_name, jh.start_date FROM hr_employees eINNER JOIN hr_job_history jh ON
e.employee_id = jh.employee_id AND jh.start_date BETWEEN '1993-01-01' AND '1997-08-31'
INNER JOIN jobs j ON jh.job_id = j.job_id INNER JOIN hr_departments d ON
jh.department_id = d.department_id;
15. Write a SQL query to calculate the difference between the maximum salary of the
job and the employee's salary. Return job title, employee name, and salary
difference.
ANS SELECT j.job_title, CONCAT(e.first_name, ' ', e.last_name) AS full_name,
(j.max_salary - e.salary) AS salary_diff FROM jr_employees e INNER JOIN hr_jobs j
ON e.job_id = j.job_id;
16. Write a SQL query to calculate the average salary, the number of employees
receiving commissions in that department. Return department name, average
salary and number of employees.
ANS SELECT d.department_name, AVG(e.salary), COUNT(commission_pct)
FROM hr_employees e JOIN hr_departments d ON e.department_id = d.department_id
GROUP BY d.department_name;
17. Write a SQL query to calculate the difference between the maximum salary and
the salary of all the employees who work in the department of ID 80. Return job
title, employee name and salary difference.
ANS SELECT CONCAT(e.first_name, ' ', e.last_name) AS full_name, j.job_title, (j.max_salary -
e.salary) AS salary_diff FROM hr_employees e INNER JOIN hr_jobs j ON e.job_id = j.job_id
WHERE e.department_id = 80;
18. Write a SQL query to find the name of the country, city, and departments, which
are running there.
ANS SELECT c.country_name,l.city, d.department_name FROM hr_countries c INNER JOIN
hr_locations l ON c.country_id = l.country_id INNER JOIN hr_departments d ON l.location_id =
d.location_id;
19. Write a SQL query to find the department name and the full name (first and last
name) of the manager.
ANS SELECT d.department_name, CONCAT(e.first_name, ' ', e.last_name) AS full_name
FROM hr_departments d INNER JOIN hr_employees e ON d.manager_id = e.employee_id;
20. Write a SQL query to calculate the average salary of employees for each job title.
ANS SELECT j.job_title,AVG(e.salary) FROM hr_employees e INNER JOIN hr_jobs j
ON e.job_id = j.job_id GROUP BY j.job_title;
21. Write a SQL query to find the employees who earn 12000 or more. Return
employee ID, starting date, end date, job ID and department ID.
ANS SELECT jh.* FROM hr_employees e INNER JOIN hr_job_history jh ON
e.employee_id = jh.employee_id WHERE salary >= 12000.00;
22. Write a SQL query to find out which departments have at least two employees.
Group the result set on country name and city. Return country name, city, and
number.
ANS SELECT c.country_name,l.city,COUNT(d.department_id) FROM hr_countries c
INNER JOIN hr_locations l ON c.country_id = l.country_id INNER JOIN hr_departments d ON
l.location_id = d.location_id WHERE d.department_id IN (SELECT e.department_id
FROM hr_employees e GROUP BY e.department_id HAVING COUNT(e.department_id) >= 2)
GROUP BY c.country_name, l.city;
23. Write a SQL query to find the department name, full name (first and last name) of
the manager and their city.
ANS SELECT d.department_name, CONCAT(e.first_name, ' ', e.last_name) AS full_name, l.city
FROM hr_employees e INNER JOIN hr_departments d ON e.employee_id = d.manager_id
INNER JOIN hr_locations l ON d.location_id = l.location_id;
24. Write a SQL query to calculate the number of days worked by employees in a
department of ID 80. Return employee ID, job title, number of days worked.
ANS SELECT jh.employee_id, j.job_title, (jh.end_date - jh.start_date) AS num_days FROM
hr_jobs j INNER JOIN hr_job_history jh ON j.job_id = jh.job_id WHERE jh.department_id = 80;
25. Write a SQL query to find full name (first and last name), and salary of all
employees working in any department in the city of London.
ANS SELECT CONCAT(e.first_name, ' ', e.last_name) AS full_name, e.salary
FROM hr_employees e INNER JOIN hr_departments d ON e.department_id = d.department_id
INNER JOIN hr_locations l ON d.location_id = l.location_id WHERE l.city = 'London';
26. Write a SQL query to find out the full name (first and last name) of the employee
with an ID and the name of the country where he/she is currently employed.
Ans SELECT CONCAT(e.first_name, ' ', e.last_name) AS full_name, employee_id, country_name
FROM employees JOIN hr_departments USING(department_id) JOIN hr_locations
USING(location_id) JOIN hr_countries USING(country_id);