🔧 Quick Syntax
SELECT AVG(column_name)
FROM table_name;
This tells SQL:
“Go through all the numbers in this column and tell me the average.”
🧾 Here’s the Table You’re Working With
You’ve got a table called orders
:
order_id | customer_name | total_amount |
---|---|---|
1 | Fenn Rask | 10.00 |
2 | Milo Kresh | 45.00 |
3 | Sora Dune | 15.99 |
4 | Lexa Vohn | 72.50 |
5 | Tovi Glint | 29.99 |
✅ Suppose You Want the Average Order Value
SELECT AVG(total_amount)
FROM orders;
💡 Output:
34.996
SQL added up all the total_amount
values and divided by how many there are.
Just like a regular average — but automatic.
✅ What Happens If One of the Values Is NULL?
SELECT AVG(total_amount)
FROM orders;
SQL will ignore any rows where total_amount
is NULL
and calculate the average from the rest. No errors — just a clean result.
✅ Suppose You Want the Average for Orders Over $30
SELECT AVG(total_amount)
FROM orders
WHERE total_amount > 30;
💡 Output:
58.75
Only the qualifying rows (over $30) are included in the average. In this case, just Milo and Lexa.
🧃 Recap – What You Learned
AVG()
calculates the average of a columnNULL
values are ignored- Combine with
WHERE
to average specific rows - Great for prices, scores, ratings, revenue per user, and more