Skip to content

Latest commit

 

History

History
130 lines (98 loc) · 3.69 KB

File metadata and controls

130 lines (98 loc) · 3.69 KB

title: "WHERE (Transact-SQL) | Microsoft Docs" ms.custom: "" ms.date: "08/09/2017" ms.prod: sql ms.prod_service: "database-engine, sql-database, sql-data-warehouse, pdw" ms.reviewer: "" ms.suite: "sql" ms.technology: t-sql ms.tgt_pltfrm: "" ms.topic: "language-reference" f1_keywords:

  • "WHERE_TSQL"
  • "WHERE" dev_langs:
  • "TSQL" helpviewer_keywords:
  • "retrieving rows"
  • "clauses [SQL Server], WHERE"
  • "WHERE clause, about WHERE clause"
  • "row retrieval [SQL Server], WHERE clause"
  • "WHERE clause" ms.assetid: a8430421-7bce-4fab-a2d2-56c00a3c6fa4 caps.latest.revision: 37 author: "douglaslMS" ms.author: "douglasl" manager: craigg monikerRange: ">= aps-pdw-2016 || = azuresqldb-current || = azure-sqldw-latest || >= sql-server-2016 || = sqlallproducts-allversions"

WHERE (Transact-SQL)

[!INCLUDEtsql-appliesto-ss2008-all-md]

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

Topic link icon Transact-SQL Syntax Conventions

Syntax

[ WHERE <search_condition> ]  

Arguments

< search_condition > Defines the condition to be met for the rows to be returned. There is 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 (Transact-SQL).

Examples

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

A. Finding a row by using a simple equality

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

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

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

C. Finding rows by using a comparison operator

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

D. Finding rows that meet any of three conditions

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

E. Finding 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. Finding rows that are in a list of values

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

G. Finding rows that have a value between two values

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

See Also

DELETE (Transact-SQL)
Predicates (Transact-SQL)
Search Condition (Transact-SQL)
SELECT (Transact-SQL)
UPDATE (Transact-SQL)
MERGE (Transact-SQL)