Types of SQL Comparison Operators (Transact-SQL)

Summary: In this tutorial, you will learn the Types of SQL Comparison Operators (Transact-SQL) With Practical Example, and How to use SQL Comparison Operators to compare two values.
SQL Comparison operators are used to test whether two expressions are the same. Furthermore, it can be used on all expressions except expressions of the text, ntext, or image data types.

What is SQL Comparison Operators

Comparison operators in SQL are used when you want to check the conditions that compare one expression with another. Furthermore, the result of comparison in SQL can be TRUE, FALSE, or UNKNOWN (Note:- an operator that has one or two NULL expressions then it returns UNKNOWN).

In SQL, to compare one expression with another expression then we will be 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 SQL comparison operators available those are:-

Operator Description
= Equal to
!= Not Equal to
> Greater than
< Less than
>= Greater than equal to
<= Less than equal to.
<> Not equal to
!< Not Less Than Operator
!> Not Greater Than Operator

Demo Database Table: EmployeeDetails

First of all, before we proceed to check each operator try to create an “EmployeeDetails” table by using the below script in the SQL database.

When we run the above SQL script our table “EmployeeDetails” will create and the result will be like as shown below:-
EmployeeDetails Demo Table

SQL Comparison Operator in SQL with proper examples

Here, we learn the different types of comparison operators in SQL with a practical examples.

I) SQL Equal (=) Operator

In this SQL example, the equal operator is useful to check whether the given two expressions are equal or not. Furthermore, if it’s equal, then the condition will be true and it will return matched records from the database.

Example:

If we run the following given below SQL statement for the equal operator it will return records where Empid equals 13.


sql comparison operators: Equal operator example

II) SQL Not Equal (!=) Operator

This SQL example illustrates, NOT Equal operator is used for the purpose of checking whether two expressions are equal or not. If it’s not equal then the condition will be true and then it will return not matched records.
Example:
If we run the given below following SQL query for not equal operator then it will return records where Empid does not equal to 12


SQL Not Equal Operator

III) SQL Greater Than (>) Operator

This SQL example illustrates, greater than an operator, is used for the purpose of checking whether the left-hand operator is higher than the right-hand operator or not. Furthermore, If the left-hand operator is higher than the right-hand operator. in that condition, this will be true and it will return matched records.
Example:
If we run the given below following SQL statement for greater than operator then it will return records where Empid is greater than 12


sql comparison operators: SQL Greater Than Operator

IV) SQL Less Than (<) Operator

This SQL example illustrates, less than an operator, is used for the purpose of checking whether the left-hand operator is lower than the right-hand operator or not. Furthermore, If the left-hand operator is lower than the right-hand operator. in that condition, this will be true and it will return matched records.
Example:
If we run the given below following SQL statement for less than operator then it will return records where Empid is less than 12


sql comparison operators: SQL Less Than Operator

V) SQL Greater Than or Equal To (>=) Operator

This SQL example illustrates, greater than or equal to the operator is used for the purpose of checking whether the left-hand operator is higher than or equal to the right-hand operator or not. Furthermore, If the left-hand operator is higher than or equal to the right-hand operator. in that condition, this will be true and it will return matched records.
Example:
If we run the given below following SQL statement for greater than or equal to operator then it will return records where Empid is higher than or equal to 12


SQL Greater Than or Equal To operator

VI) SQL Less Than or Equal To (<=) Operator

This SQL example illustrates, less than or equal to the operator is used for the purpose of checking whether the left-hand operator is less than or equal to the right-hand operator or not. Furthermore, If the left-hand operator is less than or equal to the right-hand operator. in that condition, this will be true and it will return matched records.
Example:
If we run the given below following SQL statement for less than or equal to operator then it will return records where Empid is less than or equal to 12


SQL Less Than or Equal To operator

VII) SQL Not Equal (<>) Operator

This SQL example illustrates, NOT Equal operator is used for the purpose of checking whether two expressions are equal or not. If it’s not equal then the condition will be true and then it will return not matched records. Both != and operators aren’t equal operators and then it will return the same result but != operator is not an ISO standard.
Example:
If we run the given below following SQL query for not equal operator then it will return records where Empid does not equal to 12


SQL Not Equal Operator

VIII) SQL Not Less Than (!<) Operator

This SQL example illustrates, not less than operator is used for the purpose of checking 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 then the condition will be true and then it will return matched records.
Example:
If we run the given below following SQL query for not less than operator then it will return records where Empid does not lower than 12


SQL Not Less Than Operator

IX) SQL Not Greater Than (!>) Operator

This SQL example illustrates, not greater than operator is used for the purpose of checking 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 then the condition will be true and then it will return matched records.
Example:
If we run the given below following SQL query for not greater than operator then it will return records where Empid does not higher than 12


SQL Not Greater Than operator

Conclusion:

In this article, you have learned the types of SQL Comparison Operators (Transact-SQL) With Practical Examples, and also learned how to use SQL Comparison Operators to compare two values. I hope you will enjoy it!