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 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #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

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
A complete guide to add responsive YouTube videos using HTML and CSSCSSOOP, Class and Objects Strategies For Beginners (PHP)PHPHow to create a sticky menu pin using CSS?CSSHow to create a vertically rotating texts using CSS?CSSWordPress: How to setup and get values from an ACF options page?WordPressHTML Copyright SymbolHTML