**in details with multiple examples.**

*SQL Comparison operators*Table of Contents

- 1 SQL Comparison Operators
- 1.1 Equal (=) Operator in SQL
- 1.2 Not Equal (!=) Operator in SQL
- 1.3 SQL Not Equal (<>) Operators
- 1.4 Greater Than (>) Operator in SQL
- 1.5 Less Than (<) Operator in SQL
- 1.6 Greater Than or Equal To (>=) Operator in SQL
- 1.7 Less Than or Equal To (<=) Operator in SQL
- 1.8 SQL SERVER Not Less Than (!<) Operator
- 1.9 Not Greater Than (!>) Operator in SQL

- 2 Conclusion:

**SQL Comparison Operators**

The **SQL server comparison operators** are useful to compare one expression with another expression using mathematical operators.

SQL Comparison operators return **true** or **false** based on the comparison.

The following are the list of different type of comparison operators available in SQL Server.

Consider X is the variable and the value assigned to X = 5 ;

SQL Operators | Description | Example |
---|---|---|

< | Less Than (<) Operator | X < 10 (returns true) |

> | Greater Than (>) Operator | X > 10 (returns false) |

<= | Less Than or Equal To (<=) Operator | X <= 10 (returns true) |

>= | Greater Than or Equal To (>=) Operator | X >=5 (returns true) |

= | Equal (=) Operator | X = 5 (returns true) |

!= | Not Equal (!=) Operator | X != 5 (returns false) |

<> | Not Equal (<>) Operator | X <>5 (return false) |

The following are examples of the above SQL comparison operators.

```
DECLARE @X INT;
SET @X=5;
-- Less Than (<) Operator
SELECT CASE WHEN @X < 10 THEN 'TRUE' ELSE 'FALSE' END AS [Result];
-- returns TRUE
-- Greater Than (>) Operator
SELECT CASE WHEN @X > 10 THEN 'TRUE' ELSE 'FALSE' END AS [Result];
-- returns FALSE
-- Less Than or Equal To (<=) Operator
SELECT CASE WHEN @X <= 10 THEN 'TRUE' ELSE 'FALSE' END AS [Result];
-- returns TRUE
-- Greater Than or Equal To (>=) Operator
SELECT CASE WHEN @X >= 5 THEN 'TRUE' ELSE 'FALSE' END AS [Result];
-- returns TRUE
-- Equal (=) Operator
SELECT CASE WHEN @X = 5 THEN 'TRUE' ELSE 'FALSE' END AS [Result];
-- returns TRUE
-- Not Equal (!=) Operator
SELECT CASE WHEN @X != 5 THEN 'TRUE' ELSE 'FALSE' END AS [Result];
-- returns FALSE
-- Not Equal (!=) Operator
SELECT CASE WHEN @X <>5 THEN 'TRUE' ELSE 'FALSE' END AS [Result];
-- returns FALSE
```

Let’s try to understand each operator by creating a new “Employee” table by using the below SQL script in the database.

```
-- Create a new Employee table
CREATE TABLE Employee(EmpId int, EmpName varchar(100),Designation varchar(50),Location varchar(50))
-- Insert following records in the Employee table
INSERT INTO Employee values(1,'Shekh Ali','Software Engineer','Ballia'),
(2,'Rajat','Team Lead','Mumbai'),
(3,'Julie folden','Business Analyst','Poland'),
(4,'Amit Kumar','CA','Noida'),
(5,'Mark Adam','Sr. Software Engineer','America')
-- Select all the records from the Employee table
SELECT * FROM Employee;
```

**Equal (=) Operator in SQL**

In SQL Server, the equal **(=)** operator is used to checking whether the two expressions are equal or not. If it’s equal, then the condition will be true and it will return the matched records from the respective table.

#### Example of the equal operator in SQL

```
SELECT * FROM Employee WHERE EmpId = 1;
```

Once we execute the above SQL statement for the equal operator, it will return records where ** EmpId is equal to 1**.

**Not Equal (!=) Operator in SQL**

In SQL, the **not equal (!=) operator** is used to check whether the two expressions are equal or not. If it’s not equal, then the condition becomes true and it will return the not-matched records. This is just the opposite of the equal operator.

