OR (Transact-SQL)

 

THIS TOPIC APPLIES TO: yesSQL Server (starting with 2008)yesAzure SQL DatabaseyesAzure SQL Data Warehouse yesParallel Data Warehouse

Combines two conditions. When more than one logical operator is used in a statement, OR operators are evaluated after AND operators. However, you can change the order of evaluation by using parentheses.

Topic link icon Transact-SQL Syntax Conventions

-- Syntax for SQL Server, Azure SQL Database, Azure SQL Data Warehouse, Parallel Data Warehouse  
  
boolean_expression OR boolean_expression  

boolean_expression
Is any valid expression that returns TRUE, FALSE, or UNKNOWN.

Boolean

OR returns TRUE when either of the conditions is TRUE.

The following table shows the result of the OR operator.

TRUEFALSEUNKNOWN
TRUETRUETRUETRUE
FALSETRUEFALSEUNKNOWN
UNKNOWNTRUEUNKNOWNUNKNOWN

The following example uses the vEmployeeDepartmentHistory view to retrieve the names of Quality Assurance personnel who work either the evening shift or the night shift. If the parentheses are omitted, the query returns Quality Assurance employees who work the evening shift and all employees who work the night shift.

-- Uses AdventureWorks  
  
SELECT FirstName, LastName, Shift   
FROM HumanResources.vEmployeeDepartmentHistory  
WHERE Department = 'Quality Assurance'  
   AND (Shift = 'Evening' OR Shift = 'Night');  

Here is the result set.

FirstName LastName Shift

------------ ---------------- -------

Andreas Berglund Evening

Sootha Charncherngkha Night

The following example retrieves the names of employees who either earn a BaseRate less than 20 or have a HireDate January 1, 2001 or later.

-- Uses AdventureWorks  
  
SELECT FirstName, LastName, BaseRate, HireDate   
FROM DimEmployee  
WHERE BaseRate < 10 OR HireDate >= '2001-01-01';  

Expressions (Transact-SQL)
Built-in Functions (Transact-SQL)
Operators (Transact-SQL)
SELECT (Transact-SQL)
WHERE (Transact-SQL)

Community Additions

ADD
Show: