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

Exercise - Hr Database

The document contains a series of SQL exercises focused on querying an HR database. It includes various tasks such as sorting, filtering, and using subqueries to extract employee information based on specific criteria. Each exercise provides a SQL query answer to retrieve the requested data from the database.

Uploaded by

cute girl
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)
1 views

Exercise - Hr Database

The document contains a series of SQL exercises focused on querying an HR database. It includes various tasks such as sorting, filtering, and using subqueries to extract employee information based on specific criteria. Each exercise provides a SQL query answer to retrieve the requested data from the database.

Uploaded by

cute girl
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/ 29

HR DATABASE EXERCISE

A. SORTING AND FILTERING


1. Write a SQL query to find those employees whose salaries are less than 6000.
Return full name (first and last name), and salary.
Ans SELECT First_name ,Last_Name ,Salary from hr_employees where Salary<6000 ;
2. Write a SQL query to find those employees whose salary is higher than 8000. Return
first name, last name and department number and salary.
Ans SELECT First_name ,Last_Name,Department_Id,salary from hr_employees where
salary >8000 ;
3. Write a SQL query to find those employees whose last name is "McEwen". Return
first name, last name and department ID.
Ans SELECT First_Name,Last_Name , Department_Id from hr_employees where
Last_Name='McEwen' ;
4. Write a SQL query to identify employees who do not have a department number.
Return employee_id, first_name, last_name, email, phone_number, hire_date,
job_id, salary, commission_pct, manager_id and department_id.
Ans SELECT * FROM hr_employee where department_id is NULL;
5. Write a SQL query to find the details of 'Marketing' department. Return all fields.
Ans SELECT * FROM hr_department where Department_name=’Marketing’ ;
6. Write a SQL query to find those employees whose first name does not contain the
letter ‘M’. Sort the result-set in ascending order by department ID. Return full name
(first and last name together), hire_date, salary and department_id.
Ans SELECT concat(First_Name,'',Last_Name) as
full_name,Hire_Date,Salary,Department_Id from hr_employees where First_Name
not like 'm%' order by Department_Id ;
7. Write a SQL query to find those employees who earn between 8000 and 12000
(Begin and end values are included.) and get some commission. These employees
joined before ‘1987-06-05’ and were not included in the department numbers 40,
120 and 70. Return all fields.
Ans SELECT *FROM hr_employees WHERE salary BETWEEN 8000 AND 12000 AND
commission_pct IS NOT NULL OR department_id NOT IN (40 , 120 , 70) AND
hire_date < '2003-06-05' ;
8. Write a SQL query to find those employees who do not earn any commission. Return
full name (first and last name), and salary
Ans SELECT concat(First_Name,'',Last_Name) as 'full name',salary from hr_employees
where Comission_PCT is null;
9. Write a SQL query to find the employees whose salary is in the range 9000, 17000
(Begin and end values are included). Return full name, contact details and salary.
Ans SELECT concat(First_Name,'',Last_Name) as 'full
name',salary,Phone_Number from hr_employees where Salary BETWEEN 9000 and 17
000;
10. Write a SQL query to find the employees whose first name ends with the letter
‘m’. Return the first and last name, and salary.
Ans SELECT First_Name,Last_Name,Salary from hr_employees where First_Name like
'%m';
11. Write a SQL query to find those employees whose salaries are not between 7000
and 15000 (Begin and end values are included). Sort the result-set in ascending
order by the full name (first and last). Return full name and salary.
Ans SELECT concat (First_Name,'',Last_Name) as 'full_name',Salary from hr_employees
where Salary BETWEEN 7000 and 15000 order by full_name;
12. Write a SQL query to find those employees who were hired between November
5th, 2007 and July 5th, 2009. Return full name (first and last), job id and hire date.
Ans SELECT concat (First_Name,'',Last_Name) as 'full_name',job_id,Hire_Date from
hr_employees where Hire_Date BETWEEN '2007-11-05' and '2009-07-05';
13. Write a SQL query to find those employees who work either in department 70 or
90. Return full name (first and last name), department id.
Ans SELECT concat (First_Name,'',Last_Name) as 'full_name',Department_Id from
hr_employees where Department_Id=70 or Department_Id=90;
14. Write a SQL query to find those employees who work under a manager. Return
full name (first and last name), salary, and manager ID.
Ans SELECT concat (First_Name,'',Last_Name) as 'full_name',Salary,Manager_Id from
hr_employees where Manager_Id is not null;
15. Write a SQL query to find the employees who were hired before June 21st, 2002.
Return all fields.
Ans SELECT * from hr_employees where Hire_Date=<'2002-06-21';
16. Write a SQL query to find the employees whose managers hold the ID 120, 103,
or 145. Return first name, last name, email, salary and manager ID.
Ans SELECT First_Name,Last_Name,Email,Salary,Manager_Id from hr_employees
where Manager_Id in (120,130,145);
17. Write a SQL query to find employees whose first names contain the letters D, S,
or N. Sort the result-set in descending order by salary. Return all fields.
Ans SELECT * from hr_employees where First_Name like'd%' or First_Name like 's%'
or First_Name='n%' order by Salary;
18. Write a SQL query to find those employees whose first name contains a character
’s’ in the third position. Return first name, last name and department id.
Ans SELECT First_Name,Last_Name,Department_Id FROM hr_employees where
First_Name like '__s%';
19. Write a SQL query to find those employees work in the departments that are not
part of the department 50 or 30 or 80. Return employee_id, first_name, job_id,
department_id.
Ans SELECT Employee_Id,First_Name,Job_id,Department_Id FROM hr_employees where
Department_Id in(50,30,80);
20. Write a SQL query to find those employees who worked more than two jobs in the
past. Return employee id.
Ans
SELECT employee_id FROM hr_job_history GROUP BY employee_id HAVING COUNT(*)
>= 2;
21. Write a SQL query to count the number of employees, the sum of all salary, and
difference between the highest salary and lowest salaries by each job id. Return
job_id, count, sum, salary_difference.
Ans SELECT Job_id,count(Employee_Id),sum(Salary),max(Salary)-min(Salary) as
Salary_difference from hr_employees group by Job_id;
22. Write a SQL query to find each job ids where two or more employees worked for
more than 300 days. Return job id.
ANS SELECT job_id FROM hr_job_history WHERE end_date - start_date > 300 GROUP
BY job_id HAVING COUNT(*) >= 2;
23. Write a SQL query to count the number of cities in each country. Return country
ID and number of cities.
ANS SELECT Country_Id ,count(city) from hr_locations group by Country_Id;
24. Write a SQL query to count the number of employees worked under each
manager. Return manager ID and number of employees.
ANS SELECT Manager_Id,count(Employee_Id) from hr_employees group by
Manager_Id;
25. Write a SQL query to calculate the average salary of employees who receive a
commission percentage for each department. Return department id, average
salary.
ANS SELECT Department_Id,avg(Salary) from hr_employees where Comission_PCT is
not null group by Department_Id;
26. Write a SQL query to find the departments where any manager manages four or
more employees. Return department_id.
ANS SELECT DISTINCT department_id FROM hr_employees GROUP BY department_id,
manager_id HAVING COUNT(employee_id) >= 4;
27. Write a SQL query to find the departments where more than ten employees
receive commissions. Return department id.
ANS SELECT department_id FROM employees WHERE commission_pct IS NOT NULL
GROUP BY department_id HAVING COUNT(commission_pct) > 10
28. Write a SQL query to find those employees who do not have commission
percentage and have salaries between 7000, 12000 (Begin and end values are
included.) and who are employed in the department number 50. Return all the fields
of employees.
ANS SELECT * FROM hr_employees where Comission_PCT is null and salary between
7000 and 12000 and Department_Id=50;
29. Write a SQL query to compute the average salary of each job ID. Exclude those
records where average salary is on or lower than 8000. Return job ID, average
salary.
ANS SELECT Job_id,avg(Salary) from hr_employees group by Job_id having
avg(Salary)>8000;
30. Write a SQL query to find those job titles where maximum salary falls between
12000 and 18000 (Begin and end values are included.). Return job_title, max_salary
- min_salary.
ANSSELECT job_title, max_salary-min_salary as salary_difference FROM hr_jobs
WHERE MAX_salary BETWEEN 12000 AND 18000;
31. Write a SQL query to find the employees whose first or last name begins with 'D'.
Return first name, last name.
ANS SELECT first_name,last_name from hr_employees WHERE first_name LIKE 'D%'
OR last_name LIKE 'D%';
32. Write a SQL query to find details of those jobs where the minimum salary exceeds
9000. Return all the fields of jobs.
ANS SELECT * FROM hr_jobs WHERE min_salary >9000;
33. Write a SQL query to find those employees who joined after 7th September 1987.
Return all the fields.
ANS SELECT * FROM employees WHERE hire_date > '1987-07-19'
B. SUBQUERIES
1. Write a SQL query to find those employees who receive a higher salary than the
employee with ID 163. Return first name, last name.
Ans SELECT first_name, last_name FROM hr_employees WHERE salary > ( SELECT salary
FROM employees WHERE employee_id = 163 );

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);

You might also like