Display Each Department'S Number and Name and the Number of Employees Employed in Each Department

How to return each department name and the number of employees for each department

That's a simple aggregate query. Yours fails because:

  • it is missing a GROUP BY clause
  • table alias d is not defined in the query

Consider:

SELECT d.name, COUNT(*) no_employees
FROM Department d
INNER JOIN Employees as e ON d.departmentID = e.departmentID
GROUP BY d.departmentID, d.name

If you want to also display departments without employees, then:

SELECT d.name, COUNT(e.departmentID) no_employees
FROM Department d
LEFT JOIN Employees as e ON d.departmentID = e.departmentID
GROUP BY d.departmentID, d.name

Find the number of employees in each department - SQL Oracle

Please try:

select count(*) as count,dept.DNAME 
from emp
inner join dept on emp.DEPTNO = dept.DEPTNO
group by dept.DNAME

List the department name and the number of employees in each department

If you actually need information from your employee_id table then you need to join it on using your foreign key. That said I can't see that you are using it. Also use proper join syntax not the old style implicit join you were attempting to use. At the moment you will be getting a duplicate department row per employee.

Then depending on whether you store Num_of_employ or whether you are trying to calculate it, either way you don't want to group by. You may want to sum it, but that doesn't make sense unless you have multiple entries per department.

select D.Departments_Name as "Department", sum(D.Num_of_employ) as "Number of employees"
from dbo.departments D
-- Do you actually need to join this on? You don't appear to be using it.
-- inner join dbo.employee_id E on E.DepartmentId = D.DepartmentId
group by D.Departments_Name
order by D.Departments_Name
offset 0 rows fetch first 12 rows only;

Maybe you are attempting to count the staff e.g.

select D.Departments_Name as "Department", count(*) as "Number of employees"
from dbo.departments D
inner join dbo.employee_id E on E.DepartmentId = D.DepartmentId
group by D.Departments_Name
order by D.Departments_Name
offset 0 rows fetch first 12 rows only;

How to use COUNT() and GROUP BY to display number of employees in each department

Use GROUP BY

 SELECT d.deptID, count(e.deptID)
FROM Department d
LEFT JOIN Employee e ON d.DeptID = e.DeptID
GROUP BY d.deptId

and LEFT JOIN is used to include departments that do not have employees.

MySQL: get total number of employees in each department

I think you just need left join:

select d.departmentId, d.name as departmentName,
count(e.departmentid) as totalEmployees
from department d left join
employees e
on d.departmentId = e.departmentId
group by d.departmentId, d.name;

Including the department id distinguishes among departments with the same name.

If you want all departments with the same name to be combined, then remove departmentid from the select and group by:

select d.name as departmentName,
count(e.departmentid) as totalEmployees
from department d left join
employees e
on d.departmentId = e.departmentId
group by d.name;


Related Topics



Leave a reply



Submit