SQL How to perform string does not equal
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
SQL provides two operators for "not equal" comparisons: <> (the SQL standard) and != (widely supported but not in the original standard). Both work identically for string comparisons and exclude rows where the column matches the specified value. However, neither operator matches NULL values — NULL <> 'value' evaluates to NULL, not TRUE. This article covers string inequality patterns, NULL handling, and related filtering techniques.
Basic String Not Equal
Both queries return all employees whose department is not 'Sales'. Rows where department is NULL are excluded from the results because NULL <> 'Sales' evaluates to NULL (unknown), not TRUE.
Handling NULL Values
IS DISTINCT FROM treats NULL as a comparable value — NULL IS DISTINCT FROM 'Sales' returns TRUE.
Case Sensitivity
String comparison behavior depends on the database and collation:
NOT Equal with Multiple Values
NOT IN is cleaner for excluding multiple specific values. Note that NOT IN with a NULL in the list returns no rows — filter NULLs from the subquery.
Pattern-Based Exclusion
NOT Equal in JOINs
Database-Specific Syntax
| Database | Standard <> | Non-standard != | IS DISTINCT FROM |
| PostgreSQL | Yes | Yes | Yes |
| MySQL | Yes | Yes | Yes (8.0+) |
| SQL Server | Yes | Yes | No (use IS NULL workaround) |
| Oracle | Yes | Yes | No (use DECODE or NVL) |
| SQLite | Yes | Yes | IS NOT (similar) |
Common Pitfalls
- Forgetting that
<>does not match NULL:WHERE col <> 'value'silently excludes rows wherecolis NULL. If NULL rows should be included, addOR col IS NULLor useIS DISTINCT FROM(PostgreSQL/MySQL 8.0+). - Using
NOT INwith a subquery that returns NULL:WHERE col NOT IN (SELECT ...)returns no rows if the subquery produces any NULL values. Filter NULLs from the subquery:WHERE col NOT IN (SELECT val FROM t WHERE val IS NOT NULL). - Assuming case-insensitive comparison across databases: MySQL's default collation is case-insensitive, but PostgreSQL is case-sensitive. Code that works in MySQL (
'alice' <> 'Alice'is false) fails in PostgreSQL (it is true). UseLOWER()orCOLLATEfor portable case-insensitive comparisons. - Using
!=in strict SQL standard contexts: While!=works in all major databases, it is not part of the original SQL standard. Use<>in code that must be strictly standards-compliant or portable across uncommon databases. - Comparing with empty string vs NULL: In Oracle, empty string
''is treated as NULL.WHERE col <> ''behaves differently in Oracle than in PostgreSQL or MySQL. Be explicit about NULL handling when targeting multiple databases.
Summary
- Use
<>(standard) or!=(widely supported) for string not-equal comparisons - Neither operator matches NULL — add
OR col IS NULLor useIS DISTINCT FROMto include NULLs - Use
NOT IN (...)for excluding multiple specific values - Use
NOT LIKEorNOT REGEXPfor pattern-based exclusion - Case sensitivity depends on the database collation — use
LOWER()for portable case-insensitive comparisons - Always test NULL handling explicitly, especially with
NOT INsubqueries
Related reading
- SQL Identity autonumber is Incremented Even with a Transaction Rollback
- SQL injection that gets around mysql_real_escape_string
- SQL JOIN what is the difference between WHERE clause and ON clause?
- SQL JPA - Multiple columns as primary key
- SQL keys, MUL vs PRI vs UNI
- SQL multiple column ordering
- sql multithreading application select and delete from a table
- SQL MySQL vs NoSQL CouchDB

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.