SQL Server Comparison Operator

In SQL Server, the comparison operators are used to test for equality and inequality. These operators are used in the WHERE clause to determine which records to select.

Following is a list of the SQL Server comparison operators:

IndexComparison OperatorDescription
1)=It specifies equal symbol.
2)<>It specifies not equal symbol.
3)!=It specifies not equal symbol.
4)>It specifies greater than symbol.
5)>=It specifies greater than or equal symbol.
6)<It specifies less than symbol.
7)<=It specifies less than or equal symbol.
8)!>It specifies not greater than symbol.
9)!<It specifies not less than symbol.
10)IN ( )It matches a value in a list.
11)NOTIt is used to negate a condition.
12)BETWEENIt is used to specify within a range (inclusive) value.
13)IS NULLIt specifies null value.
14)IS NOT NULLIt specifies non-null value.
15)LIKEIt specifies pattern matching with % and _
16)EXISTSIt specifies that the condition is met if subquery returns at least one row.

Equality Operator

In SQL Server database, Equality Operator “=” is used to test for equality in a query.

Example:

We have a table named “Employees”, having the following data:

SQL Comparison operators 1

Use the following query to select the specific data where “name” = “Lily”:

SELECT *  

FROM [javatpoint].[dbo].[Employees]  

WHERE name = 'Lily';

Output:

SQL Comparison operators 2

Inequality Operator

In SQL Server, inequality operators “<> or !=” are used to test for inequality in a query.

SELECT *  

FROM [javatpoint].[dbo].[Employees]  

WHERE name <> 'Lily';

Output:

SQL Comparison operators 3

OR

SELECT *  

FROM [javatpoint].[dbo].[Employees]  

WHERE name != 'Lily';

Output:

SQL Comparison operators 4

Greater Than Operator

Greater Than “>” Operator is used to test for an expression that it is “greater than”.

Example:

Let’s select employees from the table “Employees” where salary > 15000.

SELECT *  

FROM [javatpoint].[dbo].[Employees]  

WHERE salary > 15000;

Output:

SQL Comparison operators 5

Greater Than or Equal Operator

Greater Than or Equal “>=” Operator is used to test for an expression that it is “greater than or equal to”.

SELECT *  

FROM [javatpoint].[dbo].[Employees]  

WHERE salary >= 15000;

Output:

SQL Comparison operators 6

Less than Operator

Less Than “<” Operator is used to test for an expression that it is “less than” the other one.

Example:

Select all employees from the table “Employees” where salary is < 20000.

SELECT *  

FROM [javatpoint].[dbo].[Employees]  

WHERE salary < 20000;

Output:

SQL Comparison operators 7

Less Than or Equal Operator

Less Than or Equal “<=” Operator is used to test for an expression that it is “less than or equal to” the other one.

SELECT *  

FROM [javatpoint].[dbo].[Employees]  

WHERE salary <= 20000;

Output:

SQL Comparison operators 8

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *