Learn SQL
-- filtering.not_equal

Not Equal ( != , <> ) in SQL

Exclude values from your results

--concept

Two operators mean “not equal”: != and <> (the SQL-standard spelling). WHERE department <> 'Sales' keeps every row except Sales. One trap mirrors NULL logic: comparing against NULL with != returns UNKNOWN, so those rows silently vanish — combine with IS NOT NULL when the column can be blank.

--how it works
1

Two spellings, one meaning

!= and <> both mean "not equal"; <> is the ANSI standard, != the popular alias.

2

Exclude exactly one value

WHERE department <> 'Sales' removes only exact Sales rows.

3

Chain exclusions with AND

dept <> 'Sales' AND dept <> 'Marketing' — or the tidier NOT IN ('Sales', 'Marketing').

4

Prefer NOT IN for lists

One clause, obvious intent, identical plan.

5

Mind the NULL hole

salary <> 50000 also drops rows where salary is NULL — the comparison is UNKNOWN there.

--visualization
department <>
idnamedepartment1AliceEngineering2BobSales3CynthiaEngineering4DavidMarketing5ElenaSales6FaridEngineering<> and != are identical in every major engine
Stage 1 / 8
WHERE department <> 'Sales' — everything EXCEPT that exact value survives.
--example
query.sql
1SELECT name, department
2FROM employees
3WHERE department <> 'Sales';
Result
namedepartment
AliceEngineering
CynthiaEngineering
DavidMarketing
FaridEngineering

Bob and Elena (Sales) are excluded; everyone else survives. Try <> 'Engineering' in the visualization to flip which half disappears.

--variants

Exclude one department

Everything except Sales.

query.sql
1SELECT name, department
2FROM employees
3WHERE department <> 'Sales';
Result
namedepartment
AliceEngineering
CynthiaEngineering
DavidMarketing
FaridEngineering
1Bob and Elena are the two Sales rows — out.
2The other four pass the inequality.
3<> and != behave identically here.

Exclude several values

AND-chained inequalities vs NOT IN.

query.sql
1SELECT name, department
2FROM employees
3WHERE department <> 'Sales'
4 AND department <> 'Engineering';
Result
namedepartment
DavidMarketing
1Both tests must pass — only Marketing survives.
2Equivalent: department NOT IN ('Sales', 'Engineering').
3NOT IN scales to ten values; AND-chains do not.

Numbers and <>

Same operator, numeric column.

query.sql
1SELECT name, salary FROM employees
2WHERE salary <> 52000;
Result
namesalary
Alice85000
Cynthia91000
David47000
Elena58000
Farid73000
1Only Bob earns exactly 52000 — he is excluded.
2The other five pass; no rounding surprises with integers.
3On DECIMAL money columns, <> against a literal is exact by design.

The NULL hole

Where <> silently loses rows.

query.sql
1SELECT name FROM employees
2WHERE bonus <> 0;
Result
namebonus
David3000
Elena2500
Alice5000
1Bob, Cynthia and Farid have bonus NULL — 3000-row comparisons cannot call them <> 0.
2Their rows vanish without error or warning.
3Fix: bonus <> 0 OR bonus IS NULL — or COALESCE(bonus, 0) <> 0.
--key facts
Standard form<> works in every SQL dialect; != is near-universal sugar
NULL trapanything <> NULL is UNKNOWN — add IS NOT NULL when unsure
ReadabilityNOT IN lists beat long chains of <> AND <>
--use cases
Excluding sentinel rows: status <> 'archived'
Blacklists: category NOT IN ('internal', 'test')
Removing the current user/row from a comparison list
Interview trap questions about NULL and inequality
--under the hood
<> is generally NOT index-friendly on its own — "not equals" matches most rows, so engines prefer full scans plus filter.
When one value dominates a table, exclude it explicitly and let the rest ride an index on a positive predicate.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Take the queries you just visualized and see the data structures that power real databases.