Edit

Not equal to (Transact-SQL) - traditional

Applies to: SQL Server Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW) SQL analytics endpoint in Microsoft Fabric Warehouse in Microsoft Fabric SQL database in Microsoft Fabric

Compares two expressions (a comparison operator). When you compare non-null expressions, the result is TRUE if the left operand isn't equal to the right operand. Otherwise, the result is FALSE. If either or both operands are NULL, see SET ANSI_NULLS.

Transact-SQL syntax conventions

Syntax

expression <> expression

Arguments

expression

Any valid expression. Both expressions must have implicitly convertible data types. The conversion depends on the rules of data type precedence.

Return types

Boolean

Examples

The code samples in this article use the AdventureWorks2025 or AdventureWorksDW2025 sample database, which you can download from the Microsoft SQL Server Samples and Community Projects home page.

A. Use <> in a simple query

The following example returns all rows in the Production.ProductCategory table that don't have a value in ProductCategoryID that equals 3 or 2.

SELECT ProductCategoryID,
       Name
FROM Production.ProductCategory
WHERE ProductCategoryID <> 3
      AND ProductCategoryID <> 2;

Here's the result set.

ProductCategoryID  Name
------------------ ----------------------
1                  Bikes
4                  Accessories