Zibar
Zibar

Reputation: 95

Count users of each department

I have a table with the following layout :


+----+-----------+-------------+
| id | name      | department  |
+----+-----------+-------------+
|   1| John      | Finance     |
+----+-----------+-------------+
|   2| Bob       | Optics      |
+----+-----------+-------------+
|   3| Jill      | Finance     |
+----+-----------+-------------+
|   4| Jake      | Finance     |
+----+-----------+-------------+
|   5| Mike      | Support     |
+----+-----------+-------------+

I want to get the sum of all the people in each department :

+-----------+-------------+
| people    | department  |
+-----------+-------------+
|          3| Finance     |
+-----------+-------------+
|          1| Optics      |
+-----------+-------------+
|          1| Support     |
+-----------+-------------+

I am having trouble constructing a right query

Upvotes: 1

Views: 756

Answers (3)

i486
i486

Reputation: 6563

Try this :-

 SELECT COUNT(name) AS people,department FROM table GROUP BY department

Upvotes: 1

Manashvi Birla
Manashvi Birla

Reputation: 2843

Use COUNT

SELECT COUNT(name),department FROM table GROUP BY department

Upvotes: 2

The Reason
The Reason

Reputation: 7973

SELECT departmetn, Count(*) as people FROM table Group by departmetn

Upvotes: 1

Related Questions