#### Example of the not equal Operator in SQL

```
SELECT * FROM Employee WHERE EmpId != 1;
```

Once we execute the above SQL statement for the not equal **(!=)** operator, it will return records where ** EmpId is not equal to 1**.

**SQL Not Equal (<>) Operators**

In the SQL server, The **not equal (<>) operator** is used to check whether the two expressions are equal or not. If it’s not equal, then the condition becomes true and it will return the not-matched records. Here the both **!=** and **<>** operators are not equal operators and will return the same result but **!=** operator is not an ISO standard.

#### Example of not equal operator

```
SELECT * FROM Employee WHERE EmpId <> 1;
```

Once we execute the above SQL statement for the not equal **(<>)** operator, it will return records where ** EmpId is not equal to 1**.

**Greater Than (>) Operator in SQL**

In SQL Server, The 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, then the condition becomes true and it will return the matched records.

#### Example of greater than operator

```
SELECT * FROM Employee WHERE EmpId > 3;
```

Once we execute the above SQL statement for the greater than (>) operator, it will return records where *EmpId is greater than 3*

**Less Than (<) Operator in SQL**

In SQL Server, The 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 then the condition becomes true and it will return the matched records.

#### Example of Less Than Operator in SQL

```
SELECT * FROM Employee WHERE EmpId < 2;
```

Once we execute the above SQL statement for the less than (<) operator, it will return records where ** EmpId is less than 2**.

**Greater Than or Equal To (>=) Operator in SQL**

In SQL server, The **Greater than or Equal To (>=) 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 then the condition becomes true and it will return the matched records.

#### Example of Less Than Operators in SQL

```
SELECT * FROM Employee WHERE EmpId >= 3;
```

Once we execute the above SQL command for the greater than or equal (>=) operator, it will return records where ** EmpId is greater than or equal to 3**.

**Less Than or Equal To (<=) Operator in SQL**

In SQL server, The **less than or equal to (<=) operator** is used to check whether the left-hand operator is lower/less 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 becomes true and it will return the matched records.

#### Example of less than or equal to (<=) operator in SQL

```
SELECT * FROM Employee WHERE EmpId <= 3;
```

Once we execute the above SQL statement for the** less than or equal to (<=) operator**, it will return records where ** EmpIds are less than or equal to 3**.

**SQL SERVER Not Less Than (!<) Operator**

In SQL server, The not less than **(!<)** operator is used to check whether the left-hand operator is not less than the right-hand operator or not. If the left-hand operator is not less than the right-hand operator then the condition becomes true and it will return the matched records.

#### Example of not less than (!<) operator in SQL server

SELECT * FROM Employee WHERE EmpId !< 3;

Once we execute the above SQL statement for **not less than (!<) operator**, it will return records where ** EmpIds are not less than 3**.

**Not Greater Than (!>) Operator in SQL**

In SQL Server, The **Not Greater Than (!>) operator** is used to check whether the left-hand operator is not greater than the right-hand operator or not. If the left-hand operator is not greater than the right-hand operator then the condition becomes true and it will return the matched records.

#### Example of not greater than (!>) operator in SQL server

```
SELECT * FROM Employee WHERE EmpId !> 3;
```

Once we execute the above SQL statement for the not greater than (!>) operator, it will return records where EmpIds** are not greater than 3**.

**Conclusion:**

Here, In this article, we learned about **SQL Comparison Operators*** *with multiple examples. I hope you enjoyed this post and found it useful. In case you have any doubts, please post your feedback, questions, or comments below.

References: MSDN- Comparison Operators (Transact-SQL)

**Recommended Articles**

- Stored Procedure in SQL Server With Examples
- SQL Delete Cascade – SQL query to delete parent-child referenced rows
- CREATE, ALTER, AND DROP DATABASE IN SQL SERVER
- Primary Key Constraint In SQL Server
- View In SQL | Types Of Views In SQL Server
- SQL Server Trigger Update, Insert, Delete Examples

- C# Program to Convert Binary to Decimal with Examples - December 4, 2023
- C# Program to Print Multiplication Table of a Given Number - November 30, 2023
- Palindrome program in C# with examples - November 28, 2023