MySQL ISNULL() Function
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 forNULL.
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)returns1, andISNULL('hello')returns0. It never evaluates toNULL. - Index performance in
WHEREclauses: WritingWHERE ISNULL(shipped_at) = 1prevents MySQL from using a B-tree index onshipped_atin older configurations. Always writeWHERE shipped_at IS NULLinWHEREfilters so the optimizer can use indexes and follow ANSI SQL. - SQL Server two-argument error: Calling
ISNULL(shipped_at, 'Pending')in MySQL throws anIncorrect parameter counterror. UseIFNULL()orCOALESCE()instead. - Zero and empty string are not NULL:
ISNULL(0)returns0andISNULL('')returns0.
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.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.