// conditional reference

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.

类别
条件
Returns
boolean
In this category
41 of 43

语法

a <> b
a != b

Each block is a separate form. The support table and the examples show which engine takes which.

引擎支持

引擎Support备注
SQL Server 是 both; IS DISTINCT FROM from 2022
PostgreSQL 是 both; IS DISTINCT FROM for NULL-safe tests
MySQL 是 both; NOT (a <=> b) for NULL-safe tests
SQLite 是 both; IS NOT for NULL-safe tests
Oracle 是 both, plus ^=

示例

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

值得了解

  • 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.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

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.