Yogesh Chauhan's Blog

Aggregate Functions Examples in Postgres for Beginners

in Postgres on May 9, 2020

Like most other relational database products, PostgreSQL supports aggregate functions.

An aggregate function computes a single result from multiple input rows. For example, there are aggregates to compute the count, sum, avg (average), max (maximum) and min (minimum) over a set of rows.

MAX

For example if you want to find out the product with highest unit price then we can query like this:

I am using the database available on my Github public repo


SELECT MAX(unit_price) FROM products;

//Output

max
263.5

MIN

if you want to find out the product with lowest unit price then we can query like this:


SELECT MIN(unit_price) FROM products;

//Output

min
2.5

COUNT

If you want to know about the number of products in inventory that you have, we can query like this:


SELECT COUNT(product_id) FROM products;

//Output
count
77

SUM

If you want to know about the total cost of inventory you have, we can query like this:


SELECT SUM(unit_price) FROM products;

//Output
sum
2220.2102

AVG

If you want to know about the average cost of inventory you have, we can query like this:


SELECT AVG(unit_price) FROM products;


//Output
avg
28.83389609200614

We can always round up those numbers using ROUND function.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
Should we ever delete data from a database?MiscellaneousHow to Commit and Rollback Changes in SQL?SQL/MySQLFETCH clause in PostgreSQLPostgresHow to create a circle that follows a cursor using JavaScript and CSS?CSSAt-rules: @error and @warn in SCSSSCSSHow to add Date and Time picker in HTML and get the values using JavaScript?HTML