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

  Total Views : 558
  Comments : 0
 
  Keywords : What are the comparison operators in sql server, Why we need to use comparison operators in sql server, Sql comparison operators equal, not equal, less than, greater than, not less than, etc

Here we will learn what are the comparison operators in sql server and uses of comparison operators in sql server and different type of comparison operators like equal (=), greater than (>), less than (*), greater than or equal to (>=), less than or equal to (<=), not equal (<>), etc… in sql server with examples

SQL Server Comparison Operators

The 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 comparison operators we have different type of operators 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

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.

 

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

Output

 

Following is the output of sql equal operator example

 

Output of Equal (=) Operator Example in SQL Server

SQL 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.

 

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

Output

 

Following is the output of not equal (!=) operator in sql server

 

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

SQL 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. 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

Output

 

Following is the output of not equal (<>) operator in sql server

 

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

SQL 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.

 

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

Output

 

Following is the output of greater than (>) operator in sql server

 

SQL Greater than operator examples output

SQL 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.

 

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

Output

 

Following is the output of less than (<) operator in sql server

 

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

SQL 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.

 

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

Output

 

Following is the output of greater than or equal to (>=) operator in sql server

 

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

SQL 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.

 

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

Output

 

Following is the output of less than or equal to (<=) operator in sql server

 

SQL Server Less than or equal to operator example output

SQL 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.

 

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

Output

 

Following is the output of not less than (!<) operator in sql server

 

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

SQL Not Greater Than (!>) Operator

The 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

Output

 

Following is the output of not greater than (!>) operator in sql server

 

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

For more information related to comparison operators in sql server check below information

 

Operator

Description

SQL 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.

 

 

SQL 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.

SQL 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.

 

 

SQL 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

 

 

SQL 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.

SQL 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.

SQL 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.

SQL 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.

SQL 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.

 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User