Filtering Rows Using Aggregate Functions in PostgreSQL
Last Updated :
15 Apr, 2024
PostgreSQL is an advanced relational database system that supports both relational (SQL) and non-relational (JSON) queries. It is free and open-source. Filtering rows based on conditions is a regular operation in database administration.
Although filtering rows by each column value is easily done, more advanced filtering needs which may require aggregate functions usage occur more often. In this article, we will go into detail about filtering rows using aggregate functions in PostgreSQL, and see some example queries of filtering rows using aggregate functions.
Aggregate Functions
Aggregate functions perform a calculation on a set of values and return a single value. These functions are particularly useful when dealing with large datasets or when summarizing data across multiple rows. Common aggregate functions include:
Filtering Rows Using Aggregate Functions
The ability of the aggregate functions to work can be seen better when they are applied together with the HAVING clause to filter rows based on aggregated values. The HAVING condition works in the same way as the WHERE condition but is designed specifically to filter collections of rows returned by aggregate queries.
Now we do some practical examples to display the process of adding aggregate functions and filtering rows.
Let's start by creating a simple table, putting some sample data in, and then showing a few filters of rows using aggregate functions in PostgreSQL.
Create Table
CREATE TABLE sales (
salesperson VARCHAR(50),
sales_amount NUMERIC
);
Insert Data
INSERT INTO sales (salesperson, sales_amount) VALUES
('Minal', 5000),
('Priyanshi', 8000),
('Mridul', 7000),
('Asad', 12000),
('Maram', 6000),
('Mahi', 10000);
Output:
Now, we have a table named "sales" with some sample data.
Sales TableExamples of Filtering Rows Using Aggregate Functions in PostgreSQL
Example 1: Filtering Rows Based on Total Sales Greater Than 10,000.
Query:
SELECT salesperson, SUM(sales_amount) AS total_sales
FROM sales
GROUP BY salesperson
HAVING SUM(sales_amount) > 10000;
Explanation:
- SELECT salesperson, SUM(sales_amount) AS total_sales: It selects the salesperson column and calculates the total sales amount for each salesperson using the SUM aggregate function. The alias "total_sales" is assigned to the calculated sum.
- FROM sales: Specifies the table from which the data will be retrieved.
- GROUP BY salesperson: Groups the results by the salesperson column, so that the aggregate function operates on each distinct salesperson.
- HAVING SUM(sales_amount) > 10000: This clause filters the grouped rows based on the aggregated total sales amount. It ensures that only groups with a total sales amount greater than $10,000 are included in the final result set.
Output:
This query will return salespersons who have achieved a total sales amount greater than 10,000.
Using SUM FunctionExample 2: Finding the Average Sales Amount.
Query:
SELECT AVG(sales_amount) AS average_sales
FROM sales;
Explanation:
- SELECT AVG(sales_amount) AS average_sales: It calculates the average sales amount for each salesperson using the AVG aggregate function. The alias "average_sales" is assigned to the calculated average.
- FROM sales: This specifies the table which is sales here.
Output:
This query calculates the average sales amount across all transactions.
Using AVG FunctionExample 3: Identifying the Maximum Sales Amount by Salesperson.
Query:
SELECT salesperson, MAX(sales_amount) AS max_sales
FROM sales
GROUP BY salesperson;
Explanation:
- SELECT salesperson, MAX(sales_amount) AS max_sales: It selects the salesperson column and calculates the maximum sales amount for each salesperson using the MAX aggregate function. The alias "max_sales" is assigned to the calculated maximum sales.
- FROM sales: Specifies the table from which the data will be retrieved.
- GROUP BY salesperson: Group the results by the salesperson column, thereby performing aggregate operation on each distinct salesperson.
Output:
Here, we find the maximum sales amount for each salesperson.
Using MAX FunctionExample 4: Counting the Number of Transactions for Each Salesperson.
Query:
SELECT salesperson, COUNT(*) AS transaction_count
FROM sales
GROUP BY salesperson;
Explanation:
- SELECT salesperson, COUNT(*) AS transaction_count: It selects the salesperson column calculates the count of transactions for each salesperson using the COUNT(*) aggregate function. The alias "transaction_count" is assigned to the count.
- FROM sales: Specifies the table from which the data will be retrieved.
- GROUP BY salesperson: Group the results by the salesperson column, thereby performing aggregate operation on each distinct salesperson.
Output:
This query counts the number of transactions made by each salesperson.
Using COUNT FunctionExample 5: Find the minimum sales amount for each salesperson.
Query:
SELECT salesperson, MIN(sales_amount) AS min_sales
FROM sales
GROUP BY salesperson;
Explanation:
- SELECT salesperson, MIN(sales_amount) AS min_sales: It selects the salesperson column and calculates the minimum sales amount for each salesperson using the MIN aggregate function. The alias "min_sales" is assigned to the calculated minimum sales.
- FROM sales: Specifies the table from which the data will be retrieved.
- GROUP BY salesperson: Group the results by the salesperson column, thereby performing aggregate operation on each distinct salesperson.
Output:
This query retrieves the minimum sales amount recorded for each salesperson in the "sales" table.
Using MIN FunctionConclusion
The capability of PostgreSQL to go filtering rows with aggregate functions offers a wide range of data analysis and manipulation possibilities. Regardless of the use of aggregate functions for data across rows or for detecting patterns within your dataset, you can extract information meaningfully in a matter of seconds. Through the mechanisms detailed in the present article you'll be conversant with complex data filtering techniques and this way, you won't hesitate to face difficult tasks.
Similar Reads
Using SQLite Aggregate functions in Python
In this article, we are going to see how to use the aggregate function in SQLite Python. An aggregate function is a database management function that groups the values of numerous rows into a single summary value. Average (i.e., arithmetic mean), sum, max, min, Count are common aggregation functions
3 min read
PostgreSQL - STRING_AGG() Function
The STRING_AGG() function in PostgreSQL is a powerful aggregate function used to concatenate a list of strings with a specified separator. This function is essential for combining string values from multiple rows into a single string, making data aggregation more efficient and readable. Let us get a
2 min read
How to Use SELECT With Aggregate Functions in SQL?
SQL aggregate functions are essential tools for summarizing and processing data. These functions help us perform calculations on a set of values to produce a single result, such as SUM, COUNT, AVG, MAX, and MIN. These functions work with the SELECT statement to process data and derive meaningful ins
4 min read
DAX Aggregate Functions in Power BI
Microsoft Power BI is a tool that helps businesses to analyze data and create interactive reports and visualizations. It can connect to various data sources such as Excel, SQL databases, cloud services, etc and can perform aggregate functions on them for analysis. In this article we will learn about
6 min read
How to Use HAVING With Aggregate Functions in SQL?
The HAVING clause in SQL is a powerful tool used to filter grouped data based on conditions applied to aggregate functions. While the WHERE clause is used to filter rows before grouping, the HAVING clause filters aggregated results after grouping. This makes it essential for queries involving calcul
4 min read
PostgreSQL - User Defined Functions
PostgreSQL, one of the most powerful open-source relational database management systems (RDBMS), provides a strong feature set for creating and utilizing user-defined functions (UDFs). By using user-defined functions, we can enhance the modularity, maintainability, and performance of our database ap
5 min read
Aggregate data using custom functions using R
In this article, we will explore various methods to aggregate data using custom functions by using the R Programming Language. What is a custom function?Custom functions are an essential part of R programming, which allows users to create reusable blocks of code tailored to their specific needs. The
5 min read
PostgreSQL - Deleting Duplicate Rows using Subquery
In PostgreSQL, handling duplicate rows is a common task, especially when working with large datasets. Fortunately, PostgreSQL provides several techniques to efficiently delete duplicate rows, and one of the most effective approaches is using subqueries.In this article, we will demonstrate how to ide
3 min read
PostgreSQL - CREATE FUNCTION Statement
PostgreSQL allows developers to create user-defined functions to encapsulate reusable logic, making database operations more efficient and modular. The CREATE FUNCTION statement is used to define a new function, supporting various procedural languages, with plpgsql being the most commonly used in Po
5 min read
PostgreSQL Date Functions
PostgreSQL is widely recognized for its comprehensive support for date and time manipulations, making it an excellent choice for applications requiring precise time management and complex calculations. This article explores the core PostgreSQL date functions, covering how to retrieve the current dat
4 min read