NULLIF returns NULL if two expressions are equal, otherwise returns the first expression. IFF (Snowflake) and IF (BigQuery) provide simple if-then-else logic. Useful for handling division by zero, preventing duplicates, and simple conditional logic.
IFF(condition, true_value, false_value)-- Snowflake's shorthand for simple CASE
IFF(condition, true_value, false_value)-- Snowflake's shorthand for simple CASE
IFF(condition, true_value, false_value)-- Snowflake's shorthand for simple CASE
IF (BigQuery):
IF(condition, true_value, false_value)-- BigQuery's conditional function
IF(condition, true_value, false_value)-- BigQuery's conditional function
IF(condition, true_value, false_value)-- BigQuery's conditional function
Platform-Specific Notes
Snowflake:
NULLIF standard
IFF is shorthand for simple CASE WHEN
More concise than full CASE statement
BigQuery:
NULLIF standard
IF function (not IFF)
Similar to IFF functionality
Databricks:
NULLIF standard
Use CASE WHEN for conditional logic
No IFF or IF function (use CASE)
Example 1: NULLIF - Prevent Division by Zero
All Platforms:
SELECT
product_name,
total_revenue,
units_sold,-- Without NULLIF (would error on 0)-- total_revenue / units_sold as avg_price, -- ERROR if units_sold = 0-- With NULLIF (safe division)
total_revenue / NULLIF(units_sold,0)as avg_price,-- Alternative with CASECASEWHEN units_sold = 0THENNULLELSE total_revenue / units_sold
ENDas avg_price_alternative
FROM
SELECT
product_name,
total_revenue,
units_sold,-- Without NULLIF (would error on 0)-- total_revenue / units_sold as avg_price, -- ERROR if units_sold = 0-- With NULLIF (safe division)
total_revenue / NULLIF(units_sold,0)as avg_price,-- Alternative with CASECASEWHEN units_sold = 0THENNULLELSE total_revenue / units_sold
ENDas avg_price_alternative
FROM
SELECT
product_name,
total_revenue,
units_sold,-- Without NULLIF (would error on 0)-- total_revenue / units_sold as avg_price, -- ERROR if units_sold = 0-- With NULLIF (safe division)
total_revenue / NULLIF(units_sold,0)as avg_price,-- Alternative with CASECASEWHEN units_sold = 0THENNULLELSE total_revenue / units_sold
ENDas avg_price_alternative
FROM
SELECT
product_id,
product_name,
price,
discount_price,-- Only show discount if it's actually different from regular price
NULLIF(discount_price, price)as actual_discount_price,-- Calculate savings (NULL if no discount)
price - NULLIF(discount_price, price)as savings,-- Percentage discount (NULL if no discount)
ROUND((price - NULLIF(discount_price, price)) * 100.0 / price,2)as discount_percentage
FROM
SELECT
product_id,
product_name,
price,
discount_price,-- Only show discount if it's actually different from regular price
NULLIF(discount_price, price)as actual_discount_price,-- Calculate savings (NULL if no discount)
price - NULLIF(discount_price, price)as savings,-- Percentage discount (NULL if no discount)
ROUND((price - NULLIF(discount_price, price)) * 100.0 / price,2)as discount_percentage
FROM
SELECT
product_id,
product_name,
price,
discount_price,-- Only show discount if it's actually different from regular price
NULLIF(discount_price, price)as actual_discount_price,-- Calculate savings (NULL if no discount)
price - NULLIF(discount_price, price)as savings,-- Percentage discount (NULL if no discount)
ROUND((price - NULLIF(discount_price, price)) * 100.0 / price,2)as discount_percentage
FROM
Sample Data (products table):
product_id
product_name
price
discount_price
1
Laptop
999.99
899.99
2
Mouse
29.99
29.99
3
Keyboard
89.99
79.99
4
Monitor
349.99
349.99
Result:
product_id
product_name
price
discount_price
actual_discount_price
savings
discount_percentage
1
Laptop
999.99
899.99
899.99
100.00
10.00
2
Mouse
29.99
29.99
NULL
NULL
NULL
3
Keyboard
89.99
79.99
79.99
10.00
11.11
4
Monitor
349.99
349.99
NULL
NULL
NULL
Example 6: Combining NULLIF with COALESCE
All Platforms:
SELECT
employee_id,
base_salary,
bonus,
commission,-- Total compensation with safe division
base_salary + COALESCE(bonus,0) + COALESCE(commission,0)as total_comp,-- Bonus as percentage of base (NULL if no bonus)
ROUND(
NULLIF(bonus,0) * 100.0 / NULLIF(base_salary,0),2)as bonus_percentage,-- Use bonus if exists, otherwise commission, otherwise 0
COALESCE(
NULLIF(bonus,0),
NULLIF(commission,0),0)as variable_pay
FROM
SELECT
employee_id,
base_salary,
bonus,
commission,-- Total compensation with safe division
base_salary + COALESCE(bonus,0) + COALESCE(commission,0)as total_comp,-- Bonus as percentage of base (NULL if no bonus)
ROUND(
NULLIF(bonus,0) * 100.0 / NULLIF(base_salary,0),2)as bonus_percentage,-- Use bonus if exists, otherwise commission, otherwise 0
COALESCE(
NULLIF(bonus,0),
NULLIF(commission,0),0)as variable_pay
FROM
SELECT
employee_id,
base_salary,
bonus,
commission,-- Total compensation with safe division
base_salary + COALESCE(bonus,0) + COALESCE(commission,0)as total_comp,-- Bonus as percentage of base (NULL if no bonus)
ROUND(
NULLIF(bonus,0) * 100.0 / NULLIF(base_salary,0),2)as bonus_percentage,-- Use bonus if exists, otherwise commission, otherwise 0
COALESCE(
NULLIF(bonus,0),
NULLIF(commission,0),0)as variable_pay
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.