SQL not-equal operators
a <> b and a != b mean the same thing on every engine here. The trap is NULL: neither form is true when one side is NULL.
- Category
- Conditional
- Returns
boolean- In this category
- 41 of 43
Syntax
a <> b
a != b
Each block is a separate form. The support table and the examples show which engine takes which.
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Yes | both; IS DISTINCT FROM from 2022 |
| PostgreSQL | Yes | both; IS DISTINCT FROM for NULL-safe tests |
| MySQL | Yes | both; NOT (a <=> b) for NULL-safe tests |
| SQLite | Yes | both; IS NOT for NULL-safe tests |
| Oracle | Yes | both, plus ^= |
Examples
PostgreSQL
SELECT status FROM (VALUES ('open'), ('closed'), (NULL)) AS t(status)
WHERE status <> 'closed'; -- open only; the NULL row is dropped
PostgreSQL
SELECT status FROM (VALUES ('open'), ('closed'), (NULL)) AS t(status)
WHERE status IS DISTINCT FROM 'closed'; -- open, NULL
MySQL
SELECT 1 <> 2 AS a, 1 != 2 AS b, NULL <> 1 AS c; -- 1, 1, NULL
Oracle
SELECT CASE WHEN 1 ^= 2 THEN 'ne' END AS caret_form FROM dual; -- ne
Good to know
- col <> 'closed' is UNKNOWN when col is NULL, so those rows disappear. Add OR col IS NULL, or use IS DISTINCT FROM (PostgreSQL, SQL Server 2022+), IS NOT (SQLite) or NOT (col <=> 'closed') on MySQL.
- There is no performance difference: the parser treats != as <>.
- A not-equal filter rarely uses an index seek, because most rows usually qualify and a scan is cheaper.
Questions about <> and !=
Should I write <> or !=?
<> is the SQL standard and works everywhere. != also works on all five engines; pick one and use it consistently.
Why does <> leave out NULL rows?
Comparing anything with NULL yields UNKNOWN, and WHERE keeps only rows where the condition is TRUE. Use IS DISTINCT FROM, or add OR col IS NULL.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.