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
Driving Efficiency in the Transportation Sector: Enteros’ Cloud FinOps and Database Optimization Solutions
- 18 November 2024
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Empowering Nonprofits with Enteros: Optimizing Cloud Resources Through AIOps Platform
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Optimizing Healthcare Enterprise Architecture with Enteros: Leveraging Forecasting Models for Enhanced Performance and Cost Efficiency
- 15 November 2024
- Database Performance Management
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…
Transforming Banking Operations with Enteros: Leveraging Database Solutions and Logical Models for Enhanced Performance
In the fast-evolving world of finance, where banking and insurance sectors rely on massive data streams for real-time decisions, efficient anomaly man…