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:
| Operator | Description |
|---|---|
= | 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:
-
Equal to (
\=):- Finds rows where the
MARKScolumn equals 50.
SELECT * FROM MATHS WHERE MARKS = 50; - Finds rows where the
-
Greater than (
>):- Finds rows where
MARKSare greater than 60.
SELECT * FROM MATHS WHERE MARKS > 60; - Finds rows where
-
Less than (
<):- Finds rows where
MARKSare less than 40.
SELECT * FROM MATHS WHERE MARKS < 40; - Finds rows where
-
Greater than or equal to (
>=):- Finds rows where
MARKSare greater than or equal to 80.
SELECT * FROM MATHS WHERE MARKS >= 80; - Finds rows where
-
Less than or equal to (
<=):- Finds rows where
MARKSare less than or equal to 30.
SELECT * FROM MATHS WHERE MARKS <= 30; - Finds rows where
-
Not equal to (
!=or<>):- Finds rows where
MARKSare not equal to 70.
SELECT * FROM MATHS WHERE MARKS != 70; - Finds rows where
Key Points:
- SQL comparison operators are used within the
WHEREclause 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.