SQL SUM function

SQL Tutorial

Here is a clear, practical guide to the SQL SUM function, with examples, behavior notes, and best practices.


 What SQL SUM function Does

SUM() returns the total of all non-NULL numeric values in a column.


 Basic Syntax

 Basic Examples

1. Total sales amount


2. Total quantity sold

 SUM with WHERE (filter rows first)

 SUM with GROUP BY

Compute totals per category, per customer, etc.


Another example:


 Important Behavior Notes

  • SUM() ignores NULL values
  •  Returns NULL if all rows are NULL
  •  Works only on numeric data types
  •  Often combined with GROUP BY for summary reports

 Real-World Use Cases

Monthly revenue


Total stock value


 

You may also like...