Software

SQL Not Equal (<> and !=) With NULL Examples

SQL writes not equal as <> (standard) or !=. Both skip NULL rows, which surprises people. Query results show the problem and three ways to include NULLs.

The standard SQL not-equal operator is <>; almost every database also accepts !=, and they behave identically. The catch: neither matches NULL. WHERE status <> 'closed' skips rows where status is NULL, because comparing anything with NULL gives “unknown”, not true. Add OR status IS NULL, or use a null-safe operator.

The table

CREATE TABLE tickets (id INTEGER, status TEXT);
INSERT INTO tickets VALUES (1,'open'), (2,'closed'), (3,NULL), (4,'pending');

You want every ticket that is not closed. That should be tickets 1, 3 and 4.

CompareWhat each condition returned in SQLite 3.51.0

<> 'closed'

Ticket 3 is missing: NULL <> 'closed' is NULL, not true.

idstatus
1open
4pending

!= 'closed'

Identical to <>.

idstatus
1open
4pending

<> 'closed' OR IS NULL

Portable fix that works in every database.

idstatus
1open
3NULL
4pending

IS NOT 'closed'

SQLite's null-safe comparison. Standard SQL spells it IS DISTINCT FROM.

idstatus
1open
3NULL
4pending

NOT IN ('closed', NULL)

No rows at all: a NULL in a NOT IN list makes every comparison unknown.

idstatus

Why NULL is never “not equal”

SQL uses three-valued logic: a comparison is true, false or unknown. NULL means “no value”, so any comparison with it is unknown, and WHERE keeps only rows where the condition is true. Our check confirmed it:

┌─────────┬─────────┬─────────┐
│ ne_null │ eq_null │ is_null │
├─────────┼─────────┼─────────┤
│ NULL    │ NULL    │ 1       │
└─────────┴─────────┴─────────┘

NULL <> 'closed' and even NULL = NULL return NULL. Only IS NULL gives a real answer.

Null-safe options

NeedPortable SQLNull-safe operator
not equal, include NULLscol <> x OR col IS NULLcol IS DISTINCT FROM x (PostgreSQL, SQL Server 2022+, standard); col IS NOT x (SQLite); NOT (col <=> x) (MySQL)
equal, treating NULL = NULLcol = x OR (col IS NULL AND x IS NULL)col IS NOT DISTINCT FROM x; col IS x (SQLite); col <=> x (MySQL)
replace NULL firstCOALESCE(col, '') <> x(prevents index use on col)

The NOT IN trap

WHERE status NOT IN ('closed', NULL) returned no rows in our run. NOT IN expands to status <> 'closed' AND status <> NULL, and the second part is always unknown. The same happens with a subquery, NOT IN (SELECT …), whenever the subquery returns a NULL. Use NOT EXISTS, or filter NULLs out of the subquery.

!= or <>?

Both work in SQLite, PostgreSQL, MySQL, SQL Server and Oracle. <> is the ISO standard spelling, so it is the safer default in shared code and style guides; != is a widely supported alias. Pick one and be consistent.

The ticket table follows the same SQLite setup as the primary vs. foreign key and UNION vs. UNION ALL examples, so you can reproduce all three in one session.

Sources

  1. SQL language expressions (operators, IS, IS NOT) — SQLite
  2. NULL handling in SQLite versus other database engines — SQLite

How we made this

Reproduced in a local lab. All queries ran in the sqlite3 shell (SQLite 3.51.0) against the four-row table shown; outputs are quoted verbatim. Operator support in other databases is cited from their syntax, noted where it differs.

Performed by Claude, an AI assistant working in the BinConvert repository (SQLite 3.51.0), November 16, 2026.

This article was drafted with AI assistance. Every example, calculation and command output on this page was re-run or checked before release; every query was re-run and the returned rows compared. Review: automated test suite and source check, November 16, 2026.

Spotted an error? Tell us and we will correct it.

Copied to clipboard!