By Matthew Rathbone · Updated November 30, 2026

The SQL not equal operator is <>. Almost every database also accepts !=, and the two behave identically:

SELECT * FROM orders WHERE country <> 'US';   -- standard SQL
SELECT * FROM orders WHERE country != 'US';   -- same result, same speed

Both return rows where the value is different from 'US'. Neither returns rows where the value is NULL, which is the single most common surprise. The short version of this guide:

You want rows where...Write
a value is not equal to somethingcol <> 'x' or col != 'x'
a value is not equal, including NULL rowscol <> 'x' OR col IS NULL, or col IS DISTINCT FROM 'x'
a value is not NULLcol IS NOT NULL (never col <> NULL)
a value is none of several valuescol NOT IN ('a', 'b') or col <> 'a' AND col <> 'b'
a value doesn't contain some textcol NOT LIKE '%text%'
a value is outside a rangecol NOT BETWEEN a AND b
a timestamp is not on a given dayts < '2026-11-02' OR ts >= '2026-11-03'

Every example below was run on PostgreSQL 16.15, MySQL 8.0.46, SQL Server 2022 and SQLite 3.45.1, and the output is copied from those runs.

Jump to: sample data · basic example · <> vs != · the NULL trap · strings, case and spaces · multiple values · dates · joins · CASE, HAVING, UPDATE · performance · database table · mistakes · FAQ

Sample data

The examples use a small orders table. A few rows are deliberately awkward, because that's where not equal gets interesting: Ben and Dev have no coupon (NULL), Finn's coupon is an empty string, Eve's status has a capital S, and Finn's status has a trailing space.

CREATE TABLE orders (
  id         INT PRIMARY KEY,
  customer   VARCHAR(20),
  status     VARCHAR(20),
  country    VARCHAR(2),
  coupon     VARCHAR(20),
  total      DECIMAL(8,2),
  ordered_at TIMESTAMP     -- DATETIME in MySQL, DATETIME2 in SQL Server
);

INSERT INTO orders VALUES (1, 'Ana',   'shipped',   'US', 'SAVE10',  120.00, '2026-11-02 09:15:00');
INSERT INTO orders VALUES (2, 'Ben',   'pending',   'UK', NULL,       45.50, '2026-11-02 17:40:00');
INSERT INTO orders VALUES (3, 'Chloe', 'cancelled', 'US', 'SAVE10',   80.00, '2026-11-03 11:05:00');
INSERT INTO orders VALUES (4, 'Dev',   'shipped',   'CA', NULL,      210.00, '2026-11-04 08:30:00');
INSERT INTO orders VALUES (5, 'Eve',   'Shipped',   'US', 'WELCOME',  60.00, '2026-11-04 14:20:00');
INSERT INTO orders VALUES (6, 'Finn',  'pending ',  'US', '',         99.99, '2026-11-05 10:00:00');

Basic example: WHERE column <> value

Put the not equal operator in a WHERE clause to keep every row except the ones that match. Orders from outside the US:

SELECT id, customer, country
FROM orders
WHERE country <> 'US'
ORDER BY id;
 id | customer | country
----+----------+---------
  2 | Ben      | UK
  4 | Dev      | CA

All four databases return these two rows, and swapping <> for != changes nothing. Text values need single quotes ('US'); numbers don't (WHERE id <> 3).

NOT (country = 'US') is a third way to write the same test and returns the same two rows. It's handy when the condition you want to negate is longer, such as NOT (country = 'US' AND total > 100).

You can try this in the free SQL Easy WHERE lesson, which runs a real database in your browser. Swap the = for <> and you get everyone who isn't human:

The SQL Easy WHERE lesson running SELECT * FROM family_members WHERE species <> 'human', which returns one row: Pickles the dog
WHERE species <> 'human' in the WHERE lesson returns Pickles, the only non-human. != works there too.

<> vs != : is there a difference?

No. In PostgreSQL, MySQL, SQL Server, SQLite, Oracle, MariaDB, Snowflake, BigQuery and Databricks, != is simply another spelling of <>. Same results, same NULL handling, same speed. PostgreSQL even rewrites one into the other; ask it for a query plan with != and the plan shows <>:

EXPLAIN SELECT * FROM big WHERE status != 'shipped';

                        QUERY PLAN
----------------------------------------------------------
 Seq Scan on big  (cost=0.00..3588.00 rows=1973 width=12)
   Filter: (status <> 'shipped'::text)

SQL Server does the same: create a filtered index with WHERE status != 'shipped' and sys.indexes stores the filter as ([status]<>'shipped'). Microsoft's documentation puts it plainly: != "functions the same as the <> comparison operator".

