SQL AVG
AVG returns the mean of a numeric column, ignoring NULLs. The result often has many decimals, so pair it with ROUND(value, 2).
Syntax
SELECT ROUND(AVG(column), 2) FROM table_name;
Try it yourself
Run the example below on a real PostgreSQL database, then change it and run it again. Nothing to install, no sign-up.
avg.sql · PostgreSQL
Loading editor…
Examples
1. Average price
SELECT ROUND(AVG(price), 2) FROM products;
Rounded to cents.
2. Average per group
SELECT category, ROUND(AVG(price), 2) FROM products GROUP BY category;
One average per category.
Common mistakes
- Expecting NULLs to count as zero. They are skipped, which raises the average.
Practice: test yourself
This challenge is from our interactive course. Write the query and press Check answer.
Challenge · 10 pts
What is the average product price, rounded to 2 decimal places?
challenge.sql · PostgreSQL
Loading editor…
FAQ
How do I calculate a median?
PostgreSQL has PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY column).
Learn SQL step by step
65 hands-on exercises, streaks, a leaderboard and a certificate.