Home Tutorials SQL Tutorial Comparison Operators
Comparison Operators

Comparison Operators


Comparison Operators

Definition: Comparison operators are symbols used in SQL filters to determine the relationship between two values. They are the building blocks of the WHERE clause, allowing the database to evaluate whether a record should be included in your results.

Usage: Beginner SQL tutorials introduce these early because they allow you to move beyond simply viewing data to actually asking specific questions, such as "Which students are from a specific city?" or "Which products are out of stock?"


Common Comparison Operators

These operators are used to compare a column's value against a specific criteria:

Operator Description Example Usage
= Equal to Finds an exact match for text or numbers.
> Greater than Finds values strictly higher than the target.
< Less than Finds values strictly lower than the target.
>= Greater than or equal to Includes the target value and anything higher.
<= Less than or equal to Includes the target value and anything lower.
<> or != Not equal to Excludes a specific value from the results.

Practical Example

To retrieve all student records for those living specifically in Bengaluru, you use the Equal to operator:

SELECT *
FROM students
WHERE city = 'Bengaluru';

Explanation

  • Filtering by Text: When comparing text (like 'Bengaluru'), always wrap the value in single quotes.
  • Filtering by Number: When comparing numbers (like age > 18), do not use quotes.
  • Logical Evaluation: For every row in the students table, the database asks: "Is the city equal to Bengaluru?" If the answer is True, the row is displayed. If False, the row is hidden.

Key Notes

  • Standardization: While != is widely accepted in modern SQL, <> is the official ISO standard for "not equal to." Both usually work, but <> is more portable across different database types.
  • Null Values: Operators like = or != do not work on NULL (empty) values. To find empty data, you must use IS NULL or IS NOT NULL.
  • Case Sensitivity: Depending on the database settings (Collation), 'bengaluru' might be treated differently than 'Bengaluru'. It is best practice to match the case exactly as it is stored.

🏋️ Test Yourself With Exercises

Take our quiz on Comparison Operators to test your knowledge.

Browse Quizzes »