So which should you use?

  • <> is the ISO/ANSI SQL standard spelling. If you want SQL that reads the same in every database, or your team's style guide follows the standard, use it.
  • != is familiar from Python, JavaScript, Java and C, and many people find it easier to read. Every mainstream database supports it.

Pick one and be consistent. Oracle also accepts ^=, but you'll rarely see it.

One typo to watch for: =! (equals sign first) is not an operator. PostgreSQL says operator does not exist: integer =! integer, MySQL and SQL Server report a syntax error near =!, and SQLite says unrecognized token: "!".

The NULL trap: not equal skips NULL rows

Here's the query almost everyone writes first. "Show me the orders that didn't use the SAVE10 coupon":

SELECT id, customer, coupon
FROM orders
WHERE coupon <> 'SAVE10'
ORDER BY id;
 id | customer | coupon
----+----------+---------
  5 | Eve      | WELCOME
  6 | Finn     |

Ben and Dev are missing, even though they clearly didn't use SAVE10. Their coupon is NULL, and in SQL NULL means "unknown". Is an unknown value different from 'SAVE10'? SQL's answer is "unknown" too, and WHERE only keeps rows where the condition is true. You can see it directly:

SELECT NULL <> 'x', NULL != NULL, 'a' <> 'b';   -- MySQL

NULL <> 'x'   NULL != NULL   'a' <> 'b'
NULL          NULL           1

Every database does this (it's the standard's three-valued logic), so it isn't a bug you can upgrade away. Finn shows up because an empty string '' is a real value, not NULL, except in Oracle, which stores '' as NULL.

How to include NULL rows

Say so explicitly with OR ... IS NULL. This works in every database:

SELECT id, customer, coupon
FROM orders
WHERE coupon <> 'SAVE10' OR coupon IS NULL
ORDER BY id;
 id | customer | coupon
----+----------+---------
  2 | Ben      | NULL
  4 | Dev      | NULL
  5 | Eve      | WELCOME
  6 | Finn     |

If you combine it with other conditions, wrap it in parentheses: WHERE country = 'US' AND (coupon <> 'SAVE10' OR coupon IS NULL). Without them, AND binds first and the OR lets in every NULL coupon from any country. See AND and OR for the precedence rules.

IS DISTINCT FROM: a NULL-safe not equal

IS DISTINCT FROM is the standard SQL operator for "not equal, treating NULL as a normal value". Two NULLs are not distinct; NULL and 'SAVE10' are.

SELECT id, customer, coupon
FROM orders
WHERE coupon IS DISTINCT FROM 'SAVE10'
ORDER BY id;

This returns the same four rows as the OR IS NULL version in PostgreSQL, SQL Server 2022 and SQLite 3.39+. MySQL doesn't support it (ERROR 1064 ... near 'DISTINCT FROM 'SAVE10' ORDER BY id'); use its NULL-safe equals operator <=> with NOT instead:

-- MySQL and MariaDB
SELECT id, customer, coupon
FROM orders
WHERE NOT (coupon <=> 'SAVE10')
ORDER BY id;

That also returns Ben, Dev, Eve and Finn. <=> is MySQL-only: PostgreSQL rejects it with operator does not exist: character varying <=> unknown. SQLite also accepts coupon IS NOT 'SAVE10'.

The COALESCE shortcut (and its catch)

Another common fix is to replace NULL with a placeholder before comparing, using COALESCE:

WHERE COALESCE(coupon, '') <> 'SAVE10'

It returns the same four rows everywhere and reads clearly. The catch is speed: wrapping a column in a function usually stops the database using an index on it. On a big table prefer OR coupon IS NULL or IS DISTINCT FROM.

Don't write <> NULL

To find rows that have a value, WHERE coupon <> NULL looks right but returns zero rows in every database, because comparing anything to NULL is unknown. Use IS NOT NULL:

SELECT COUNT(*) AS n FROM orders WHERE coupon IS NOT NULL;   -- 4

Old SQL Server code sometimes gets away with <> NULL because of SET ANSI_NULLS OFF, which makes it behave like IS NOT NULL (we confirmed it returns Ana, Chloe, Eve and Finn). Microsoft has deprecated that setting and says it will become an error in a future version, so don't rely on it.

Not equal with strings: case and trailing spaces

Comparing text looks simple, but databases disagree on whether 'Shipped' equals 'shipped', and whether 'pending ' (with a trailing space) equals 'pending'.

Case sensitivity

