Retrieve The Department Name
Retrieve The Department Name
Retrieve the department name, manager's last name, and the total number of employees in
each department. Only include departments with at least one employee and order the
results by the total number of employees in descending order
SELECT d.department_name, e.last_name AS manager_last_name, COUNT(emp.employee_id) AS
total_employees FROM departments d LEFT JOIN employees e ON d.manager_id = e.employee_id
LEFT JOIN employees emp ON d.department_id = emp.department_id GROUP BY d.department_name,
e.last_name ORDER BY total_employees DESC
2. List the job titles along with the minimum, maximum, and average salary for each job title,
and include only those job titles where the average salary is greater than $50,000.
SELECT
j.job_title,
MIN(e.salary) AS min_salary,
MAX(e.salary) AS max_salary,
AVG(e.salary) AS avg_salary
FROM
jobs j
join employees e on e.job_id=j.job_id
GROUP BY
j.job_title
HAVING AVG(e.salary) > 5000
3. Show the department name and the total number of employees hired in each year, but only
display departments with at least one employee hired in the given year. Order the results by
department name and year of hire.
select d.department_name,count(e.employee_id) as no_of_emp,extract(year from e.hire_date) as
hirng_year
from employees e
left join departments d on e.department_id=d.department_id
group by d.department_name,EXTRACT(YEAR FROM e.hire_date)
order by d.department_name,hirng_year;
4. Display the employee's last name, job title, and salary, along with the difference between
each employee's salary and the average salary for their job title. Include only employees
whose salary is above the average salary for their job title.
SELECT last_name, job_title, salary, (salary-avg_salary_per_job) as salary_diff
FROM (
SELECT
e.last_name, j.job_title, e.salary,
AVG(e.salary) OVER (PARTITION BY j.job_id) AS avg_salary_per_job
FROM
employees e
JOIN
jobs j
ON
j.job_id = e.job_id
) sub
WHERE
sub.salary > sub.avg_salary_per_job
5. List the employee IDs, last names, and hire dates of employees who were hired before their
manager. Order the results by hire date.
select e.employee_id,e.last_name,e.hire_date,emp.hire_date
from employees e
left join employees emp on e.employee_id=emp.manager_id
where emp.hire_date>e.hire_date
order by e.hire_date;
6. Show the department name, manager's last name, and the total salary for employees
managed by each manager. Only include managers who have at least one employee and
order the results by total salary in descending order.
select d.department_name,emp.last_name,sum(emp.salary) as sal
from departments d
join employees emp on d.manager_id=emp.employee_id
join employees e on emp.employee_id=e.manager_id
group by d.department_name,emp.last_name
having count(e.employee_id)>0
order by sal desc;
7. Retrieve the job title, department name, and the highest salary in each department, but only
include departments with at least one employee. Order the results by department name.
SELECT j.job_title, d.department_name, MAX(e.salary) AS highest_salary
FROM employees e JOIN departments d ON e.department_id = d.department_id
JOIN jobs j ON e.job_id = j.job_id GROUP BY j.job_title, d.department_name HAVING
COUNT(e.employee_id) > 0 ORDER BY d.department_name
8. Display the employee's last name, job title, and salary, along
with the difference between each employee's salary and the
maximum salary for their job title. Include only employees
whose salary is below the maximum salary for their job title.
SELECT
employee_id,
last_name,
job_id,
salary,
max_salary_per_job
FROM (
SELECT
e.employee_id,
e.last_name,
j.job_id,
e.salary,
max(e.salary) OVER (PARTITION BY j.job_id) AS max_salary_per_job
FROM
employees e
JOIN
jobs j
ON
j.job_id = e.job_id
) sub
WHERE
sub.salary < sub.max_salary_per_job
9. List the department name, job title, and the total number of
employees in each department who hold each job title. Only
include departments with at least one employee and order the
results by department name and job title.
select d.department_name,j.job_title,count(e.employee_id) as ss
from employees e
join jobs j on e.job_id=j.job_id
join departments d on d.department_id=e.department_id
group by j.job_title,d.department_name;
10. Show the employee's last name, department name, and the difference between each employee's
salary and the average salary in their department. Include only employees whose salary is above the
average salary for their department.
SELECT
last_name,
department_id,
salary,
avg_salary_per_dep
FROM (
SELECT
e.last_name,
d.department_id,
e.salary,
avg(e.salary) OVER (PARTITION BY d.department_id) AS avg_salary_per_dep
FROM
employees e
JOIN
departments d
ON
e.department_id = d.department_id
) sub
WHERE
sub.salary > sub.avg_salary_per_dep
11. Retrieve the department name and the total number of employees in each department who
have a salary greater than the average salary for their department. Only include
departments with at least one employee and order the results by department name.
select d.department_name,count(e.employee_id) as count ,sum(e.salary) as sum,
(sum(e.salary)/count(e.employee_id)) as average
from departments dS
join employees e on e.department_id=d.department_id
group by d.department_name;
12. Display the employee's last name, job title, and salary, along with the difference between each
employee's salary and the minimum salary for their job title. Include only employees whose salary
is above the minimum salary for their job title.
select e.last_name,j.job_title,e.salary,(e.salary-j.min_salary) as diff
from employees e
left join jobs j on e.job_id=j.job_id
where e.salary>j.min_salary;
13. List the job title, department name, and the average number of years employees with that job
title have been with the company. Only include job titles where the average number of years is
greater than five and order the results by department name and job title.
SELECT j.job_title, d.department_name,
AVG(extract(YEAR from e.hire_date))-2000 AS average FROM departments d
LEFT JOIN employees e ON e.department_id = d.department_id
LEFT JOIN jobs j ON j.job_id = e.job_id
GROUP BY j.job_title, d.department_name
HAVING AVG(extract(YEAR from e.hire_date)) - 2000 > 5
ORDER BY d.department_name, j.job_title;
14. Show the department name and the total number of employees in each department who have
the same job title as their manager. Only include departments with at least one employee and order
the results by department name.
SELECT d.department_name, COUNT(e.employee_id) AS total_employees
FROM departments d
JOIN employees e ON d.department_id = e.department_id
JOIN employees m ON e.manager_id = m.employee_id
WHERE e.job_id = m.job_id
GROUP BY d.department_name
HAVING COUNT(e.employee_id) > 0
15. Retrieve the employee's last name, job title, and salary, along with the difference between each
employee's salary and the salary of the employee who earns the least in the same department.
Include only employees whose salary is above the minimum salary for their department and order
the results by department name and employee last name.
SELECT
last_name,
department_id,
salary,
( salary- min_salary_per_dep) as diff
FROM (
SELECT
e.last_name,
d.department_id,
e.salary,
min(e.salary) OVER (PARTITION BY d.department_id) AS min_salary_per_dep
FROM
employees e
JOIN
departments d
ON
e.department_id = d.department_id
) sub
WHERE
sub.salary > sub.min_salary_per_dep
order by department_id,last_name;