SQL Comparison Operator
SQL Comparison Operators are used to compare two values, columns, or expressions in a database. They are mostly used with the WHERE clause to filter rows based on specific conditions.
What Are Comparison Operators?
- Used in SQL queries to compare values in columns.
- Return results that match, differ, or are greater/less than a given value.
- Widely used in SELECT, UPDATE, and DELETE statements with the WHERE clause.
List of SQL Comparison Operators
Student Table
Example: Using equal to ( = )
Output:
More Use Cases
> Greater Than
Returns students with chemistry marks grater than 91.
!= Not Equal
Returns all students except Aman.
Operator | Description |
---|---|
= | Equal to - returns rows equal to the given value |
> | Greater than - returns rows with values greater |
< | Less than - returns rows with values less |
>= | Greater than or equal to - returns rows greater than or equal to the value |
<= | Less than or equal to - returns rows less than or equal to the value |
!= or <> | Not equal to - returns rows not matching the value |
Student Table
Header 1 | Header 2 | Header 3 | Header 4 |
---|---|---|---|
1 | Aman | 86 | 92 |
2 | Sushant | 91 | 91 |
3 | Soumya | 98 | 98 |
Example: Using equal to ( = )
SELECT *
FROM student
WHERE physics = 86;
Output:
ID | Name | Physics | Chemistry |
---|---|---|---|
1 | Aman | 86 | 92 |
Explanation: This query selects the row(s) where the value in the Physics column is exactly equal to 86.
More Use Cases
> Greater Than
SELECT * FROM student WHERE chemistry > 91;
Returns students with chemistry marks grater than 91.
!= Not Equal
SELECT * FROM student WHERE name != 'Aman';
Returns all students except Aman.
Key Point
- Comparison operators return TRUE or FALSE for each row, determining whether to include it in the result.
- Can be used to compare columns, values, or expressions.
- Can be combined with logical operators (AND, OR, NOT) for advanced filtering.