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
Sql Join: Selecting the Last Records in a One-To-Many Relationship
Sql Server How to Return Null Instead of 0 If a Grouped Value Doesn't Have Rows in Source
Phone Number Display Method, SQL Query
Query to Get All Those Names of Employees,Who Have 'A' as Their Middle Character in Their Name
Two Rows With the Same Id and Two Different Values, Getting the Second Value into Another Column
Mysql Select Rows on First Occurrence of Each Unique Value
Postgresql - Combining Multiple Rows With Several Attributes into One Row
How to Tell If a Value Is Not Numeric in Oracle
Sql Max Function Returns One Row When Multiple Rows Have the Same Value
How to Get Previous Row Data in SQL Server
Create a Query That Will Display Employee Name and Department Number
Sql Server-How to Replace a Date Column With a Current Date When the Column Has a Null Value
How to Compare Datetime With Only Date in SQL Server
Constraint for Phone Number in SQL Server
Left Join Without Duplicate Rows from Left Table