SELECT id, customer, status
FROM orders
WHERE status <> 'shipped'
ORDER BY id;

PostgreSQL and SQLite return four rows, including Eve, because 'Shipped' with a capital S is a different string:

 id | customer |  status
----+----------+-----------
  2 | Ben      | pending
  3 | Chloe    | cancelled
  5 | Eve      | Shipped
  6 | Finn     | pending

MySQL and SQL Server return only three rows (Ben, Chloe, Finn). Their default collations (utf8mb4_0900_ai_ci in MySQL 8 and SQL_Latin1_General_CP1_CI_AS in SQL Server) are case-insensitive, so 'Shipped' <> 'shipped' is false and Eve is filtered out.

If you need the same answer everywhere, normalise the case yourself:

WHERE LOWER(status) <> 'shipped'

Or force a case-sensitive comparison where the default is insensitive: status COLLATE utf8mb4_bin <> 'shipped' in MySQL, status COLLATE Latin1_General_CS_AS <> 'shipped' in SQL Server. In PostgreSQL, ILIKE or LOWER() gives you the case-insensitive version. The LIKE guide covers case rules in more detail.

Trailing spaces

Finn's status is 'pending ' with a space at the end. Ask for orders that aren't pending:

SELECT id, customer, status
FROM orders
WHERE status <> 'pending'
ORDER BY id;
DatabaseIs Finn returned?Why
PostgreSQLYesVARCHAR comparisons count trailing spaces
SQLiteYesCompares bytes exactly
MySQL 8YesThe default utf8mb4_0900_ai_ci collation is NO PAD
SQL ServerNoPads the shorter string with spaces before comparing, so 'pending ' = 'pending'

MySQL 5.7 and older collations such as utf8mb4_general_ci are PAD SPACE and behave like SQL Server. Stray spaces usually come from imports and web forms; clean them with UPDATE orders SET status = TRIM(status) (see SQL UPDATE) rather than working around them in every query.

Empty strings

WHERE coupon <> '' returns Ana, Chloe and Eve: it drops Finn's empty string and, as always, the NULLs. To mean "has a real coupon", that is exactly what you want. In Oracle, '' is NULL, so coupon <> '' returns nothing at all; use coupon IS NOT NULL there.

Not equal to multiple values

To exclude several values, use NOT IN:

SELECT id, customer, status
FROM orders
WHERE status NOT IN ('pending', 'cancelled')
ORDER BY id;
 id | customer | status
----+----------+----------
  1 | Ana      | shipped
  4 | Dev      | shipped
  5 | Eve      | Shipped
  6 | Finn     | pending

That's PostgreSQL, SQLite and MySQL (Finn survives because of the trailing space). SQL Server returns Ana, Dev and Eve. NOT IN (a, b) is shorthand for status <> 'pending' AND status <> 'cancelled', and you can write it that way too.

The OR mistake

Writing the long form with OR instead of AND is a classic bug:

SELECT id, customer, status
FROM orders
WHERE status <> 'pending' OR status <> 'cancelled'
ORDER BY id;

This returns all six rows in every database. Every status is either not 'pending' or not 'cancelled' (a pending order isn't cancelled), so the condition is always true. When you negate a list, the ORs become ANDs.

NOT IN with a NULL in the list

If the list contains a NULL, NOT IN returns nothing:

SELECT id, customer FROM orders WHERE coupon NOT IN ('SAVE10', NULL);
-- 0 rows in PostgreSQL, MySQL, SQL Server and SQLite

It expands to coupon <> 'SAVE10' AND coupon <> NULL, and the second half is never true. Nobody types NULL into a list on purpose, but it happens constantly with NOT IN (SELECT ...) when the subquery's column has NULLs. Use NOT EXISTS instead; the subquery guide walks through it.

Not equal to a pattern

<> compares whole values. To exclude rows that contain, start with or end with some text, use NOT LIKE: WHERE customer NOT LIKE 'A%'. See SQL LIKE.

Not equal with dates and timestamps

Comparing a DATE column with <> '2026-11-02' works as you'd expect. A TIMESTAMP or DATETIME column is different. Try to exclude orders placed on 2 November:

SELECT id, customer, ordered_at
FROM orders
WHERE ordered_at <> '2026-11-02'
ORDER BY id;

All four databases return all six rows. '2026-11-02' means midnight, and no order was placed at exactly 00:00:00, so Ana's 09:15 and Ben's 17:40 orders both count as "not equal". To exclude a whole day, use a half-open range, which works everywhere and can use an index:

SELECT id, customer, ordered_at
FROM orders
WHERE ordered_at < '2026-11-02' OR ordered_at >= '2026-11-03'
ORDER BY id;
 id | customer |     ordered_at
----+----------+---------------------
  3 | Chloe    | 2026-11-03 11:05:00
  4 | Dev      | 2026-11-04 08:30:00
  5 | Eve      | 2026-11-04 14:20:00
  6 | Finn     | 2026-11-05 10:00:00

CAST(ordered_at AS DATE) <> '2026-11-02' gives the same four rows in PostgreSQL, MySQL and SQL Server. Don't use it in SQLite: there it returns all six rows, because SQLite has no date type and CAST(... AS DATE) turns the text into the number 2026. Use DATE(ordered_at) <> '2026-11-02' in SQLite. For more, see comparing dates in SQL and the BETWEEN dates trap, which is the same problem from the other side.

Not equal in a JOIN

<> in a join condition is how you pair a row with every other row, for example to find customers from the same country:

SELECT a.customer AS customer_1, b.customer AS customer_2
FROM orders a
JOIN orders b
  ON a.country = b.country
 AND a.id <> b.id
WHERE a.country = 'US'
ORDER BY a.id, b.id;

This returns 12 rows: Ana–Chloe, Ana–Eve, Ana–Finn, Chloe–Ana, and so on. Every pair appears twice, once in each order. If you want each pair once, use < instead of <>:

SELECT a.customer AS customer_1, b.customer AS customer_2
FROM orders a
JOIN orders b
  ON a.country = b.country
 AND a.id < b.id
WHERE a.country = 'US'
ORDER BY a.id, b.id;
 customer_1 | customer_2
------------+------------
 Ana        | Chloe
 Ana        | Eve
 Ana        | Finn
 Chloe      | Eve
 Chloe      | Finn
 Eve        | Finn

Six rows, one per pair. The same trick builds round-robin fixtures and "every combination except itself" lists; see the CROSS JOIN guide and the self join lesson. A join on <> alone (with no =) matches almost everything with almost everything, so on big tables it gets slow fast.

Not equal in CASE, HAVING, UPDATE and DELETE

<> works anywhere a condition does. In a CASE expression:

SELECT id, customer,
       CASE WHEN country <> 'US' THEN 'International' ELSE 'Domestic' END AS shipping
FROM orders
ORDER BY id;
 id | customer |   shipping
----+----------+---------------
  1 | Ana      | Domestic
  2 | Ben      | International
  3 | Chloe    | Domestic
  4 | Dev      | International
  5 | Eve      | Domestic
  6 | Finn     | Domestic

(The NULL trap applies here too: a NULL country would land in the ELSE branch and be called "Domestic".)

In HAVING, to filter groups by an aggregate:

SELECT country, COUNT(*) AS orders
FROM orders
GROUP BY country
HAVING COUNT(*) <> 1;

 country | orders
---------+--------
 US      |      4

And in UPDATE and DELETE, where the NULL trap can bite harder because you won't see the rows that were skipped:

-- Meant to archive every unshipped order (assumes an archived column),
-- but silently skips any order whose status is NULL
UPDATE orders SET archived = 1 WHERE status <> 'shipped';

Run the WHERE as a SELECT first, and add OR status IS NULL if those rows should be included.

Does not equal use an index?

Usually not, and that's normally the right call. If 99% of rows are 'shipped', then status <> 'shipped' wants the other 1%, but a B-tree index can't jump straight to "everything except one value". We loaded 200,000 rows (1% pending) with an index on status into PostgreSQL:

EXPLAIN SELECT * FROM big WHERE status != 'shipped';
 Seq Scan on big  (cost=0.00..3588.00 rows=1973 width=12)
   Filter: (status <> 'shipped'::text)

EXPLAIN SELECT * FROM big WHERE status = 'pending';
 Index Scan using big_status on big  (cost=0.29..221.40 rows=1973 width=12)
   Index Cond: (status = 'pending'::text)

EXPLAIN SELECT * FROM big WHERE status < 'shipped' OR status > 'shipped';
 Bitmap Heap Scan on big  (cost=28.38..1169.55 rows=1973 width=12)
   ->  BitmapOr
         ->  Bitmap Index Scan on big_status  Index Cond: (status < 'shipped'::text)
         ->  Bitmap Index Scan on big_status  Index Cond: (status > 'shipped'::text)

The <> query reads the whole table (estimated cost 3588); the same rows fetched with = cost 221. What to do about it:

  • If you know the values you want, ask for them. status IN ('pending', 'cancelled') beats status <> 'shipped' when the excluded value is most of the table.
  • Rewrite as two ranges (< x OR > x) if you need an index and don't know the other values. The planner can combine two index scans.
  • Use a partial or filtered index for a filter you run constantly: CREATE INDEX ix_open ON orders (id) WHERE status <> 'shipped' works as a partial index in PostgreSQL and SQLite and as a filtered index in SQL Server. Queries with the same WHERE can then read just the small index.
  • Don't worry about it on small tables. When <> excludes only a few rows, a full scan is the fastest plan anyway.

Either spelling, <> or !=, produces the same plan.

Not equal by database

PostgreSQLMySQL 8SQL ServerSQLiteOracle
<>YesYesYesYesYes
!=YesYesYesYesYes (also ^=)
NULL-safe not equalIS DISTINCT FROMNOT (a <=> b)IS DISTINCT FROM (2022+)IS DISTINCT FROM (3.39+), IS NOTDECODE(a, b, 0, 1) = 1
Default text comparisonCase-sensitiveCase-insensitiveCase-insensitiveCase-sensitiveCase-sensitive
Trailing spaces count?Yes (VARCHAR)Yes (default collation)NoYesYes (VARCHAR2)
'' is NULL?NoNoNoNoYes

Common mistakes

  1. Expecting <> to return NULL rows. It never does. Add OR col IS NULL or use IS DISTINCT FROM.
  2. Writing <> NULL or != NULL. Always zero rows. Use IS NOT NULL.
  3. Chaining with OR. a <> 1 OR a <> 2 is always true. Use AND, or NOT IN (1, 2).
  4. Using NOT IN with a subquery that can return NULL. You get zero rows. Use NOT EXISTS.
  5. Assuming text comparisons work the same everywhere. MySQL and SQL Server ignore case by default; SQL Server also ignores trailing spaces.
  6. Using <> on a timestamp to exclude a day. It only excludes midnight. Use a range.
  7. Typing =!. It's a syntax error. The operator is !=.
  8. Forgetting parentheses around OR ... IS NULL when there are other AND conditions.

FAQ

What is the not equal operator in SQL?

<> is the standard SQL not equal operator, and != is an alternative that every major database supports. WHERE country <> 'US' and WHERE country != 'US' return the same rows.

Should I use != or <> in SQL?

Either. They are identical in results and performance in PostgreSQL, MySQL, SQL Server, SQLite, Oracle and the cloud warehouses. <> is the ISO standard, so it's the safer choice for portable SQL or strict style guides; != is what most programmers find more readable.

What does <> mean in SQL?

"Not equal to". a <> b is true when a and b are both known and different, false when they're equal, and unknown (NULL) when either side is NULL.

Why does not equal not return NULL values?

Because comparing anything with NULL gives "unknown", not true, and WHERE only keeps rows where the condition is true. To include them, write WHERE col <> 'x' OR col IS NULL, or WHERE col IS DISTINCT FROM 'x' in PostgreSQL, SQL Server 2022 and SQLite.

How do I check for not NULL in SQL?

Use WHERE col IS NOT NULL. col <> NULL and col != NULL return no rows.

How do I write not equal to multiple values in SQL?

Use NOT IN: WHERE status NOT IN ('pending', 'cancelled'). It's the same as status <> 'pending' AND status <> 'cancelled'. Don't use OR between the conditions, and make sure the list (or subquery) contains no NULLs.

Is SQL not equal case sensitive?

It depends on the database and collation. PostgreSQL, SQLite and Oracle compare text case-sensitively by default, so 'Shipped' <> 'shipped' is true. MySQL and SQL Server use case-insensitive collations by default, so it's false. Use LOWER(col) <> 'shipped' to get the same result everywhere.

Can I use not equal with dates?

Yes. <> works on dates and numbers as well as text. On a timestamp column, though, ts <> '2026-11-02' only excludes the exact moment of midnight. To exclude a whole day use ts < '2026-11-02' OR ts >= '2026-11-03'.

Is != slower than <>?

No. The database treats them as the same operator; PostgreSQL shows != as <> in its query plan. Not equal in general can be slower than equals because it usually can't use an index, whichever spelling you use.

Practice

Not equal is one of the first operators you'll reach for, and the NULL rule is what separates a query that looks right from one that is right. Try it in the free WHERE lesson (swap = for <>), then the IN lesson for NOT IN and the NULL lesson for IS NOT NULL. The joins with WHERE lesson uses != on two tables at once. After that, work through the SQL practice problems and keep the SQL cheat sheet handy. NULL comparisons are a favourite in SQL interview questions, so it's worth getting this one right.