Robert Johns | 26 Feb, 2025
Fact checked by Jim Markus

SQL Not Equal To | Docs With Examples

SQL Not Equal To (<> or !=), allows users to exclude specific values when retrieving data from a database. As one of the most essential SQL operators, the not equal operator is crucial for performing inequality operations and for filtering data in SQL queries.

What is the SQL Not Equal To Operator?

The SQL Not Equal To operator is used to filter out results that do not match a specific value. It helps in conditions where you want to exclude certain records from your query output. This operator increases flexibility in filtering datasets.

Syntax:

SELECT column_name(s)
FROM table_name
WHERE column_name <> value;

Alternatively, you can use != as well:

SELECT column_name(s)
FROM table_name
WHERE column_name != value;

Common Questions About <> in SQL

Are <> and != the Same in SQL?

In most relational databases, the <> and != operators function the same way. Both are used to check for inequality and can be used interchangeably when crafting SQL commands. However, some older SQL dialects prefer <> as it is the standard ANSI SQL operator.

How to Use Not Equals in SQL?

You can use either <> or != in the WHERE clause of a query to filter out specific values from the result set.

Example:

SELECT *
FROM employees
WHERE department <> 'IT';

This query returns all employees who are not in the IT department.

What is <> in SQL?

The <> symbol in SQL represents the not equal to operator. It is used to exclude specific values from query results and is commonly used in complex queries with multiple conditions.

Why Use <> Instead of !=?

Although != works in most modern databases, <> is the standard ANSI SQL operator for inequality. Using <> ensures better consistency across different DBMS.

Which Operator is <>?

The <> operator is the standard SQL not equal to comparison operator. It is used to check if two values are not equal.

What is <> in Queries?

When using <> in an SQL query, it filters out rows where the specified specific column does not match the given value.

Example:

SELECT *
FROM products
WHERE price <> 500;

This query retrieves all products where the price is not 500.

Is <> and != the Same in SQL?

Yes, both <> and != perform the same function in SQL, checking for inequality between values. While != is widely supported, <> is the preferred standard in ANSI SQL.

SQL Not Equal To with Numeric Values

Let's consider an example where we need to filter numeric data. Consider a products table:

product_id product_name price
101 Laptop 1000
102 Phone 500
103 Tablet 700
104 Monitor 300

To select products that are not priced at 500, use:

SELECT *
FROM products
WHERE price <> 500;

SQL Not Equal To with Other Conditions

Let's consider another example where we combine <> with other conditions using AND or OR.

This query will return employees who are not in IT and not in HR:

SELECT *
FROM employees
WHERE department <> 'IT' AND department <> 'HR';

NULL Values and Not Equal To

The <> and != operators do not work as expected with NULL values. Since NULL represents an unknown value, any comparison involving NULL will return FALSE.

Consider the following orders table and let's look at an example where we need to handle NULL values.

order_id customer amount
1 John 100
2 Alice NULL
3 Bob 200
4 Eve NULL

If you run the following query:

SELECT *
FROM orders
WHERE amount <> 100;

It will return:

order_id customer amount
3 Bob 200

The rows where amount is NULL are excluded from the result. To include NULL values, you need to explicitly check for them:

SELECT *
FROM orders
WHERE amount <> 100 OR amount IS NULL;

Key Takeaways

  • The SQL Not Equal To operator (<> or !=) excludes specific values in SQL queries.
  • Both <> and != function the same way in most SQL DBMS.
  • <> is the standard ANSI SQL operator for inequality, while != is widely accepted.
  • When using <> with NULL values, comparisons will not work as expected unless IS NULL is explicitly checked.
  • It can be combined with AND, OR, and other conditions to refine queries.
  • Useful for query execution involving aggregation and Boolean values.
  • Parentheses can be used to structure queries for clarity.
  • The SQL Not Equal To operator improves query flexibility, making it essential for your SQL cheat sheet.

Wrapping Up

The SQL not equal to operator is a fundamental tool for filtering data by excluding specific values. By understanding how to use it effectively with different data types and in combination with other conditions, you can write more powerful and precise SQL queries. This topic is essential for working with relational databases and structuring queries effectively. Happy querying!

By Robert Johns

Technical Editor for Hackr.io | 15+ Years in Python, Java, SQL, C++, C#, JavaScript, Ruby, PHP, .NET, MATLAB, HTML & CSS, and more... 10+ Years in Networking, Cloud, APIs, Linux | 5+ Years in Data Science | 2x PhDs in Structural & Blast Engineering

View all post by the author

Subscribe to our Newsletter for Articles, News, & Jobs.

I accept the Terms and Conditions.

Disclosure: Hackr.io is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.

In this article

Learn More

Please login to leave comments