w3resource

PostgreSQL Aggregate Functions and Group By: Get the number of employees working in each post


7. Write a query to get the number of employees working in each post.

Sample Solution:

Code:

SELECT job_id, COUNT(*) 
FROM employees 
GROUP BY job_id;

Sample table: employees


Output:

pg_exercises=# SELECT job_id, COUNT(*)
pg_exercises-# FROM employees
pg_exercises-# GROUP BY job_id;
   job_id   | count
------------+-------
 Salesman   |     5
 AC_ACCOUNT |     1
 IT_PROG    |     5
 SA_MAN     |     5
 AD_PRES    |     1
 AC_MGR     |     1
 FI_MGR     |     1
 AD_ASST    |     1
 MK_MAN     |     1
 PU_CLERK   |     5
 HR_REP     |     1
 PR_REP     |     1
 FI_ACCOUNT |     5
 SH_CLERK   |    20
 AD_VP      |     2
 SA_REP     |    29
 ST_CLERK   |    20
 MK_REP     |     1
 PU_MAN     |     1
(19 rows)

Practice Online


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming