YogeshChauhan.com
Aggregate Functions Examples in Postgres for Beginners
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.

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 4 The substr() method in JavaScript and how it’s different from substring() Mar 4 A complete guide to add responsive YouTube videos using HTML and CSS Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript Mar 3 Window setInterval() Method in JavaScript

You might also like these

addcslashes() and addslashes() String Functions in PHPPHPWhat is Hadoop and Hadoop Ecosystem?MiscCustom Post Type Template Files in WordPressWordPressHow to create a new HTML element programmatically using JavaScript?HTMLContent types in DrupalDrupalSolution to pod install fails with json error on Mac OS X 10.15 (or Mac OS Catalina)Misc