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 Tree:
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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework