Note

Access to this page requires authorization. You can try signing in or .

Access to this page requires authorization. You can try .

WHERE (Transact-SQL)

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

Specifies the search condition for the rows returned by the query.

👁 Image
Transact-SQL syntax conventions

Syntax

[ WHERE <search_condition> ]

Arguments

<search_condition>

Defines the condition to be met for the rows to be returned. There's no limit to the number of predicates that can be included in a search condition. For more information about search conditions and predicates, see Search condition.

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.

The following examples show how to use some common search conditions in the WHERE clause.

A. Find a row by using a simple equality

-- Uses AdventureWorksDW
SELECT EmployeeKey, LastName
FROM DimEmployee
WHERE LastName = 'Smith';

B. Find rows that contain a value as part of a string

-- Uses AdventureWorksDW
SELECT EmployeeKey, LastName
FROM DimEmployee
WHERE LastName LIKE '%Smi%';

C. Find rows by using a comparison operator

-- Uses AdventureWorksDW
SELECT EmployeeKey, LastName
FROM DimEmployee
WHERE EmployeeKey <= 500;

D. Find rows that meet any of three conditions

-- Uses AdventureWorksDW
SELECT EmployeeKey, LastName
FROM DimEmployee
WHERE EmployeeKey = 1
 OR EmployeeKey = 8
 OR EmployeeKey = 12;

E. Find rows that must meet several conditions

-- Uses AdventureWorksDW
SELECT EmployeeKey, LastName
FROM DimEmployee
WHERE EmployeeKey <= 500
 AND LastName LIKE '%Smi%'
 AND FirstName LIKE '%A%';

F. Find rows that are in a list of values

-- Uses AdventureWorksDW
SELECT EmployeeKey, LastName
FROM DimEmployee
WHERE LastName IN ('Smith', 'Godfrey', 'Johnson');

G. Find rows that have a value between two values

-- Uses AdventureWorksDW
SELECT EmployeeKey, LastName
FROM DimEmployee
WHERE EmployeeKey BETWEEN 100 AND 200;

Related content


Feedback

Was this page helpful?

Additional resources