In SQL, "not equal" is written <>, and most databases also accept != as the same operator. Both return NULL, not true, when either side is NULL, so WHERE status <> 'active' silently drops every row whose status is NULL.
The syntax takes a few seconds to learn. The NULL behaviour is where queries go wrong, so most of this article is about that.
SQL not equal: <> or !=?
Use <> if the query has to run on more than one database. It is the standard SQL spelling, and PostgreSQL's documentation says so directly: <> is the standard notation for "not equal", and != is an alias that is converted to <> at a very early stage of parsing. The same page adds that it is not possible to implement != and <> as operators that do different things.
| Database | <> | != | Source of the claim |
|---|---|---|---|
| PostgreSQL | Yes (standard) | Yes (alias) | Comparison operators table and its note |
| MySQL | Yes | Yes | Both listed together as "Not equal" |
| SQLite | Yes | Yes | "The not-equal operator can be either != or <>" |
| SQL Server (T-SQL) | Yes | Yes | Microsoft documents != as working the same as the traditional <> |
A basic example, using a small table you can create in any of them:
CREATE TABLE accounts (id INTEGER, name TEXT, status TEXT);
INSERT INTO accounts VALUES
(1, 'Ada', 'active'),
(2, 'Ben', 'closed'),
(3, 'Chloe', NULL),
(4, 'Dev', 'active');
SELECT id, name FROM accounts WHERE status <> 'active';
Example output:
id | name
---+-----
2 | Ben
Ben is there. Chloe is not, and that is the trap.
Why NULL rows disappear from a not-equal filter
NULL means "unknown", not "empty". PostgreSQL's documentation puts it this way: ordinary comparison operators yield null, signifying unknown, rather than true or false, when either input is null. Its examples are 7 = NULL and 7 <> NULL, and both yield null. SQLite says the same for all its operators in general, in accordance with the SQL92 standard, and Microsoft's page for != states that if either or both operands are NULL, NULL is returned.
A WHERE clause keeps only the rows for which the condition is true. For Chloe, status <> 'active' evaluates to NULL, which is not true, so the row is filtered out. You asked for accounts that are not active, and an account with an unknown status was not returned. Whether that is correct depends on what NULL means in your data. Often the person reading the report expects Chloe to be included.
The same rule explains the common beginner mistake of comparing to NULL directly:
-- Returns no rows, always:
SELECT * FROM accounts WHERE status = NULL;
SELECT * FROM accounts WHERE status <> NULL;
SELECT * FROM accounts WHERE status != NULL;
PostgreSQL's manual is blunt about it: do not write expression = NULL, because NULL is not "equal to" NULL, as it is not known whether two unknown values are equal. The correct tests are IS NULL and IS NOT NULL.
How to include NULL rows: four ways
1. Add an explicit IS NULL branch
This works in every database and makes the intent obvious to the next reader:
SELECT id, name
FROM accounts
WHERE status <> 'active' OR status IS NULL;
Example output:
id | name
---+------
2 | Ben
3 | Chloe
2. IS DISTINCT FROM (standard SQL)
PostgreSQL documents IS DISTINCT FROM as "not equal, treating null as a comparable value". Its examples: 1 IS DISTINCT FROM NULL is true, and NULL IS DISTINCT FROM NULL is false. For non-null inputs it is the same as <>. It never returns NULL.
SELECT id, name
FROM accounts
WHERE status IS DISTINCT FROM 'active';
This returns the same rows as the previous query, Ben and Chloe. The opposite predicate is IS NOT DISTINCT FROM, a NULL-safe equals. SQLite's documentation lists it too. For MySQL, see <=> below. Check your database's documentation before relying on it elsewhere, since only these sources were confirmed.
3. IS NOT in SQLite
SQLite's documentation says IS and IS NOT work like = and != except when one or both operands are NULL, and that an IS or IS NOT expression can never evaluate to NULL. It also notes that this compact form is an SQLite extension, and that standard SQL spells it IS [NOT] DISTINCT FROM.
-- SQLite only
SELECT id, name FROM accounts WHERE status IS NOT 'active';
4. The NULL-safe operator in MySQL
MySQL has <=>, which behaves like = but returns 1 instead of NULL when both operands are NULL, and 0 instead of NULL when one is. The manual states it is equivalent to the standard IS NOT DISTINCT FROM. To get a NULL-safe "not equal", negate it:
-- MySQL
SELECT id, name FROM accounts WHERE NOT (status <=> 'active');
The manual's own example shows the contrast: SELECT 1 = 1, NULL = NULL, 1 = NULL; returns 1, NULL, NULL, while SELECT 1 <=> 1, NULL <=> NULL, 1 <=> NULL; returns 1, 1, 0.
Testing for not null
To exclude NULLs rather than keep them, use IS NOT NULL:
SELECT id, name FROM accounts WHERE status IS NOT NULL;
That returns Ada, Ben and Dev. If you want rows with a real value that is also not 'active', combine the two conditions: status IS NOT NULL AND status <> 'active'. In that case the NULL check is redundant for the result, since <> already drops NULLs, but it states your intent.
NOT IN and NOT EQUAL with a list
To exclude several values, NOT IN is shorter than chaining <> with AND:
SELECT id, name FROM accounts
WHERE status NOT IN ('active', 'closed');
It inherits the same NULL behaviour, because x NOT IN (a, b) means x <> a AND x <> b. A NULL in the column makes the whole condition NULL, so Chloe is again excluded. A NULL inside the list is worse: x NOT IN (1, NULL) expands to x <> 1 AND x <> NULL. The second part is never true, so the whole expression is never true, and the query returns no rows at all. This follows directly from the rules above; you can confirm it by running the query on the sample table. It often shows up when the list comes from a subquery over a nullable column, for example WHERE id NOT IN (SELECT manager_id FROM staff). Filter the subquery with WHERE manager_id IS NOT NULL, or use NOT EXISTS.
The join-based version of "rows with no match" is covered in SQL joins explained, under anti-joins.
Not equal with multiple columns
MySQL documents row comparisons: (a, b) <> (x, y) is equivalent to (a <> x) OR (b <> y). So the row is "not equal" if any column differs. The NULL rules apply per column: a NULL in one column makes that part of the OR evaluate to NULL, which hides a difference there. If you are comparing rows to find changes between two tables, IS DISTINCT FROM on each column is the safer choice, because a column changing from a value to NULL is a change you want to see.
Strings and case
Whether 'Ada' <> 'ada' is true depends on the collation of the column or database, so it varies by database and configuration. MySQL's manual example shows how comparison semantics can surprise you: SELECT .01 <> '0.01'; returns 0 because the string is converted to a number, while SELECT '.01' <> '0.01'; returns 1 because both are compared as strings. Compare values of the same type where you can.
If your query still returns the wrong rows
- Check whether the column allows NULL. If it does, decide what NULL should mean for this filter and write it out with
IS NULLorIS DISTINCT FROM. - Look for
= NULL,<> NULLor!= NULLin the query. Replace them withIS NULLorIS NOT NULL. - If a
NOT INreturns nothing, look for a NULL in the list or subquery. - If you are new to filtering, SQL for beginners covers
WHEREand the other core statements with a runnable example table.
Frequently asked questions
- Is != valid in SQL?
- It works in PostgreSQL, MySQL, SQLite and SQL Server, but the standard operator is <>. PostgreSQL's documentation calls <> the standard SQL notation and != an alias that the parser converts to <> early on. If you want one spelling that every database accepts, use <>.
- Why does WHERE column <> 'value' not return rows where the column is NULL?
- Comparing NULL with anything using <> yields NULL, not true. WHERE keeps only rows where the condition is true, so rows with NULL are dropped along with the rows that equal the value. Add OR column IS NULL, or use IS DISTINCT FROM (or IS NOT in SQLite, or the negation of <=> in MySQL) to keep them.
- How do I check for not null in SQL?
- Use column IS NOT NULL. Writing column <> NULL or column != NULL never matches any row, because the comparison always evaluates to NULL.
- What is the difference between <> and IS DISTINCT FROM?
- For non-null inputs they behave the same. When one input is NULL, <> returns NULL while IS DISTINCT FROM returns true. When both are NULL, <> returns NULL while IS DISTINCT FROM returns false. IS DISTINCT FROM always returns true or false.