PostgreSQL Tutorial

  • Home
  • Stored Procedures
  • Triggers
  • Views
  • Interfaces
    • PostgreSQL PHP
    • PostgreSQL Python
    • PostgreSQL JDBC
  • Functions
    • Aggregate Functions
    • Date / Time Functions
    • String Functions
    • Math Functions
Home / PostgreSQL Tutorial / PostgreSQL GROUP BY

PostgreSQL GROUP BY

Summary: in this tutorial, you will learn how to divide rows into groups by using the PostgreSQL GROUP BY clause.

Introduction to PostgreSQL GROUP BY clause

The GROUP BY clause divides the rows returned from the SELECT statement into groups. For each group, you can apply an aggregate function e.g., SUM to calculate the sum of items or COUNT to get the number of items in the groups.

The following statement illustrates the syntax of the GROUP BY clause:

1
2
3
SELECT column_1, aggregate_function(column_2)
FROM tbl_name
GROUP BY column_1;

The GROUP BY clause must appear right after the FROM or WHERE clause. Followed by the GROUP BY clause is one column or a list of comma-separated columns. You can also put an expression in the GROUP BY clause.

PostgreSQL GROUP BY clause examples

Let’s take a look at the payment table in the sample database.

payment table

PostgreSQL GROUP BY without aggregate function

You can use the GROUP BY clause without applying an aggregate function. The following query gets data from the payment table and groups the result by customer id.

1
2
3
4
5
6
SELECT
customer_id
FROM
payment
GROUP BY
customer_id;

PostgreSQL GROUP BY without aggregate function

In this case, the GROUP BY acts like the DISTINCT clause that removes the duplicate rows from the result set.

PostgreSQL GROUP BY with SUM function example

The GROUP BY clause is useful when it is used in conjunction with an aggregate function. For example, to get how much a customer has been paid, you use the GROUP BY clause to divide the payments table into groups; for each group, you calculate the total amounts of money by using the SUM function as the following query:

1
2
3
4
5
6
7
SELECT
customer_id,
SUM (amount)
FROM
payment
GROUP BY
customer_id;

PostgreSQL GROUP BY customer

The GROUP BY clause sorts the result set by customer id and adds up the amount that belongs to the same customer. Whenever the customer_id changes, it adds the row to the returned result set.

You can use the ORDER BY clause with GROUP BY clause to sort the groups:

1
2
3
4
5
6
7
8
9
SELECT
customer_id,
SUM (amount)
FROM
payment
GROUP BY
customer_id
ORDER BY
SUM (amount) DESC;

PostgreSQL GROUP BY with ORDER BY

PostgreSQL GROUP BY with COUNT function example

To count the number of transactions each staff has been processing, you group the payments table based on staff id and use the COUNT function to get the number of transactions as the following query:

1
2
3
4
5
6
7
SELECT
staff_id,
COUNT (payment_id)
FROM
payment
GROUP BY
staff_id;

PostgreSQL GROUP BY staff

The GROUP BY clause sorts the result set by staff id. It keeps a running total of rows, and whenever the staff id changes, it adds the row to the returned result set.

To filter groups, you use the HAVING clause instead of WHERE clause.

In this tutorial, we have shown you how to use the PostgreSQL GROUP BY clause to divide a result set into groups.

Previous Tutorial: PostgreSQL Cross Join By Example
Next Tutorial: PostgreSQL HAVING

PostgreSQL Quick Start

  • What is PostgreSQL?
  • Install PostgreSQL
  • Connect to Database
  • Download PostgreSQL Sample Database
  • Load Sample Database
  • Explore Server and Database Objects

PostgreSQL Fundamentals

  • PostgreSQL Select
  • PostgreSQL Order By
  • PostgreSQL Select Distinct
  • PostgreSQL Where
  • PostgreSQL LIMIT
  • PostgreSQL IN
  • PostgreSQL Between
  • PostgreSQL Like
  • PostgreSQL Inner Join
  • PostgreSQL Left Join
  • PostgreSQL Full Outer Join
  • PostgreSQL Cross Join
  • PostgreSQL Natural Join
  • PostgreSQL Group By
  • PostgreSQL Having
  • PostgreSQL Union
  • PostgreSQL Intersect
  • PostgreSQL Except
  • PostgreSQL Subquery
  • PostgreSQL Insert
  • PostgreSQL Update
  • PostgreSQL Delete

Managing Table Structure

  • PostgreSQL Data Types
  • PostgreSQL Create Table
  • PostgreSQL Alter Table
  • PostgreSQL Drop Table
  • PostgreSQL Truncate Table
  • PostgreSQL CHECK Constraint
  • PostgreSQL Not-Null Constraint
  • PostgreSQL Foreign Key
  • PostgreSQL Primary Key
  • PostgreSQL UNIQUE Constraint

PostgreSQL Views

  • Managing PostgreSQL Views
  • Creating Updatable Views
  • PostgreSQL Materialized Views
  • The WITH CHECK OPTION Views
  • PostgreSQL Recursive View

PostgreSQL Triggers

  • Introduction to Trigger
  • Creating A Trigger
  • Managing PostgreSQL Triggers

About PostgreSQL Tutorial

PostgreSQLTutorial.com is a website dedicated to developers and database administrators who are working on PostgreSQL database management system.

We constantly publish useful PostgreSQL tutorials to keep you up-to-date with the latest PostgreSQL features and technologies. All PostgreSQL tutorials are simple, easy-to-follow and practical.

Recent PostgreSQL Tutorials

  • PostgreSQL ANY Operator
  • PostgreSQL EXISTS
  • How To Delete Duplicate Rows in PostgreSQL
  • PostgreSQL TO_CHAR Function
  • PostgreSQL TO_NUMBER Function
  • PostgreSQL TO_TIMESTAMP Function
  • PostgreSQL CEIL Function
  • PostgreSQL MOD Function
  • PostgreSQL FLOOR Function
  • PostgreSQL ABS Function

More Tutorials

  • PostgreSQL Cheat Sheet
  • PostgreSQL Administration
  • PostgreSQL PHP
  • PostgreSQL Python
  • PostgreSQL JDBC
  • PostgreSQL Resources

Site Info

  • Home
  • About Us
  • Contact Us
  • Privacy Policy

Copyright © 2017 by PostgreSQL Tutorial Website. All Rights Reserved.