SQL Keywords

SQL Keywords

NULLIF / IFF (IF)

Feb 23, 2026

·

5

min read

Category: Conditional Expressions

Platform Support:

✅ Snowflake (NULLIF, IFF) | ✅ BigQuery (NULLIF, IF) | ✅ Databricks (NULLIF)

Description

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.

Syntax

NULLIF:

NULLIF(expression1, expression2)
-- Returns NULL if expression1 = expression2
-- Otherwise returns expression1
NULLIF(expression1, expression2)
-- Returns NULL if expression1 = expression2
-- Otherwise returns expression1
NULLIF(expression1, expression2)
-- Returns NULL if expression1 = expression2
-- Otherwise returns expression1

IFF (Snowflake):

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 CASE
    CASE 
        WHEN units_sold = 0 THEN NULL
        ELSE total_revenue / units_sold
    END as 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 CASE
    CASE 
        WHEN units_sold = 0 THEN NULL
        ELSE total_revenue / units_sold
    END as 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 CASE
    CASE 
        WHEN units_sold = 0 THEN NULL
        ELSE total_revenue / units_sold
    END as avg_price_alternative
FROM

Sample Data (product_sales table):

product_id

product_name

total_revenue

units_sold

1

Laptop

10000.00

10

2

Mouse

500.00

25

3

Monitor

0.00

0

4

Keyboard

2000.00

0

Result:

product_name

total_revenue

units_sold

avg_price

avg_price_alternative

Laptop

10000.00

10

1000.00

1000.00

Mouse

500.00

25

20.00

20.00

Monitor

0.00

0

NULL

NULL

Keyboard

2000.00

0

NULL

NULL

Example 2: NULLIF - Convert Empty Strings to NULL

All Platforms:

SELECT 
    customer_id,
    customer_name,
    email,
    phone,
    -- Convert empty strings to NULL
    NULLIF(TRIM(email), '') as email_clean,
    NULLIF(TRIM(phone), '') as phone_clean,
    NULLIF(TRIM(secondary_email), '') as secondary_email_clean,
    
    -- Count non-empty contact methods
    (CASE WHEN NULLIF(TRIM(email), '') IS NOT NULL THEN 1 ELSE 0 END +
     CASE WHEN NULLIF(TRIM(phone), '') IS NOT NULL THEN 1 ELSE 0 END +
     CASE WHEN NULLIF(TRIM(secondary_email), '') IS NOT NULL THEN 1 ELSE 0 END) as contact_methods_count
FROM

SELECT 
    customer_id,
    customer_name,
    email,
    phone,
    -- Convert empty strings to NULL
    NULLIF(TRIM(email), '') as email_clean,
    NULLIF(TRIM(phone), '') as phone_clean,
    NULLIF(TRIM(secondary_email), '') as secondary_email_clean,
    
    -- Count non-empty contact methods
    (CASE WHEN NULLIF(TRIM(email), '') IS NOT NULL THEN 1 ELSE 0 END +
     CASE WHEN NULLIF(TRIM(phone), '') IS NOT NULL THEN 1 ELSE 0 END +
     CASE WHEN NULLIF(TRIM(secondary_email), '') IS NOT NULL THEN 1 ELSE 0 END) as contact_methods_count
FROM

SELECT 
    customer_id,
    customer_name,
    email,
    phone,
    -- Convert empty strings to NULL
    NULLIF(TRIM(email), '') as email_clean,
    NULLIF(TRIM(phone), '') as phone_clean,
    NULLIF(TRIM(secondary_email), '') as secondary_email_clean,
    
    -- Count non-empty contact methods
    (CASE WHEN NULLIF(TRIM(email), '') IS NOT NULL THEN 1 ELSE 0 END +
     CASE WHEN NULLIF(TRIM(phone), '') IS NOT NULL THEN 1 ELSE 0 END +
     CASE WHEN NULLIF(TRIM(secondary_email), '') IS NOT NULL THEN 1 ELSE 0 END) as contact_methods_count
FROM

Sample Data (customers table):

customer_id

customer_name

email

phone

secondary_email

