On this page
  1. SUM
  2. Syntax & Overview
  3. Notes & Best Practices
  4. Example

SUM

Prev: `COUNT` | Up: functions | Next: `COALESCE`
Rel: functions, Aggregations, CTEs & Window Functions
Ref: PostgreSQL — SQL Commands
Tags: #public #databases #sql #functions #tier/core


Syntax & Overview

SUM(total_cents)  /* + AVG(), MIN(), MAX() */

Computes sum (or average/min/max) across non-null values in a group.

Notes & Best Practices

Example

SELECT user_id, COALESCE(SUM(amount), 0) AS total_spent
FROM payments
GROUP BY user_id;

Linked from

Related notes