Sling Academy
Home/PostgreSQL/PostgreSQL aggregation: SUM, AVG, MIN, and MAX

PostgreSQL aggregation: SUM, AVG, MIN, and MAX

Last updated: January 06, 2024

Introduction

Data analysis often requires summarizing data in various ways to extract meaningful insights. In PostgreSQL, this can be accomplished using aggregation functions like SUM, AVG, MIN, and MAX, which allow you to calculate totals, averages, minimum values, and maximum values across a range of data. This tutorial will guide you through using these functions with practical examples.

Using SUM in PostgreSQL

The SUM function calculates the total sum of a numeric dataset. Here’s a simple usage:

SELECT SUM(column_name) FROM table_name;

For example, if you have a sales table:

SELECT SUM(amount) FROM sales;

This will return the total sales amount.

For more advanced usage, you might want to sum sales by category:

SELECT category, SUM(amount) FROM sales GROUP BY category;

Calculating Average with AVG

The AVG function returns the average value of a numeric column. Basic usage is:

SELECT AVG(column_name) FROM table_name;

Using the sales example:

SELECT AVG(amount) FROM sales;

This gives you the average sales amount. To see the average sales amount by salesperson:

SELECT salesperson, AVG(amount) FROM sales GROUP BY salesperson;

Identifying Minimums with MIN

The MIN function retrieves the smallest value in a set. Usage is simple:

SELECT MIN(column_name) FROM table_name;

To find the smallest sale transaction:

SELECT MIN(amount) FROM sales;

Or, to find the smallest sale per category:

SELECT category, MIN(amount) FROM sales GROUP BY category;

Finding Maximum Values with MAX

The MAX function finds the largest value. Basic usage:

SELECT MAX(column_name) FROM table_name;

For the highest sale transaction:

SELECT MAX(amount) FROM sales;

Similarly, to locate the highest sale in each category:

SELECT category, MAX(amount) FROM sales GROUP BY category;

Advanced Aggregation

PostgreSQL allows complex aggregations, like combining functions:

SELECT MIN(amount), MAX(amount), AVG(amount), SUM(amount) FROM sales WHERE date > '2021-01-01';

You can also use these functions in window functions. For example, to get a running total:

SELECT id, amount, SUM(amount) OVER (ORDER BY id) as running_total FROM sales;

To get more detailed insights, using JOIN operations combined with aggregation:

SELECT s.salesperson, SUM(amount) as total_sales FROM sales s JOIN salespeople sp ON s.salesperson_id = sp.id GROUP BY s.salesperson;

Handling Null Values

By default, PostgreSQL ignores NULL values in aggregation functions. However, you can handle them explicitly with COALESCE:

SELECT AVG(COALESCE(column_name, 0)) FROM table_name;

This replaces any NULL values with 0 before calculating the average.

Using Aggregates in HAVING Clauses

The HAVING clause is often used with GROUP BY to filter groups by aggregated data:

SELECT salesperson, SUM(amount) as total_sales FROM sales GROUP BY salesperson HAVING SUM(amount) > 10000;

This will return salespeople with total sales exceeding 10,000.

Performance Considerations

Large datasets can cause slow query performance. Indexes on columns you’re aggregating can improve speed. Consider using EXPLAIN to analyze and adjust your queries as needed.

Conclusion

PostgreSQL’s aggregation functions like SUM, AVG, MIN, and MAX are simple to use but powerful for data analysis. They can be combined with other SQL features for advanced reporting and analytics. Mastering these functions will significantly enhance your ability to extract insights from your data sets.

Next Article: Understanding PostgreSQL: GREATEST and LEAST Functions

Previous Article: PostgreSQL COUNT(*), COUNT(column_name), and COUNT(1): Which is fastest?

Series: PostgreSQL Tutorials: From Basic to Advanced

PostgreSQL

You May Also Like

  • PostgreSQL with TimescaleDB: Querying Time-Series Data with SQL
  • PostgreSQL Full-Text Search with Boolean Operators
  • Filtering Stop Words in PostgreSQL Full-Text Search
  • PostgreSQL command-line cheat sheet
  • How to Perform Efficient Rolling Aggregations with TimescaleDB
  • PostgreSQL with TimescaleDB: Migrating from Traditional Relational Models
  • Best Practices for Maintaining PostgreSQL and TimescaleDB Databases
  • PostgreSQL with TimescaleDB: Building a High-Performance Analytics Engine
  • Integrating PostgreSQL and TimescaleDB with Machine Learning Models
  • PostgreSQL with TimescaleDB: Implementing Temporal Data Analysis
  • Combining PostgreSQL, TimescaleDB, and Airflow for Data Workflows
  • PostgreSQL with TimescaleDB: Visualizing Real-Time Data with Superset
  • Using PostgreSQL with TimescaleDB for Energy Consumption Analysis
  • PostgreSQL with TimescaleDB: How to Query Massive Datasets Efficiently
  • Best Practices for Writing Time-Series Queries in PostgreSQL with TimescaleDB
  • PostgreSQL with TimescaleDB: Implementing Batch Data Processing
  • Using PostgreSQL with TimescaleDB for Network Traffic Analysis
  • PostgreSQL with TimescaleDB: Troubleshooting Common Performance Issues
  • Building an IoT Data Pipeline with PostgreSQL and TimescaleDB