MariaDB COUNT, SUM, AVG, MAX, and MIN: What each function returns
In MariaDB, COUNT() counts, SUM() adds, AVG() averages, and MAX() and MIN() find the largest and smallest values. COUNT(*) counts all rows. When these functions take a column as their argument, they ignore NULL. A NULL score is unknown, while zero is a recorded score.
Prepare the sample scores and compare the functions
| Function | Result | Value in this sample |
|---|---|---|
| COUNT(*) | All rows | 5 |
| COUNT(score) | Known scores | 4 |
| SUM(score) | Total of known scores | 50.00 |
| AVG(score) | Arithmetic mean of known scores | 12.50 |
| MAX(score), MIN(score) | Largest and smallest score | 20.00, 0.00 |
Use a MariaDB server, a SQL client, and a practice database named learning. Run the setup once and keep the same connection for the queries. If the table name is taken, use a separate practice database. The five scores are 10, 20, 20, NULL, and 0.
USE learning;
CREATE TABLE scores_aggregate_demo (
id INT PRIMARY KEY,
team CHAR(1) NOT NULL,
score DECIMAL(8,2) NULL
);
INSERT INTO scores_aggregate_demo VALUES
(1, 'A', 10), (2, 'A', 20),
(3, 'B', 20), (4, 'B', NULL), (5, 'B', 0);
Use COUNT to test what gets counted
SELECT COUNT(*) AS all_rows,
COUNT(score) AS known_scores,
COUNT(DISTINCT score) AS distinct_scores
FROM scores_aggregate_demo;
The result is five total rows, four known scores, and three distinct scores. Zero counts, and COUNT(DISTINCT score) counts the two 20s once. The difference COUNT(*) - COUNT(score) is one, so one score is missing. Use WHERE score IS NULL to find the source row. MariaDB Docs · COUNT() MariaDB Docs · COUNT(DISTINCT)
Compare NULL, zero, and DISTINCT in SUM and AVG
SELECT SUM(score) AS total_score,
ROUND(AVG(score), 2) AS average_score,
ROUND(AVG(COALESCE(score, 0)), 2) AS average_with_zero,
COALESCE(ROUND(AVG(score), 2), 0) AS average_display
FROM scores_aggregate_demo;
The sum is 50.00 and the average is 12.50, computed over the four known scores. Putting COALESCE(score, 0) inside AVG counts the missing score as zero and lowers the average to 10.00. By contrast, COALESCE(AVG(score), 0) only supplies a fallback when the aggregate returns NULL. It leaves this sample’s average at 12.50. MariaDB Docs · SUM() MariaDB Docs · AVG() MariaDB Docs · COALESCE()
SELECT SUM(DISTINCT score) AS distinct_total,
ROUND(AVG(DISTINCT score), 2) AS distinct_average
FROM scores_aggregate_demo;
DISTINCT inside the aggregate keeps only distinct values, giving a sum of 30.00 and an average of 10.00. Two different people can legitimately score 20, so both belong in the ordinary sum. If a JOIN seems to repeat scores, count occurrences of the source ID before deciding what to deduplicate.
Find extreme values and retrieve tied rows
SELECT MAX(score) AS maximum_score, MIN(score) AS minimum_score
FROM scores_aggregate_demo;
SELECT id, team, score
FROM scores_aggregate_demo
WHERE score = (SELECT MAX(score) FROM scores_aggregate_demo)
ORDER BY id;
SELECT id, team, score
FROM scores_aggregate_demo
WHERE score IS NOT NULL
ORDER BY score DESC, id ASC
LIMIT 1;
The first query returns a maximum of 20.00 and a minimum of 0.00. The second returns all the rows with the maximum, including ID 2 in team A and ID 3 in team B. Adding team next to MAX(score) does not retrieve the team from the winning source row. MariaDB Docs · MAX() MariaDB Docs · MIN()
If you need exactly one known score and the smallest ID should win a tie, use ORDER BY score DESC, id ASC with LIMIT 1, as in the third query. It returns ID 2 in this fixture. If every score is NULL, it returns no rows.
Read team totals and distinguish empty input
SELECT team, COUNT(*) AS row_count, COUNT(score) AS known_scores,
SUM(score) AS total_score, ROUND(AVG(score), 2) AS average_score,
MAX(score) AS maximum_score, MIN(score) AS minimum_score
FROM scores_aggregate_demo
GROUP BY team
ORDER BY team;
Team A has two rows, two known scores, a total of 30.00, and an average of 15.00. Team B has three rows, two known scores, a total of 20.00, and an average of 10.00. See the comparison of GROUP BY, WHERE, and HAVING for how grouping keys and filter placement change the output.
SELECT COUNT(*) AS row_count, COUNT(score) AS known_scores,
SUM(score) AS total_score, AVG(score) AS average_score,
MAX(score) AS maximum_score, MIN(score) AS minimum_score
FROM scores_aggregate_demo
WHERE team = 'C';
No source rows match team C. The final aggregate has no grouping key and returns one row with both counts at 0 and the other four values NULL. If rows match but all their scores are NULL, COUNT(*) is positive. Before displaying zero, decide whether it denotes a recorded zero, a missing score, or the absence of source rows.




