On this page
COALESCE
Prev: `SUM` | Up: functions | Next: `NULLIF`
Rel: functions, Aggregations, CTEs & Window Functions
Ref: PostgreSQL — SQL Commands
Tags: #public #databases #sql #functions #tier/core
Syntax & Overview
COALESCE(nickname, full_name, 'Anonymous')
Returns the first non-NULL argument from left to right (short-circuiting).
- Category / Stage: Null Handling
- Dialects: ANSI, SQLite, PostgreSQL
Notes & Best Practices
- Returns the first non-
NULLargument: Ideal for default values inSELECTand safe math (COALESCE(SUM(amount), 0)).
Example
SELECT name, COALESCE(nickname, name, 'Anonymous') AS display_name
FROM users;
Linked from
Related notes
- tuts/databases/sql/functions/NULLIF Linked here; links to this page; shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/SUM Linked here; links to this page; shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions Linked here; links to this page; shared tags: #databases, #functions, #sql
- tuts/databases/sql/aggregations_ctes_and_windows Linked here; shared tags: #databases, #sql
- tuts/databases/sql/functions/CASE shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/CAST shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/COUNT shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/CURRENT_TIMESTAMP shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/LAG shared tags: #databases, #functions, #sql, #tier
- tuts/databases/sql/functions/LOWER shared tags: #databases, #functions, #sql, #tier