SUM Function

SQL · Aggregate Functions

Theory

SUM adds up the values of a numeric column across a set of rows and returns one total. Without GROUP BY it totals the whole table (after WHERE has filtered it). With GROUP BY it produces one total per group, for example total salary per department.

SUM simply ignores NULL values: a NULL is not treated as 0, it is just left out. If every value in the group is NULL, or the group has no rows, SUM returns NULL rather than 0, which surprises many beginners. Wrap it as COALESCE(SUM(col), 0) when you want a 0. You can also sum an expression, such as SUM(quantity * price), or a condition, such as SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) to total only paid orders.

Why we use it

Totals are the backbone of business reporting: revenue, cost, salary bill, units sold. SUM lets the database add millions of rows in one pass and hand back just the numbers you need, per group if you like.

When to use it

Use SUM for additive measures like money, quantity and hours. Do not sum things that are not additive, such as percentages, averages or account balances taken at different dates. For running totals that keep every row, use SUM() OVER instead.

How it works

  1. Rows arrive after FROM and WHERE
  2. GROUP BY splits rows into groups (or one group if absent)
  3. Read the next value in the group
  4. Is the value NULL? Yes: Skip it. No: Add it to the running total.
  5. Any non-NULL value in the group? Yes: Return the total. No: Return NULL (use COALESCE for 0).
  6. One total per group

Syntax

SUM(column)

Example

SELECT SUM(salary) FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic