Find the number of employees in each department using SQL

Write a method to find the number of employees in each department.

My initial thoughts(code):

SELECT Department.DepartmentName, COUNT(employee.lastName)
FROM employee, department 
WHERE employee.DepartmentID = department.DepartmentID
GROUP BY Department.DepartmentName

Solution:

SELECT Dept_Name, Departments.Dept_ID, COUNT(*) AS 'num_employees'
FROM Departments
LEFT JOIN Employees
ON Employees.Dept_ID = Departments.Dept_ID
GROUP BY Departments.Dept_ID, Dept_Name
Advertisements

1 Comment (+add yours?)

  1. dorababu743
    Jan 23, 2013 @ 12:13:08

    Thanks man you saved ma

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: