Step 6
8 min read

AVG

Learn to calculate average values from your data.

What You Will Learn

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows

In this lesson, you will understand:

  • What AVG function does
  • How to calculate averages
  • Using AVG with WHERE clause
  • Rounding average results
  • Real-world applications

What is AVG?

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows

AVG calculates the average (mean) of numeric values. It adds all values and divides by the count.

Think of it like:

  • Calculating your average test score
  • Finding average product price
  • Computing average customer age

Basic Syntax

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows
SELECT AVG(column_name) FROM table_name;

Simple Examples

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows

Example 1: Average Age

Result: AVG(age)
AVG(age)
21
1 row
SELECT AVG(age) FROM students;

If ages are: 18, 20, 22, 24 Calculation: (18 + 20 + 22 + 24) / 4 = 21

Example 2: Average with Alias

Result: Average Age with Alias
average_age
21
1 row
SELECT AVG(age) AS average_age FROM students;

Makes the result column name more readable.

Example 3: Average with WHERE

Result: Average Age for Grade A Students
avg_age
21
1 row
SELECT AVG(age) AS avg_age FROM students WHERE grade = 'A';

Calculates average age only for A-grade students.

Rounding Results

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows

AVG often returns many decimal places. Use ROUND to clean it up:

SELECT ROUND(AVG(age), 2) AS avg_age FROM students;

ROUND(value, 2) rounds to 2 decimal places.

Examples:

  • ROUND(21.666666, 2) = 21.67
  • ROUND(21.666666, 1) = 21.7
  • ROUND(21.666666, 0) = 22

AVG Ignores NULL

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows

NULL values are automatically excluded:

Example: If ages are 20, NULL, 22, NULL, 24

  • AVG(age) = (20 + 22 + 24) / 3 = 22
  • NULL values ignored, not counted as 0

Real-World Examples

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows

Example 1: Average Order Value

SELECT ROUND(AVG(total), 2) AS avg_order_value FROM orders WHERE status = 'completed';

Example 2: Average Salary by Department

SELECT department, ROUND(AVG(salary), 2) AS avg_salary FROM employees GROUP BY department;

Example 3: Average Product Rating

SELECT ROUND(AVG(rating), 1) AS avg_rating FROM reviews WHERE product_id = 101;

Example 4: Average Response Time

SELECT ROUND(AVG(response_time_ms), 0) AS avg_response FROM api_logs WHERE date = CURRENT_DATE;

Common Mistakes

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows

Mistake 1: Using AVG on Text

Wrong:

SELECT AVG(name) FROM students; -- Error: Cannot calculate average of text

Correct:

SELECT AVG(age) FROM students;

Mistake 2: Using AVG in WHERE

Wrong:

SELECT * FROM products WHERE price > AVG(price);

Correct:

SELECT * FROM products WHERE price > (SELECT AVG(price) FROM products);

Mistake 3: Not Rounding

Hard to read:

SELECT AVG(price) FROM products; -- Result: 125.4567891234

Better:

SELECT ROUND(AVG(price), 2) FROM products; -- Result: 125.46

Summary

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows

Key points to remember:

  1. AVG calculates mean of numeric values
  2. Automatically ignores NULL values
  3. Returns decimal number (often many digits)
  4. Use ROUND for cleaner output
  5. Can combine with WHERE to filter
  6. Cannot use directly in WHERE clause
  7. Use with GROUP BY for averages per group
  8. Only works on numeric columns
  9. If all values are NULL, returns NULL
  10. Common for salaries, prices, scores, ages

What Comes Next

Sample Students Table
idnameagegrade
1John20A
2Mary18B
3Peter22A
4Sarah24B
4 rows

Now you know how to calculate averages. Next, you will learn about MIN and MAX to find the smallest and largest values.

SQL Editor
Loading...

Calculate average age of all students

Output

Click "Run Query" to see results

SQL Editor
Loading...

Calculate and round average age

Output

Click "Run Query" to see results

SQL Editor
Loading...

Average age of A-grade students only

Output

Click "Run Query" to see results

Finished this topic?

Mark it complete to track your progress and maintain your streak!

SkillsetMaster - AI, Web Development & Data Analytics Courses