SQL Comparison Operators (Equal, Not Equal, Less than, Grater than)

In sql, comparison operators are used 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 a different type of comparison operators available those are 

 

  • SQL Equal (=) Operator
  • SQL Not Equal (!= or <>) Operator
  • SQL Greater Than (>) Operator
  • SQL Less Than (<) Operator
  • SQL Greater Than or Equal To (>=) Operator
  • SQL Less Than or Equal To (<=) Operator
  • SQL Not Less Than (!<) Operator
  • SQL Not Greater Than (!>) Operator

Before we proceed to check each operator try to create “EmployeeDetails” table by using below script in SQL database

 

create table EmployeeDetails(empid int, empname varchar(50),designation varchar(50),salary int,Location varchar(50))

 

insert into EmployeeDetails

values(1,'suresh','software engineer',25000,'chennai'),

(2,'rohini','AEO',15000,'chennai'),

(3,'madhavsai','business analyst',50000,'nagpur'),

(4,'mahendra','CA',75000,'guntur'),

(5,'sateesh','Doctor',65000,'guntur')

 

select * from EmployeeDetails

Once we run above sql script our table “EmployeeDetails” will create and result will be like as shown below

 

Newly created employeedetails table in sql server

Now we will learn each comparison operator in sql with proper examples

SQL Equal (=) Operator

In sql, equal operator is used to check whether the given two expressions equal or not. If it’s equal, then the condition will be true and it will return matched records.

 

Example:

 

If we run following SQL statement for equal operator it will return records where empid equals to 1.

 

SELECT * FROM EmployeeDetails WHERE empid 1

When we execute above sql equal operator query, we will get the result like as shown below.

 

Output of Equal (=) Operator Example in SQL Server

SQL Not Equal (!=) Operator

In sql, not equal operator is used to check whether two expressions equal or not. If it’s not equal then condition will be true and it will return not matched records.

 

Example:

 

If we run following SQL statement for not equal operator it will return a records where empid not equals to 1.

 

SELECT * FROM EmployeeDetails WHERE empid != 1

When we execute above sql not equal operator query, we will get the result like as shown below.

 

Output of Not Equal(!=) Operator Example in SQL Server

SQL Not Equal (<>) Operator

In sql, not equal operator is used to check whether two expressions equal or not. If it’s not equal then condition will be true and it will return not matched records. Both != and <> operators are not equal operators and will return same result but != operator is not a ISO standard.

 

Example:

 

If we run following SQL statement for not equal operator it will return records where empid not equals to 1

 

SELECT * FROM EmployeeDetails WHERE empid <> 1

When we execute above sql not equal operator query, we will get the result like as shown below.

 

Output of Not Equal(!=) Operator Example in SQL Server

SQL Greater Than (>) Operator

In sql, greater than operator is used to check whether the left hand operator is higher than right hand operator or not. If left hand operator higher than right hand operator then condition will be true and it will return matched records.

 

Example:

 

If we run following SQL statement for greater than operator it will return records where empid greater than 2

 

SELECT * FROM EmployeeDetails WHERE empid 2

When we execute above sql greater than operator query, we will get the result like as shown below.

 

SQL Greater than operator examples output

SQL Less Than (<) Operator

In sql, less than operator is used to check whether left hand operator is lower than right hand operator or not. If left hand operator lower than right hand operator then condition will be true and it will return matched records.

 

Example:

 

If we run following SQL statement for less than operator it will return records where empid less than 2

 

SELECT * FROM EmployeeDetails WHERE empid 2

When we execute above sql less than operator query, we will get the result like as shown below.

 

Output of Less than (<) Operator Example in SQL Server

SQL Greater Than or Equal To (>=) Operator

In sql, greater than or equal to operator is used to check whether left hand operator is higher than or equal to right hand operator or not. If left hand operator higher than or equal to right hand operator then condition will be true and it will return matched records.

 

Example:

 

If we run following SQL statement for greater than or equal to operator it will return records where empid higher than or equal to 2

 

SELECT * FROM EmployeeDetails WHERE empid >= 2

When we execute above sql greater than or equal to operator query, we will get the result like as shown below.

 

SQL Server Greater than or equal to (>=) Operator Example

SQL Less Than or Equal To (<=) Operator

In sql, less than or equal to operator is used to check whether left hand operator is lower than or equal to right hand operator or not. If left hand operator lower than or equal to right hand operator then condition will be true and it will return matched records.

 

Example:

 

If we run following SQL statement for less than or equal to operator it will return records where empid lower than or equal to 2

 

SELECT * FROM EmployeeDetails WHERE empid <= 2

When we execute above sql less than or equal to operator query, we will get the result like as shown below.

 

SQL Server Less than or equal to operator example output

SQL Not Less Than (!<) Operator

In sql, not less than operator is used to check whether left hand operator not lower than right hand operator or not. If left hand operator not lower than right hand operator then condition will be true and it will return matched records.

 

Example:

 

If we run following SQL statement for not less than operator it will return records where empid not lower than 2

 

SELECT * FROM EmployeeDetails WHERE empid !< 2

When we execute above sql not less than operator query, we will get the result like as shown below.

 

SQL Server Not Less than than (!<) Operator Example

SQL Not Greater Than (!>) Operator

In sql, not greater than operator is used to check whether left hand operator is not higher than right hand operator or not. If left hand operator not higher than right hand operator then condition will be true and it will return matched records.

 

Example:

 

If we run following SQL statement for not greater than operator it will return records where empid not higher than 2

 

SELECT * FROM EmployeeDetails WHERE empid !> 2

When we execute above sql not greater than operator query, we will get the result like as shown below.

 

SQL Server Not Greater Than (!>) Operator Example Output

 

For more information related to comparison operators in sql server check following information.

 

OperatorDescription
Equal (=) Operator The sql equal operator is used to check whether two expressions equal or not. If it’s equal then condition will be true and it will return matched records.
Not Equal (!=) Operator The sql not equal operator is used to check whether two expressions equal or not. If it’s not equal then 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 equal or not. If it’s not equal then condition will be true and it will return not matched records.
Greater Than (>) Operator The sql Greater Than operator is used to check whether left hand operator is higher than right hand operator or not. If left hand operator higher than right hand operator then condition will be true and it will return matched records.
Less Than (<) Operator The sql Less Than operator is used to check whether left hand operator is lower than right hand operator or not. If left hand operator lower than right hand operator then condition will be true and it will return matched records.
Greater Than or Equal To (>=) Operator The sql Greater Than or Equal To operator is used to check whether left hand operator is higher than or equal to right hand operator or not. If left hand operator higher than or equal to right hand operator then condition will be true and it will return matched records.
Less Than or Equal To (<=) Operator The sql Less Than or Equal To operator is used to check whether left hand operator is lower than or equal to right hand operator or not. If left hand operator lower than or equal to right hand operator then condition will be true and it will return matched records.
Not Less Than (!<) Operator The sql Not Less Than operator is used to check whether left hand operator not lower than right hand operator or not. If left hand operator not lower than right hand operator then condition will be true and it will return matched records.
Not Greater Than(!>) Operator The sql Not Less Than operator is used to check whether left hand operator not lower than right hand operator or not. If left hand operator not lower than right hand operator then condition will be true and it will return matched records.

Topics Covered