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
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
How to Use HAVING With Aggregate Functions in SQL? The SQL HAVING clause is an essential tool for filtering aggregated data after performing group operations. While the WHERE clause filters data before grouping, HAVING clause allows us to apply conditions to the results of aggregate functions, such as SUM(), COUNT(), AVG(), MIN() and MAX.Using HAVIN
4 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 - ARRAY_AGG() Function The PostgreSQL ARRAY_AGG function is a aggregate function that allows users to combine values from multiple rows into an array. This function is particularly useful when managing grouped data and returning multiple values in a single row. In this article, We will learn about the What is PostgreSQL A
4 min read
SQL Aggregate functions SQL Aggregate Functions are used to perform calculations on a set of rows and return a single value. These functions are particularly useful when we need to summarize, analyze, or group large datasets in SQL databases. Whether you are working with sales data, employee records or product inventories,
4 min read