SQL comparison operators are used to compare values in a database, usually within the WHERE clause, to filter records based on specific conditions. These operators return true or false based on the comparison.


List of SQL Comparison Operators:

OperatorDescription
=Checks if two operands are equal.
!=Checks if two operands are not equal (also <> in some databases).
>=Checks if the left operand is greater than or equal to the right operand.
<Checks if the left operand is less than the right operand.
>Checks if the left operand is greater than the right operand.
<=Checks if the left operand is less than or equal to the right operand.

Syntax:

SELECT * FROM table_name
WHERE column_name condition_operator value;
  • column_name: The column to filter.
  • condition_operator: One of the comparison operators.
  • value: The value to compare against.

Examples:

  1. Equal to (\=):

    • Finds rows where the MARKS column equals 50.
    SELECT * FROM MATHS WHERE MARKS = 50;
  2. Greater than (>):

    • Finds rows where MARKS are greater than 60.
    SELECT * FROM MATHS WHERE MARKS > 60;
  3. Less than (<):

    • Finds rows where MARKS are less than 40.
    SELECT * FROM MATHS WHERE MARKS < 40;
  4. Greater than or equal to (>=):

    • Finds rows where MARKS are greater than or equal to 80.
    SELECT * FROM MATHS WHERE MARKS >= 80;
  5. Less than or equal to (<=):

    • Finds rows where MARKS are less than or equal to 30.
    SELECT * FROM MATHS WHERE MARKS <= 30;
  6. Not equal to (!= or <>):

    • Finds rows where MARKS are not equal to 70.
    SELECT * FROM MATHS WHERE MARKS != 70;

Key Points:

  • SQL comparison operators are used within the WHERE clause to filter results.
  • They are essential for specifying conditions and retrieving the data you need.
  • Operators such as \= and != check for equality, while > and < check for greater/lesser values.
  • The >= and <= operators allow for inclusive comparisons.
  • Comparison operators can be used with numeric, string, and date values.