Comparison operators are used to compare one expression with another and return TRUE, FALSE, or UNKNOWN (if NULLs are involved). They are most often used in filtering conditions (WHERE and HAVING) to select specific data sets.
Both operators represent “not equal to” in SQL. The != operator is more commonly used in MySQL, PostgreSQL, and SQL Server, while <> is the ANSI SQL standard and works across all database systems.
Any comparison with a NULL value returns UNKNOWN, not TRUE or FALSE. To explicitly check NULLs, you must use IS NULL or IS NOT NULL instead of comparison operators.
SELECT * FROM Employees WHERE bonus IS NULL;
Yes. They are often used with subqueries to compare a single value against the result of another query.
SELECT name, salary
FROM Employees
WHERE salary > (SELECT AVG(salary) FROM Employees);
Comparison operators can be combined using AND, OR, and NOT to form complex conditions.
SELECT * FROM Students
WHERE marks > 80 AND attendance >= 75;
This retrieves students who scored above 80 and have attendance greater than or equal to 75%.