NOT EQUAL TO Operator (<>) - SQL
Overview
The Not equal to operator (=) in SQL is used to compare two expressions and determine if they are not equal. Different SQL dialects use different symbols for this operator. Here are the commonly used "not equal to" operators:
<> (Standard SQL)
!= (Alternative)
Syntax:
SELECT column1 <> value
column1 <> value
checks if the value in column1
is not equal to value
.
Sample Data:
first_name | vacation_days |
---|---|
Frank | 5 |
Jane | 2 |
Ashley | 3 |
Glenn | -3 |
Kelly | 2 |
Richard | -7 |
George | 2 |
Kyle | 1 |
James | -2 |
Gustavo | -10 |
Example: Hard-coded values (TRUE) value
In this example, the Not equal to operator is used to evaluate whether the expression 5 <> 2 is true. Since 5 does not equal to 2, the query will return an output of 1, indicating that it is true.
Example:
SELECT 5 <> 2
Example: Hard-coded values (FALSE) value
In this example, the Not equal to operator is used to evaluate whether the expression 5 <> 5 is true. Since 5 is equal to 5, the query will return an output of 0, indicating that it is false.
Example:
SELECT 5 <> 5
Example: Query without the WHERE Statement
We are returning the vacation_days column and a column that checks if each value in the vacation_days column is not equal to 2. The final output is the original vacation_days column alongside the column checking whether the value is not equal to 2. If the value is not equal to 2, it will return a 1 and if it does equal 2, it will return a 0.
Example:
SELECT vacation_days, vacation_days <> 2
FROM company.employees
Example: Query with the WHERE Statement
In this example, we are filtering for only rows in department_id 1. We are returning the vacation_days column and a column that checks if each value in the vacation_days column is not equal to 2. The final output is the original vacation_days column alongside the column checking whether the value is not equal to 2. If the value is not equal to 2, it will return a 1 and if it does equal 2, it will return a 0.
Example:
SELECT vacation_days, vacation_days <> 2
FROM company.employees
WHERE department_id = 1