SUM() in MySQL — How It Works
What the MySQL SUM() function does, with an example.
Published October 20, 2026
SUM() adds up the values of a numeric column and ignores NULLs. It returns NULL when there are no rows.
How it works
Wrap with IFNULL or COALESCE to return 0.
Try it in MySQL
SELECT SUM(total) FROM orders
48250.00
SELECT customer_id, SUM(total) FROM orders GROUP BY customer_id
per customer
SELECT COALESCE(SUM(total), 0) FROM orders WHERE 1=0
0
Practical examples
Example 1
SELECT SUM(total) FROM orders;
-- 48250.00Example 2
SELECT customer_id, SUM(total) FROM orders GROUP BY customer_id;
-- per customerExample 3
SELECT COALESCE(SUM(total), 0) FROM orders WHERE 1=0;
-- 0Use it in a real query
SELECT SUM(total) FROM orders;Common mistakes
- Getting NULL instead of 0 for empty sets.
- Summing floats and seeing rounding noise (use DECIMAL).
- Mixing SUM with non-grouped columns.
Key takeaways
- SUM() in one line: SUM() adds up the values of a numeric column and ignores NULLs.
- Try queries in a test database first.
- Check NULL handling and data types.
FAQ
SUM of an empty set?
NULL, unless wrapped in COALESCE.
Does SUM() work in other databases?
Most have an equivalent, but names and syntax vary between MySQL, PostgreSQL, SQL Server and SQLite.