Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
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)

Relational Algebra Expression:

Relational Algebra Expression: Get the number of employees working in each post.

Relational Algebra Tree:

Relational Algebra Tree: Get the number of employees working in each post.

Practice Online


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

Previous: Write a query to get the highest, lowest, total, and average salary of all employees.
Next: Write a query to get the difference between the highest and lowest salaries.

What is the difficulty level of this exercise?