Preamble
PostgreSQL GROUP BY statement is used in the SELECT statement to collect data on several records and group results by one or more columns.
The syntax for GROUP BY statement in PostgreSQL
SELECT expression1_id, expression2_id,. expression_n_id,
aggregate_function
FROM tabs
[WHERE conds]
GROUP BY expression1_id, expression2_id,. expression_n_id;
Parameters and arguments of the statement
- expression1_id, expression2_id,…_n_id – Expressions that are not enclosed in an aggregate function and must be included in the GROUP BY offer.
- aggregate_function – This may be an aggregate function, for example, sum, count, min, max, or avg.
- Tabs – Tables from which you want to get records. At least one table must be specified in the FROM operator.
- WHERE conds – Optional. The conditions that must be met for the records to be selected.
Example using the sum function
Let’s consider an example of PostgreSQL query GROUP BY, which uses the sum function.
In this PostgreSQL example of GROUP BY, the sum function is used to return department names and “Salaries for the department”.
SELECT department, sum(salary_id) AS "Salaries for department".
FROM empls
GROUP BY depart;
Since you specified one column (department field) in your SELECT operator that is not encapsulated in the sum function, you should use the GROUP BY operator. That’s why the department field should be specified in the GROUP BY operator.
Example using the count function
Let’s consider how we could use the GROUP BY operator with the count function.
In this GROUP BY example, the count function is used to return department and “Number of employees” that have status ‘Active’.
SELECT department, count(*) AS "Number of employees"
FROM empls
WHERE status = 'Active'
GROUP BY depart;
Example using min function
Let’s now see how we can use the GROUP BY operator with the min function.
In this GROUP BY example, the function min is used to return department names and “Lowest salary”.
SELECT depart, min(salary_id) AS "Lowest salary".
FROM empls
GROUP BY depart;
Example using max
Finally, let’s see how we can use the GROUP BY operator with the max function.
In this GROUP BY example, the function max is used to get the department name and “Highest salary”.
SELECT department, max(salary_id) AS "Highest salary".
FROM empls
GROUP BY depart;
PostgreSQL: Group By | Course
About Enteros
Enteros offers a patented database performance management SaaS platform. It proactively identifies root causes of complex business-impacting database scalability and performance issues across a growing number of clouds, RDBMS, NoSQL, and machine learning database platforms.
The views expressed on this blog are those of the author and do not necessarily reflect the opinions of Enteros Inc. This blog may contain links to the content of third-party sites. By providing such links, Enteros Inc. does not adopt, guarantee, approve, or endorse the information, views, or products available on such sites.
Are you interested in writing for Enteros’ Blog? Please send us a pitch!
RELATED POSTS
How to Improve Retail Profitability with Enteros Cost Attribution, Database Analytics, and Software Management
- 21 June 2026
- Software Engineering
Introduction The retail industry is evolving at an unprecedented pace. Driven by ecommerce growth, omnichannel experiences, customer personalization, and digital transformation initiatives, retailers are investing heavily in technology to remain competitive. Modern retail organizations rely on: Ecommerce platforms Point-of-sale (POS) systems Inventory management applications Customer engagement platforms Supply chain systems Retail analytics environments AI-powered recommendation … Continue reading “How to Improve Retail Profitability with Enteros Cost Attribution, Database Analytics, and Software Management”
How to Drive Education Platform Growth with Enteros Database Performance Analytics and RevOps Efficiency
Introduction The education sector is experiencing a profound digital transformation. Universities, colleges, online learning providers, K-12 institutions, and corporate learning platforms are increasingly relying on technology to deliver engaging learning experiences, manage academic operations, and support student success. Modern education ecosystems now support: Learning Management Systems (LMS) Student Information Systems (SIS) Online learning platforms Virtual … Continue reading “How to Drive Education Platform Growth with Enteros Database Performance Analytics and RevOps Efficiency”
Why Intelligent Database Workload Management Is Essential for High-Growth SaaS Platforms
- 19 June 2026
- Database Performance Management
Introduction Telecommunications providers are operating in one of the most competitive and technology-intensive industries in the world. While demand for connectivity, mobile services, broadband access, and digital experiences continues to grow, profit margins are increasingly challenged by rising infrastructure costs, complex network operations, and expanding customer expectations. Modern telecom organizations must support: 5G networks Cloud-native … Continue reading “Why Intelligent Database Workload Management Is Essential for High-Growth SaaS Platforms”
Reducing Operational Complexity with AI-Driven Database Observability and AIOps
Modern enterprises operate in increasingly complex digital environments. Applications now span hybrid cloud infrastructures, multi-cloud deployments, containerized platforms, microservices architectures, and globally distributed data systems. While this transformation enables greater scalability, agility, and innovation, it also creates significant operational challenges for IT and engineering teams. At the heart of these complex environments lies the database … Continue reading “Reducing Operational Complexity with AI-Driven Database Observability and AIOps”