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.
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
| Need | Portable SQL | Null-safe operator |
|---|---|---|
| not equal, include NULLs | col <> x OR col IS NULL | col IS DISTINCT FROM x (PostgreSQL, SQL Server 2022+, standard); col IS NOT x (SQLite); NOT (col <=> x) (MySQL) |
| equal, treating NULL = NULL | col = x OR (col IS NULL AND x IS NULL) | col IS NOT DISTINCT FROM x; col IS x (SQLite); col <=> x (MySQL) |
| replace NULL first | COALESCE(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
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.