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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
create table EmployeeDetails ( Empid int, Empname varchar(50), Designation varchar(50), Department varchar(30), Salary int, Location varchar(50) ) insert into EmployeeDetails values(11,'Ramesh Tiwari','software engineer','IT',25000,'chennai'), (12,'Chadani Mishra','AEO','Management',15000,'chennai'), (13,'Narendra Gupta','business analyst','Operation',50000,'nagpur'), (14,'Priya Singh','CA','Account',75000,'guntur'), (15,'Mahendra Narayan','Doctor','Medical',65000,'guntur') select * from EmployeeDetails |
When we run the above SQL script our table “EmployeeDetails” will create and the result will be like as shown below:-
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.
1 2 |
select * from EmployeeDetails SELECT * FROM EmployeeDetails WHERE empid = 13 |
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
1 |
SELECT * FROM EmployeeDetails WHERE empid != 12 |
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
1 |
SELECT * FROM EmployeeDetails WHERE empid > 12 |
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
1 |
SELECT * FROM EmployeeDetails WHERE empid < 12 |
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
1 |
SELECT * FROM EmployeeDetails WHERE empid >= 12 |
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
1 |
SELECT * FROM EmployeeDetails WHERE empid <= 12 |
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
1 |
SELECT * FROM EmployeeDetails WHERE empid <> 12 |
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
1 |
SELECT * FROM EmployeeDetails WHERE empid !< 12 |
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
1 |
SELECT * FROM EmployeeDetails WHERE empid !> 12 |
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!