How to write NOT EQUAL in SQL

How to write NOT EQUAL in SQL

In Structured Query Language (SQL), comparison operators are essential for filtering data based on conditions. One of the most commonly used is the “not equal” operator, which checks whether two values are different. While this seems straightforward, SQL provides multiple ways to express inequality — and not all of them are standardized.

This article explores the ISO-standard and non-standard ways of expressing “not equal” in SQL, supported by example data and queries for practical understanding.

Learn SQL online

For our instructor lead, more in depth look at learning to write a SQL SELECT, SORT BY and WHERE clauses why not check out our video course hosted on UDEMY or enjoy the free SQL course below.

ISO Standard Syntax: <>

The ISO SQL standard defines <> as the correct operator for not equal. This syntax is supported across all major relational database systems, including:

  • PostgreSQL

  • MySQL

  • Oracle

  • SQL Server

  • SQLite

Example Usage

				
					SELECT * FROM employees
WHERE department_id <> 10;
				
			

This query returns all employees who are not in department 10.

Non-ISO (Vendor-Specific) Syntax: !=

Many SQL engines also allow != as a shorthand for inequality. While this is not part of the ISO standard, it’s widely supported as a non-standard extension, particularly by:

  • MySQL

  • PostgreSQL

  • SQL Server

  • SQLite

However, Oracle does not support !=, and using it will result in a syntax error. For maximum compatibility, prefer <>.

Example Usage

				
					SELECT * FROM employees
WHERE department_id != 10;
				
			

This query behaves the same way as the previous example — selecting employees not in department 10 — but it may not work on every RDBMS.

Sample Data

Consider the following employees table:

employee_idnamedepartment_id
1Alice10
2Bob20
3Carol30
4Dave10

Using the <> operator:

				
					SELECT name FROM employees
WHERE department_id <> 10;
				
			

Result:

name
Bob
Carol

The same result would be returned using != in most databases:

				
					SELECT name FROM employees
WHERE department_id != 10;
				
			

Summary of Compatibility

SyntaxISO StandardSupported ByNotes
<>✅ YesAll major SQL databasesRecommended for portability
!=❌ NoMySQL, PostgreSQL, SQL Server, SQLiteMay cause errors in Oracle

Best Practice

  • Use <> for maximum portability and standards compliance.

  • Use != only if you are certain of the database system in use and prefer the syntax.

Bonus Tip: Handling NULL with Inequality

Neither <> nor != will return rows where the column is NULL, due to SQL’s three-valued logic (TRUE, FALSE, UNKNOWN). Use IS NOT NULL to explicitly filter out NULL values.

				
					SELECT * FROM employees
WHERE department_id <> 10 AND department_id IS NOT NULL;
				
			

Conclusion

While both <> and != are widely used in SQL for expressing inequality, only <> is officially standardized by ISO. Using <> ensures compatibility across different database systems, making it the preferred choice in multi-database environments or in codebases meant to be portable.

Always be aware of how your target database interprets SQL syntax and handle NULL values explicitly to avoid unexpected results.

Please see the other SQL video learning articles below as part of this learning SQL online course video. 

If you would like an instructor lead SQL course that goes into more detail with worked examples then we also provide a video course to learn SQL online hosted on UDEMY. 

Learning SQL programming with SQL server
Learn SQL Online