1

John Smith

john@email.com

555-1234

""

2

Maria Garcia

""

""

maria2@email.com

3

Sarah Johnson

sarah@email.com

""

""

Result:

customer_id

customer_name

email_clean

phone_clean

secondary_email_clean

contact_methods_count

1

John Smith

john@email.com

555-1234

NULL

2

2

Maria Garcia

NULL

NULL

maria2@email.com

1

3

Sarah Johnson

sarah@email.com

NULL

NULL

1

Example 3: IFF - Simple Conditional Logic (Snowflake)

Snowflake:

SELECT 
    customer_id,
    total_purchases,
    -- Simple if-then-else
    IFF(total_purchases > 1000, 'VIP', 'Standard') as customer_tier,
    IFF(total_purchases > 500, 'Yes', 'No') as qualifies_for_discount,
    IFF(total_purchases = 0, 'Never Purchased', 'Active') as status,
    
    -- Nested IFF
    IFF(total_purchases > 5000, 'Platinum',
        IFF(total_purchases > 1000, 'Gold',
            IFF(total_purchases > 500, 'Silver', 'Bronze')
        )
    ) as loyalty_level
FROM

SELECT 
    customer_id,
    total_purchases,
    -- Simple if-then-else
    IFF(total_purchases > 1000, 'VIP', 'Standard') as customer_tier,
    IFF(total_purchases > 500, 'Yes', 'No') as qualifies_for_discount,
    IFF(total_purchases = 0, 'Never Purchased', 'Active') as status,
    
    -- Nested IFF
    IFF(total_purchases > 5000, 'Platinum',
        IFF(total_purchases > 1000, 'Gold',
            IFF(total_purchases > 500, 'Silver', 'Bronze')
        )
    ) as loyalty_level
FROM

SELECT 
    customer_id,
    total_purchases,
    -- Simple if-then-else
    IFF(total_purchases > 1000, 'VIP', 'Standard') as customer_tier,
    IFF(total_purchases > 500, 'Yes', 'No') as qualifies_for_discount,
    IFF(total_purchases = 0, 'Never Purchased', 'Active') as status,
    
    -- Nested IFF
    IFF(total_purchases > 5000, 'Platinum',
        IFF(total_purchases > 1000, 'Gold',
            IFF(total_purchases > 500, 'Silver', 'Bronze')
        )
    ) as loyalty_level
FROM

Sample Data (customer_summary table):

customer_id

total_purchases

1

250.00

2

750.00

3

1500.00

4

6000.00

5

0.00

Result:

customer_id

total_purchases

customer_tier

qualifies_for_discount

status

loyalty_level

1

250.00

Standard

No

Active

Bronze

2

750.00

Standard

Yes

Active

Silver

3

1500.00

VIP

Yes

Active

Gold

4

6000.00

VIP

Yes

Active

Platinum

5

0.00

Standard

No

Never Purchased

Bronze

Example 4: IF - Simple Conditional Logic (BigQuery)

BigQuery:

SELECT 
    order_id,
    order_total,
    shipping_cost,
    -- Simple conditional
    IF(order_total > 100, 0, shipping_cost) as final_shipping_cost,
    IF(order_total > 100, 'Free Shipping', 'Standard') as shipping_type,
    
    -- Calculate final total
    order_total + IF(order_total > 100, 0, shipping_cost) as final_total,
    
    -- Nested IF
    IF(order_total > 500, 'Express Free',
        IF(order_total > 100, 'Standard Free', 'Standard Paid')
    ) as shipping_tier
FROM

SELECT 
    order_id,
    order_total,
    shipping_cost,
    -- Simple conditional
    IF(order_total > 100, 0, shipping_cost) as final_shipping_cost,
    IF(order_total > 100, 'Free Shipping', 'Standard') as shipping_type,
    
    -- Calculate final total
    order_total + IF(order_total > 100, 0, shipping_cost) as final_total,
    
    -- Nested IF
    IF(order_total > 500, 'Express Free',
        IF(order_total > 100, 'Standard Free', 'Standard Paid')
    ) as shipping_tier
