COALESCE returns the first non-NULL value from a list of expressions. It's essential for handling NULL values, providing default values, and consolidating data from multiple columns. Evaluates arguments from left to right and stops at the first non-NULL value.
Syntax
COALESCE(value1, value2, ..., valueN)
COALESCE(value1, value2, ..., valueN)
COALESCE(value1, value2, ..., valueN)
Returns: The first non-NULL value, or NULL if all values are NULL
Platform-Specific Notes
Snowflake:
Also supports NVL(expr1, expr2) - 2-argument version
IFNULL(expr1, expr2) - alias for NVL
IFF(condition, true_val, false_val) - conditional alternative
BigQuery:
Also supports IFNULL(expr1, expr2) - 2-argument version
IF(condition, true_val, false_val) - conditional alternative
Databricks:
Also supports NVL(expr1, expr2) - 2-argument version
SELECT
order_id,
customer_id,-- Use shipping address if billing address is missing
COALESCE(billing_address, shipping_address)as invoice_address,-- Use estimated date if actual date is missing
COALESCE(actual_delivery_date, estimated_delivery_date)as delivery_date,-- Use default values for missing metrics
COALESCE(discount_amount,0)as discount,
COALESCE(tax_amount, order_total * 0.08)as tax,
order_total + COALESCE(shipping_cost,9.99)as total_with_shipping
FROM
SELECT
order_id,
customer_id,-- Use shipping address if billing address is missing
COALESCE(billing_address, shipping_address)as invoice_address,-- Use estimated date if actual date is missing
COALESCE(actual_delivery_date, estimated_delivery_date)as delivery_date,-- Use default values for missing metrics
COALESCE(discount_amount,0)as discount,
COALESCE(tax_amount, order_total * 0.08)as tax,
order_total + COALESCE(shipping_cost,9.99)as total_with_shipping
FROM
SELECT
order_id,
customer_id,-- Use shipping address if billing address is missing
COALESCE(billing_address, shipping_address)as invoice_address,-- Use estimated date if actual date is missing
COALESCE(actual_delivery_date, estimated_delivery_date)as delivery_date,-- Use default values for missing metrics
COALESCE(discount_amount,0)as discount,
COALESCE(tax_amount, order_total * 0.08)as tax,
order_total + COALESCE(shipping_cost,9.99)as total_with_shipping
FROM
Sample Data (orders table):
order_id
customer_id
billing_address
shipping_address
actual_delivery_date
estimated_delivery_date
discount_amount
tax_amount
order_total
shipping_cost
1001
101
123 Main St
123 Main St
2024-01-20
2024-01-18
10.00
NULL
100.00
5.00
1002
102
NULL
456 Oak Ave
NULL
2024-01-25
NULL
8.00
150.00
NULL
1003
103
789 Elm St
789 Elm St
2024-01-22
2024-01-23
15.00
12.00
200.00
0.00
Result:
order_id
invoice_address
delivery_date
discount
tax
total_with_shipping
1001
123 Main St
2024-01-20
10.00
8.00
105.00
1002
456 Oak Ave
2024-01-25
0.00
8.00
159.99
1003
789 Elm St
2024-01-22
15.00
12.00
200.00
Example 4: Aggregation with NULL Handling
All Platforms:
SELECT
department,COUNT(*)as total_employees,-- Count employees with bonus (NULL-safe)COUNT(bonus_amount)as employees_with_bonus,-- Average bonus, treating NULL as 0
AVG(COALESCE(bonus_amount,0))as avg_bonus_including_zeros,-- Average bonus, excluding NULLs
AVG(bonus_amount)as avg_bonus_excluding_nulls,-- Total bonus pool
SUM(COALESCE(bonus_amount,0))as total_bonus_pool
FROM employees
GROUPBY
SELECT
department,COUNT(*)as total_employees,-- Count employees with bonus (NULL-safe)COUNT(bonus_amount)as employees_with_bonus,-- Average bonus, treating NULL as 0
AVG(COALESCE(bonus_amount,0))as avg_bonus_including_zeros,-- Average bonus, excluding NULLs
AVG(bonus_amount)as avg_bonus_excluding_nulls,-- Total bonus pool
SUM(COALESCE(bonus_amount,0))as total_bonus_pool
FROM employees
GROUPBY
SELECT
department,COUNT(*)as total_employees,-- Count employees with bonus (NULL-safe)COUNT(bonus_amount)as employees_with_bonus,-- Average bonus, treating NULL as 0
AVG(COALESCE(bonus_amount,0))as avg_bonus_including_zeros,-- Average bonus, excluding NULLs
AVG(bonus_amount)as avg_bonus_excluding_nulls,-- Total bonus pool
SUM(COALESCE(bonus_amount,0))as total_bonus_pool
FROM employees
GROUPBY
Sample Data (employees table):
employee_id
department
salary
bonus_amount
1
Sales
75000
5000
2
Sales
65000
NULL
3
Sales
80000
6000
4
IT
90000
7000
5
IT
85000
NULL
6
IT
95000
NULL
Result:
department
total_employees
employees_with_bonus
avg_bonus_including_zeros
avg_bonus_excluding_nulls
total_bonus_pool
Sales
3
2
3666.67
5500.00
11000
IT
3
1
2333.33
7000.00
7000
Example 5: Multi-Source Data Consolidation
All Platforms:
SELECT
user_id,-- Try profile name, then account name, then email prefix, then 'Anonymous'
COALESCE(
profile_display_name,
account_full_name,
SPLIT_PART(email,'@',1),'Anonymous User')as display_name,-- Get most recent activity timestamp from multiple sources
COALESCE(
last_purchase_date,
last_login_date,
account_created_date
)as last_activity,-- User timezone preference with fallback
COALESCE(
user_timezone,
account_country_timezone,'UTC')as timezone
FROM
SELECT
user_id,-- Try profile name, then account name, then email prefix, then 'Anonymous'
COALESCE(
profile_display_name,
account_full_name,
SPLIT_PART(email,'@',1),'Anonymous User')as display_name,-- Get most recent activity timestamp from multiple sources
COALESCE(
last_purchase_date,
last_login_date,
account_created_date
)as last_activity,-- User timezone preference with fallback
COALESCE(
user_timezone,
account_country_timezone,'UTC')as timezone
FROM
SELECT
user_id,-- Try profile name, then account name, then email prefix, then 'Anonymous'
COALESCE(
profile_display_name,
account_full_name,
SPLIT_PART(email,'@',1),'Anonymous User')as display_name,-- Get most recent activity timestamp from multiple sources
COALESCE(
last_purchase_date,
last_login_date,
account_created_date
)as last_activity,-- User timezone preference with fallback
COALESCE(
user_timezone,
account_country_timezone,'UTC')as timezone
FROM
*dbt® and dbt Core® are federally registered trademarks of dbt Labs, Inc. in the United States and various jurisdictions around the world. Paradime is not a partner of dbt Labs. All rights therein are reserved to dbt Labs. Paradime is not a product or service of or endorsed by dbt Labs, Inc.
*dbt® and dbt Core® are federally registered trademarks of dbt Labs, Inc. in the United States and various jurisdictions around the world. Paradime is not a partner of dbt Labs. All rights therein are reserved to dbt Labs. Paradime is not a product or service of or endorsed by dbt Labs, Inc.
*dbt® and dbt Core® are federally registered trademarks of dbt Labs, Inc. in the United States and various jurisdictions around the world. Paradime is not a partner of dbt Labs. All rights therein are reserved to dbt Labs. Paradime is not a product or service of or endorsed by dbt Labs, Inc.