In SQL, you can use several operators to represent "not equal to." The two most common operators are <> and !=. Both operators are used to compare two values and return true if the values are not equal.
Examples:
Using <>
Operator
SELECT *
FROM employees
WHERE salary <> 50000;
This query retrieves all rows from the employees
table where the salary
is not equal to 50000
.
Using != Operator
SELECT *
FROM employees
WHERE salary != 50000;
This query does the same thing as the previous one: it retrieves all rows from the employees
table where the salary
is not equal to 50000
.
Example Table: employees
Assume we have the following employees
table:
emp_id emp_name salary
1 John 50000
2 Jane 75000
3 Jake 45000
4 Jill 90000
Practical Examples
Retrieving Employees with Salary Not Equal to 50000
Using either <>
or!=
:
SELECT *
FROM employees
WHERE salary <> 50000;
or
SELECT *
FROM employees
WHERE salary != 50000;
Result:
emp_id emp_name salary
2 Jane 75000
3 Jake 45000
4 Jill 90000
Combining with Other Conditions
You can also combine the "not equal to" condition with other conditions using AND
or OR
.
SELECT *
FROM employees
WHERE salary != 50000 AND emp_name <> 'Jane';
This query retrieves all rows from the employees table where the salary is not equal to 50000 and the emp_name is not 'Jane'.
Result:
emp_id emp_name salary
3 Jake 45000
4 Jill 90000
Using NOT
with Equality
An alternative approach is to use the NOT
keyword with the equality operator (=
).
SELECT *
FROM employees
WHERE NOT salary = 50000;
This query also retrieves all rows where the salary
is not equal to 50000.
Understanding and using the "not equal to" operators in SQL is essential for filtering data based on inequality conditions effectively. Both <>
and !=
are widely supported and commonly used in SQL queries.