How to Check ‘Is Not Equal’ in SQL
Database:
Operators:
Table of Contents
Problem
You want to check if two values are not equal in SQL.
Example
A social platform’s database has a table named employees with data in the columns first_name
, last_name
, and hire_date
.
first_name | last_name | hire_date |
---|---|---|
Oliver | Crawford | 2021-03-15 |
William | Parker | 2022-01-22 |
Ralph | Crawford | 2009-10-01 |
Lane | Walters | 2020-03-11 |
Keith | Pope | 2021-07-08 |
Jeffrey | Read | 2021-09-25 |
We want to select all employees except those with the last name of Crawford – i.e. we want all surnames not equal to “Crawford”.
Solution
To filter out employees with the last name Crawford, you can use the !=
operator. This operator checks that the values on either side of it are not equal. Let’s see the solution for the example above:
SELECT * FROM employees WHERE last_name != 'Crawford' ; |
Alternatively, you can use the <>
operator, which also means ‘does not equal’ or ‘does not match’. Both operators work in the same way and both will return the same result in all SQL languages.
SELECT * FROM employees WHERE last_name <> 'Crawford' ; |
With either query, this is what you’ll get:
first_name | last_name | hire_date |
---|---|---|
William | Parker | 2022-01-22 |
Lane | Walters | 2020-03-11 |
Keith | Pope | 2021-07-08 |
Jeffrey | Read | 2021-09-25 |
Discussion
Both !=
and work across popular SQL dialects: SQL Server, MySQL, PostgreSQL, Oracle SQL, and SQLite. Both will get you the same result. Both operators work with other data types, including integers, floats, dates, etc.
Let’s explore another example.
Example 2
The social platform’s database has a table named error_report
with data in the columns user_id
, error_code
, and report_date
.
user_id | report_date | error_code |
---|---|---|
VBJ16J | 2021-03-15 | 1 |
AD5723 | 2022-01-22 | 404 |
G8FA5N | 2009-10-01 | 301 |
997TT2 | 2020-03-11 | 1 |
17NBF6 | 2021-07-08 | 402 |
HSIMAO | 2021-09-25 | 405 |
BK5818 | 0001-01-01 | 1001 |
78936H | 0001-01-01 | 1001 |
We want to select all reports except those with the code 1.
Solution 2
Let’s use the !=
operator to compare values in the column error_date
and the error code value 1:
SELECT * FROM error_report WHERE error_date != 1; |
This is the result:
user_id | report_date | error_code |
---|---|---|
AD5723 | 2022-01-22 | 404 |
G8FA5N | 2009-10-01 | 301 |
17NBF6 | 2021-07-08 | 402 |
HSIMAO | 2021-09-25 | 405 |
BK5818 | 0001-01-01 | 1001 |
78936H | 0001-01-01 | 1001 |
The last two records have obviously wrong dates. Let’s filter out these records.
Solution 3
To eliminate records with faulty dates from the results, let’s add another comparison between the date in the column report_date
and the value '0001-01-01'
:
SELECT * FROM error_report WHERE error_date != 1 AND report_date <> '0001-01-01' ; |
And here’s our result:
user_id | report_date | error_code |
---|---|---|
AD5723 | 2022-01-22 | 404 |
G8FA5N | 2009-10-01 | 301 |
17NBF6 | 2021-07-08 | 402 |
HSIMAO | 2021-09-25 | 405 |