In SQL, the comparison operators are useful to compare one expression with another expression using mathematical operators like equal (=), greater than (>), less than (*), greater than or equal to (>=), less than or equal to (<=), not equal (<>), etc. on SQL statements. In SQL, we have different types of comparison operators available those are
Before we check each operator, try to create an “EmployeeDetails” table by using the below script in the SQL database.
Once we run the above SQL script, our table “EmployeeDetails” will create, and the result as shown below.
Now we will learn each comparison operator in SQL with proper examples.
In SQL, the equal operator is useful to check whether the given two expressions are equal or not. If it’s equal, then the condition will be true, returning matched records.
Example:
If we run the following SQL statement for the equal operator, it will return records where empid equals 1.
When we execute the above SQL equal operator query, we will get the result below.
In SQL, the not equal operator is used to check whether two expressions are equal or not. If it’s not equal, then the condition will be true, and it will return not matched records.
Example:
If we run the following SQL statement for not equal operator it will return records where empid is not equal to 1.
When we execute the above SQL not equal operator query, we will get the result below.
In SQL, not equal operator is used to check whether two expressions are equal or not. If it’s not equal, then the condition will be true, and it will return not matched records. Both != and <> operators are not equal operators and will return the same result, but the != operator is not an ISO standard.
Example:
If we run the following SQL statement for not equal operator it will return records where empid is not equal to 1.
When we execute the above SQL not equal operator query, we will get the result below.
In SQL, greater than operator is used to check whether the left-hand operator is higher than the right-hand operator or not. If the left-hand operator is higher than the right-hand operator, the condition will be true and return matched records.
Example:
If we run the following SQL statement for greater than operator it will return records where empid greater than 2.
When we execute the above SQL greater than the operator query, we will get the result below.
In SQL, less than operator is used to check whether the left-hand operator is lower than the right-hand operator or not. If the left-hand operator is lower than the right-hand operator, the condition will be true and return matched records.
Example:
If we run the following SQL statement for less than operator, it will return records where empid is less than 2.
When we execute the above SQL less than operator query, we will get the result below.
In SQL, greater than or equal to the operator is used to check whether the left-hand operator is higher than or equal to the right-hand operator or not. If the left-hand operator is higher than or equal to the right-hand operator, the condition will be true and return matched records.
Example:
If we run the following SQL statement for greater than or equal to the operator it will return records where empid is higher than or equal to 2.
When we execute the above SQL greater than or equal to operator query, we will get the result below.
In SQL, less than or equal to the operator is useful to check whether the left-hand operator is lower than or equal to the right-hand operator or not. If the left-hand operator is lower than or equal to the right-hand operator, then the condition will be true and return matched records.
Example:
If we run the following SQL statement for less than or equal to the operator, it will return records where empid is lower than or equal to 2.
When we execute the above SQL less than or equal to operator query, we will get the result below.
In SQL, not less than operator is used to check whether the left-hand operator is not lower than the right-hand operator or not. If the left-hand operator is not lower than the right-hand operator, the condition will be true and return matched records.
Example:
If we run the following SQL statement for not less than operator it will return records where empid is not lower than 2.
When we execute the above SQL not less than operator query, we will get the result below.
In SQL, not greater than operator is used to check whether the left-hand operator is not higher than the right-hand operator or not. If the left-hand operator is not higher than the right-hand operator, the condition will be true and return matched records.
Example:
If we run the following SQL statement for not greater than operator, it will return records where empid is not higher than 2.
When we execute the above SQL not greater than operator query, we will get the result below.
For more information related to comparison operators in SQL server, check the following information.
Operator | Description |
---|---|
Equal (=) Operator | The sql equal operator is used to check whether two expressions are equal or not. If it’s equal, the condition will be true and will return matched records. |
Not Equal (!=) Operator | The sql not equal operator is used to check whether two expressions are equal or not. If it’s not equal, then the condition will be true, and it will return not matched records. |
Not Equal (<>) Operator | The sql not equal operator is used to check whether two expressions are equal or not. If it’s not equal, then the condition will be true, and it will return not matched records. |
Greater Than (>) Operator | The sql Greater Than operator is used to check whether the left-hand operator is higher than the right-hand operator or not. If the left-hand operator is higher than the right-hand operator, the condition will be true and return matched records. |
Less Than (<) Operator | The sql Less Than operator is used to check whether the left-hand operator is lower than the right-hand operator or not. If the left-hand operator is lower than the right-hand operator, the condition will be true and return matched records. |
Greater Than or Equal To (>=) Operator | The sql Greater Than or Equal To operator is useful to check whether the left-hand operator is higher than or equal to the right-hand operator or not. If the left-hand operator is higher than or equal to the right-hand operator, the condition will be true, returning matched records. |
Less Than or Equal To (<=) Operator | The sql Less Than or Equal To operator is useful to check whether the left-hand operator is lower than or equal to the right-hand operator or not. If the left-hand operator is lower than or equal to the right-hand operator, the condition will be true and return matched records. |
Not Less Than (!<) Operator | The sql Not Less Than operator is used to check whether the left-hand operator not lower than the right-hand operator or not. If the left-hand operator is not lower than the right-hand operator, the condition will be true and return matched records. |
Not Greater Than(!>) Operator | The sql Not Less Than operator is used to check whether the left-hand operator not lower than the right-hand operator or not. If the left-hand operator is not lower than the right-hand operator, the condition will be true and return matched records. |