In MySQL, the NOT EQUAL
operator is used to check whether two values are not equal to each other. There are two ways to write the NOT EQUAL
condition:
- Using
!=
- Using
<>
Both operators are functionally identical and can be used interchangeably.
Syntax:
SELECT * FROM table_name
WHERE column_name != value;
or
SELECT * FROM table_name
WHERE column_name <> value;
Example:
Let’s assume we have a table called employees
with a column age
, and we want to select all employees whose age is not equal to 30:
SELECT * FROM employees
WHERE age != 30;
or
SELECT * FROM employees
WHERE age <> 30;
Both queries will return all records from the employees
table where the age
is not equal to 30.
Notes:
!=
and<>
are the standard ways to represent “not equal” in SQL.- Be aware that
NULL
values are not considered equal to any other value, includingNULL
itself. To check forNULL
values, you would use theIS NOT NULL
condition.