SQL Server > Query

Not equals to in query in SQL Server

How to use not equal to condition in the query of SQL Server?


There are two variance of using not equal to. First is in case of checking for null value and other for any data type values. To check for null value, we use “not” keyword and for other datatype we use ‘<>’.

NOTE: ‘<>’ and ‘!=’ gives equal meaning in SQL Server and can be used interchangeably however ‘<>’ is recommended to use as it is of ANSI SQL standard.

SELECT * FROM PersonalDetails
WHERE LastName is not null

SELECT * FROM PersonalDetails
WHERE Age <> 30

SELECT * FROm PersonalDetails
WHERE FirstName <> 'Shreeharsh'

The 1st SELECT statement retrieves all records from the table whose LastName is not null.

The 2nd SELECT statement retrieves all records from the table whose Age is not 30.

The 3rd SELECT statement retrieves all records from the table whose FirstName is not equal to ‘Shreeharsh’

 Views: 1757 | Post Order: 50



Write for us