🔍
👶 Kids📝 Blog About Contact 🚀 Get Started Free

SQL – SUM

Add up all values in a numeric column using the SUM() aggregate function.

SUM() totals all non-NULL values in a numeric column. It is the cornerstone of financial reporting — total revenue, total expenses, sum of all scores, combined inventory value. Without SUM(), you would need to pull every individual value into your application and add them yourself, massively increasing data transfer and slowing everything down.

Combine SUM() with GROUP BY to get subtotals per category — the total sales per region, total enrollment fees per course, total score per grade group. This pattern is one of the most common in business intelligence.

SUM with GROUP BY — the power combo

SELECT category, SUM(price) AS total_value
FROM courses
GROUP BY category;

Try It Yourself — Interactive SQL Editor

Edit the query below and click Run Query ▶ to see live results powered by SQLite running directly in your browser.

SQLite – edit & run
Results
← Click Run Query ▶ to see results

Key Points

  • SUM ignores NULL values
  • SUM only works on numeric columns
  • Combine with GROUP BY for per-category totals
  • SUM(CASE WHEN … THEN 1 ELSE 0 END) is a conditional count trick

Pro Tip from CodesCompiler: The best way to learn SQL is to break things intentionally — modify the query above, change the WHERE conditions, try different columns. Every error teaches you something the docs cannot.

In the next lesson, we continue exploring SQL’s powerful feature set to build your database mastery.