Lesson

MySQL ISNULL() Function

10 min reading
Free Course

MySQL ISNULL() Function

The MySQL ISNULL() function tests whether an expression is NULL and returns 1 if it is NULL, or 0 if it is not NULL. Unlike almost every comparison operator in SQL, ISNULL() always returns a boolean integer (1 or 0) and never returns NULL.

Syntax

ISNULL(expression)
  • expression: Any column, literal, or calculation to check for NULL.

Be careful if you are coming from SQL Server (T-SQL): MySQL ISNULL() takes one argument and returns 1 or 0. If you want to replace NULL with a fallback value in MySQL, use IFNULL(a, b) or COALESCE(a, b).

Practical Example

In demo_orders, you can use ISNULL(shipped_at) inside SUM() to count unshipped orders, or inspect which rows lack a shipping date.

SELECT
    order_id,
    customer_id,
    shipped_at,
    ISNULL(shipped_at) AS is_unshipped
FROM demo_orders
ORDER BY order_id;
order_id customer_id shipped_at is_unshipped
1001 12 2026-10-04 09:15:00 0
1002 15 NULL 1
1003 19 2026-10-06 14:30:00 0

Common Mistakes & NULL Behavior

  • Never returns NULL: ISNULL(NULL) returns 1, and ISNULL('hello') returns 0. It never evaluates to NULL.
  • Index performance in WHERE clauses: Writing WHERE ISNULL(shipped_at) = 1 prevents MySQL from using a B-tree index on shipped_at in older configurations. Always write WHERE shipped_at IS NULL in WHERE filters so the optimizer can use indexes and follow ANSI SQL.
  • SQL Server two-argument error: Calling ISNULL(shipped_at, 'Pending') in MySQL throws an Incorrect parameter count error. Use IFNULL() or COALESCE() instead.
  • Zero and empty string are not NULL: ISNULL(0) returns 0 and ISNULL('') returns 0.

Frequently Asked Questions

What does MySQL ISNULL(NULL) return?

ISNULL(NULL) returns 1. If the argument is any non-NULL value (including 0 or ''), it returns 0.

Should I use ISNULL(col) or col IS NULL in a WHERE clause?

Always prefer col IS NULL in WHERE clauses. It is standard ANSI SQL, easier to read, and reliably uses indexes on col.

Why does ISNULL(a, b) fail in MySQL?

Because MySQL's ISNULL() accepts only one argument to test for NULL. Use IFNULL(a, b) or COALESCE(a, b) to provide a fallback value.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum