What “average for each row” can mean
To average the columns of one row, add them and divide by the number of columns. To show a group’s average on every row, use AVG() with OVER(). The rest of this article covers both, plus NULL handling and the traps that catch people out.
When people ask how to get the average for each row in SQL, they usually mean one of two things:
- An average across the columns of one row. Each row is one student with a score in each subject column, and you want that student’s average.
- An average that keeps every row. Each row is one test result, and you want every test to show its student’s average next to it.
AVG() handles both, but not in the same way. With GROUP BY, AVG() returns one row per group, so the individual rows are gone. To keep every row, use AVG() with OVER().
The examples use two tables. student_scores has one row per student, with math, science, and english columns. test_results has one row per test, with student_id, subject, test_date, and score columns.
Averaging across columns in the same row
Add the scores in a row and divide by the number of columns:
SELECT
student_id,
(math + science + english) / 3.0 AS avg_score
FROM student_scores;
The 3.0 matters. In SQL Server and PostgreSQL, dividing one integer by another gives an integer, so 7 / 2 returns 3. Dividing by 3.0 keeps the decimal part. MySQL returns a decimal from / anyway, so the 3.0 does no harm there.
This formula breaks when a score is missing. A NULL in the addition makes the whole sum NULL, so the average is NULL too. If a student has no science score, this query returns NULL for that student.
Leaving out missing scores
To average only the scores that exist, count the present scores and divide by that count:
SELECT
student_id,
(COALESCE(math, 0) + COALESCE(science, 0) + COALESCE(english, 0)) * 1.0
/ NULLIF(
(CASE WHEN math IS NULL THEN 0 ELSE 1 END)
+ (CASE WHEN science IS NULL THEN 0 ELSE 1 END)
+ (CASE WHEN english IS NULL THEN 0 ELSE 1 END),
0
) AS avg_score
FROM student_scores;
COALESCE() turns a missing score into 0 so the addition can run. The divisor counts only the scores that are present, so a missing score does not pull the average down. NULLIF(…, 0) returns NULL instead of dividing by zero when every score is missing.
Whether a blank should count as zero depends on your data. If a blank means the student did not take that test, leaving it out is usually the fairer choice. If a blank really means zero, use COALESCE() on each score and divide by the full number of columns.
For a student with a math score of 72 and an English score of 88 and no science score, this query returns 80.0.
Averaging per group with AVG() and GROUP BY
With test results stored one row per test, AVG() with GROUP BY gives one average per student:
SELECT
student_id,
AVG(score) AS avg_score
FROM test_results
GROUP BY student_id;
This returns one row per student. AVG() skips NULL scores, so it averages only the scores that are present. This query works well for a summary, but it removes the individual test rows.
Keeping every row with AVG() OVER
To show each student’s average on every one of that student’s test rows, use AVG() with OVER() and PARTITION BY:
SELECT
student_id,
subject,
score,
AVG(score) OVER (PARTITION BY student_id) AS student_avg
FROM test_results;
PARTITION BY splits the rows into groups, much as GROUP BY does, but the rows stay separate. The average is computed over each group and repeated on every row in it. Other window functions, such as lag(), use the same OVER() syntax.
Running averages over earlier rows
To average the current row with the two rows before it, for each student, add an ORDER BY and a ROWS frame:
SELECT
student_id,
test_date,
score,
AVG(score) OVER (
PARTITION BY student_id
ORDER BY test_date
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
) AS avg_last_three
FROM test_results;
The ROWS clause limits the window to the current row and up to two earlier rows from the same student. The first test has only its own score to average, and the second has two, so those early averages rest on fewer scores.
Window functions are available in current versions of MySQL, PostgreSQL, SQL Server, and SQLite. MySQL 5.7 and earlier do not have them.
Choosing an approach
| What you want | Approach |
|---|---|
| One average across the columns of each row | (col1 + col2 + col3) / 3.0, or the NULL-safe version |
| One average per group, one row per group | AVG() with GROUP BY |
| The group average on every row | AVG() OVER (PARTITION BY ...) |
| The average of the current and earlier rows | AVG() OVER (PARTITION BY ... ORDER BY ... ROWS BETWEEN ...) |
Common mistakes
- Expecting plain AVG() to keep rows. Without GROUP BY or OVER, AVG() returns one value for the whole query. Selecting another column beside it usually causes an error in many databases.
- Dividing integers. In SQL Server and PostgreSQL,
7 / 2returns3. Multiply by1.0or divide by2.0to keep the decimal part. - Truncating averages in SQL Server. The SQL Server AVG() reference says an int expression returns an int, so
AVG(score)on an int column drops the decimal part. Cast first, as inAVG(CAST(score AS DECIMAL(10, 2))). - Turning NULL into 0 without deciding.
AVG(COALESCE(score, 0))counts every missing score as zero and pulls the average down. Replacing NULL with 0 is the fix many people reach for first, and for averages it is usually the wrong one. If you are looking up how to get 0 instead of NULL in SQL, keep this in mind: for averages, leaving NULLs out is usually the fairer result.
Wrapping up
Decide what “row” means before you write the query. Use plain arithmetic for an average across the columns of one row, AVG() with GROUP BY for a summary, and AVG() OVER for a value on every row. Then check the NULL handling and integer division, and test the query on a few rows where you know the answer by hand.