NULLIF Function

SQL · Conditional Expressions

Theory

NULLIF(a, b) compares two values: if they are equal it returns NULL, otherwise it returns a. NULLIF(qty, 0) gives NULL when qty is 0 and qty otherwise. It is the same as CASE WHEN a = b THEN NULL ELSE a END.

Its best-known job is preventing divide-by-zero errors. total / qty fails (or returns NULL or an error, depending on the engine) when qty is 0, but total / NULLIF(qty, 0) returns NULL for those rows and the query keeps running. It also cleans placeholder values: NULLIF(phone, '') turns empty strings into real NULLs so COUNT and COALESCE treat them as missing. A common pattern combines both functions: COALESCE(total / NULLIF(qty, 0), 0) shows 0 instead of NULL. Watch out for integer division in some engines, where 7 / 2 is 3.

Why we use it

Zero divisors and fake "empty" values (0, '', 'N/A') break calculations and counts. NULLIF turns them into NULL, which SQL already knows how to skip and handle safely.

When to use it

Use NULLIF to guard divisions and to convert placeholder values into NULL. Use COALESCE for the opposite direction, turning NULL into a value, and CASE for anything more complex than one equality test.

How it works

  1. NULLIF(a, b) for a row
  2. Compare a with b
  3. Are they equal? Yes: Return NULL. No: Return a.
  4. Use the result in the outer expression, e.g. total / result
  5. Division by NULL gives NULL instead of an error

Syntax

NULLIF(value1, value2)

Example

SELECT NULLIF(discount, 0) FROM orders;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic