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.
Filtering Rows Using Aggregate Functions in PostgreSQL
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.
Contact Us