FROM

SELECT 
    order_id,
    order_total,
    shipping_cost,
    -- Simple conditional
    IF(order_total > 100, 0, shipping_cost) as final_shipping_cost,
    IF(order_total > 100, 'Free Shipping', 'Standard') as shipping_type,
    
    -- Calculate final total
    order_total + IF(order_total > 100, 0, shipping_cost) as final_total,
    
    -- Nested IF
    IF(order_total > 500, 'Express Free',
        IF(order_total > 100, 'Standard Free', 'Standard Paid')
    ) as shipping_tier
FROM

Sample Data (orders table):

order_id

order_total

shipping_cost

101

50.00

10.00

102

150.00

10.00

103

600.00

10.00

Result:

order_id

order_total

shipping_cost

final_shipping_cost

shipping_type

final_total

shipping_tier

101

50.00

10.00

10.00

Standard

60.00

Standard Paid

102

150.00

10.00

0.00

Free Shipping

150.00

Standard Free

103

600.00

10.00

0.00

Free Shipping

600.00

Express Free

Example 5: NULLIF for Data Quality

All Platforms:

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

Sample Data (employees table):

employee_id

base_salary

bonus

commission

1

75000

5000

NULL

2

65000

0

3000

3

80000

NULL

NULL

4

70000

7000

2000

Result:

employee_id

base_salary

bonus

commission

total_comp

bonus_percentage

variable_pay

1

75000

5000

NULL

80000

6.67

5000

2

65000

0

3000

68000

NULL

3000

3

80000

NULL

NULL

80000

NULL

0

4

70000

7000

2000

79000

10.00

7000

NULLIF vs CASE Comparison

NULLIF:

NULLIF(column, value)
NULLIF(column, value)
NULLIF(column, value)

Equivalent CASE:

CASE WHEN column = value THEN NULL ELSE column END
CASE WHEN column = value THEN NULL ELSE column END
CASE WHEN column = value THEN NULL ELSE column END

IFF/IF:

IFF(condition, true_value, false_value)  -- Snowflake
IF(condition, true_value, false_value)   -- BigQuery
IFF(condition, true_value, false_value)  -- Snowflake
IF(condition, true_value, false_value)   -- BigQuery
IFF(condition, true_value, false_value)  -- Snowflake
IF(condition, true_value, false_value)   -- BigQuery

Equivalent CASE:

CASE WHEN condition THEN true_value ELSE false_value END
CASE WHEN condition THEN true_value ELSE false_value END
CASE WHEN condition THEN true_value ELSE false_value END

Common Use Cases

NULLIF:

  1. Division by zero: Safe calculations

  2. Remove sentinel values: Convert -1, 0, '' to NULL

  3. Duplicate detection: Find when values shouldn't match

  4. Data cleaning: Empty string to NULL conversion

IFF/IF:

  1. Simple flags: Binary conditions

  2. Tier assignment: Customer/product categorization

  3. Fee calculation: Conditional pricing

  4. Status determination: Active/Inactive logic

Performance Considerations

  • NULLIF is slightly faster than equivalent CASE

  • IFF/IF faster than multi-condition CASE

  • Use CASE for complex logic (>2 conditions)

  • All prevent query errors in calculations

Best Practices

  1. Use NULLIF for:

  2. Use IFF/IF for:

  3. Use CASE for:

  4. General:

Interested to Learn More?
Try Out the Free 14-Days Trial

More Articles

decorative icon

Experience Analytics for the AI-Era

Start your 14-day trial today - it's free and no credit card needed

decorative icon

Experience Analytics for the AI-Era

Start your 14-day trial today - it's free and no credit card needed

decorative icon

Experience Analytics for the AI-Era

Start your 14-day trial today - it's free and no credit card needed

Copyright © 2026 Paradime Labs, Inc.

Made with ❤️ in San Francisco ・ London

*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.

Copyright © 2026 Paradime Labs, Inc.

Made with ❤️ in San Francisco ・ London

*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.

Copyright © 2026 Paradime Labs, Inc.

Made with ❤️ in San Francisco ・ London

*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.