SQL Comparison Operators
SQL comparison operators are used to compare the values in SQL statements. They are used to compare one value against another to determine whether the condition is true or false. Comparison operators return a logical value of TRUE or FALSE based on the comparison.
Syntax
The syntax of SQL comparison operators is as follows:
value1 operator value2
Here, value1
and value2
can be any valid SQL expression that returns a scalar value, and operator
is one of the comparison operators listed below:
Comparison Operator | Description |
---|---|
= | Equal to |
<> or != | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal to |
>= | Greater than or equal to |
BETWEEN | Between a range of values |
LIKE | Searching for a pattern |
IN | Checking whether a value exists in a set of values |
IS NULL | Checking for NULL values |
IS NOT NULL | Checking for non-NULL values |
Example
Let's consider a table named inventory
with the columns item
and quantity
. We can use the comparison operators to filter the data in this table based on certain conditions.
SELECT * FROM inventory WHERE item = 'Apple';
This SQL statement retrieves all the rows from the inventory
table where the item
is equal to Apple
.
Output
The output of the above SQL statement will be a list of rows that meet the condition.
Item | Quantity |
---|---|
Apple | 10 |
Explanation
The above SQL statement uses the equality operator =
to compare the value of the item
column to the string literal Apple
. The result of the comparison is true
for the row where the item
is equal to Apple
. The WHERE
clause filters the rows to only include those where the comparison is true.
Use
SQL comparison operators are used to filter data based on certain conditions. They are used in the WHERE
clause of SQL statements to selectively retrieve data from a table that meets certain criteria.
Important Points
- Comparison operators are used to test for equality, inequality, greater than, less than, etc.
- Comparison operators compare values of the same data type.
- Comparison operators always return a Boolean value of true or false.
Summary
Comparison operators are an essential part of SQL. They are used to filter data based on certain conditions and return a Boolean value of true or false. Commonly used comparison operators in SQL include =
, <>
or !=
, <
, >
, <=
, >=
, BETWEEN
, LIKE
, IN
, IS NULL
, and IS NOT NULL
.