Query for getting number of employee by department wise.

Hi all,

Here I am having the following two tables.

Table : Department

Id Name

--- ----

1 IT

2 Maths

3 Science


Table: Employee

Id Name DeptId

--- ---- ------

1 aaa 2

2 vvv 2

3 eee 2

4 www 1

5 yyy 2

6 ppp 3

7 ooo 1

8 rrr 3

9 uuu 1

10 iii 3



Now, I need to get the number of employee in each department. How to do?

Here is the query for that.

select dept.Name as DepartmentName,count(*) as NoOfEmp from Department dept

INNER JOIN Employee emp on emp.DeptId=dept.Id group by dept.Name


By running this query, we can get the values like as follows.


DepartmentName NoOfEmp

-------------- -------

IT 3

Maths 4

Science 3


That's it...



...S.VinothkumaR.

No comments: