Table of Contents

Introduction

SQL Server OR operator combines two or more search conditions and returns a row when at least one condition evaluates to TRUE. It is useful when a query needs to match alternative values, search across different columns, or apply several possible criteria. Unlike AND, which requires every condition to be true, OR allows any one condition to satisfy the filter.

SQL Server OR Operator Syntax

SELECT column1, column2
FROM table_name
WHERE condition1
   OR condition2
   OR condition3;

Each condition must be a valid predicate. You can combine equality checks, comparison expressions, IN, LIKE, BETWEEN, and other predicates with OR. SQL Server evaluates the complete expression according to its logical operator precedence and parentheses.

Using OR in a WHERE Clause

Assume an Employees table stores employee details, including department and employment status. To find employees who work in either the Sales or Marketing department, use:

SELECT EmployeeID, EmployeeName, Department
FROM dbo.Employees
WHERE Department = 'Sales'
   OR Department = 'Marketing';

The query returns employees from both departments. A row does not need to satisfy both department comparisons; matching either one is sufficient.

You can also use OR to match alternative employment statuses:

SELECT EmployeeID, EmployeeName, EmploymentStatus
FROM dbo.Employees
WHERE EmploymentStatus = 'Active'
   OR EmploymentStatus = 'On Leave';

This query includes employees whose status is Active or On Leave. It excludes rows with any other status unless another condition is added.

Using OR with Comparison Operators

The OR operator can combine numeric and date comparisons. For example, the following query finds products that are inexpensive or have a large stock quantity:

SELECT ProductID, ProductName, UnitPrice, UnitsInStock
FROM dbo.Products
WHERE UnitPrice < 10
   OR UnitsInStock > 100;

A product is included when its price is below 10, its stock is above 100, or both conditions are true. The two conditions do not have to refer to the same type of value.

Here is an example using date conditions:

SELECT OrderID, CustomerID, OrderDate
FROM dbo.Orders
WHERE OrderDate < '2026-01-01'
   OR OrderDate >= '2026-10-01';

This returns orders placed before 2026 or on or after October 1, 2026. When using date filters, choose date literals and data types carefully to avoid implicit conversion and ambiguous date formats.

Using OR with IN

When several alternatives compare the same column to different values, the IN predicate is often shorter and easier to maintain than multiple OR expressions.

SELECT OrderID, OrderStatus
FROM dbo.Orders
WHERE OrderStatus = 'Pending'
   OR OrderStatus = 'Processing'
   OR OrderStatus = 'On Hold';

The same filter can be written using IN:

SELECT OrderID, OrderStatus
FROM dbo.Orders
WHERE OrderStatus IN ('Pending', 'Processing', 'On Hold');

Both forms express alternative values for one column. Prefer IN when the alternatives are a straightforward list. Use OR when conditions involve different columns or different kinds of expressions.

Using OR with LIKE

You can search for multiple text patterns with OR. The following example returns customers whose names begin with either A or M:

SELECT CustomerID, CustomerName
FROM dbo.Customers
WHERE CustomerName LIKE 'A%'
   OR CustomerName LIKE 'M%';

Another example searches more than one city:

SELECT CustomerID, CustomerName, City
FROM dbo.Customers
WHERE City LIKE '%Dhaka%'
   OR City LIKE '%Chattogram%';

Whether text matching is case-sensitive depends on the collation of the column or expression. A leading wildcard such as %Dhaka% can also make an index seek less likely, so consider the search requirements and table size when using contains-style patterns.

Using OR with BETWEEN

The BETWEEN predicate checks whether a value falls within an inclusive range. You can combine multiple ranges with OR when values from either range should be returned.

SELECT EmployeeID, EmployeeName, Salary
FROM dbo.Employees
WHERE Salary BETWEEN 20000 AND 30000
   OR Salary BETWEEN 60000 AND 80000;

This query returns employees whose salaries fall in either range. The endpoints are included in each BETWEEN range.

Combining AND and OR in One Query

SQL Server allows AND and OR in the same condition. However, their precedence matters: AND has higher precedence than OR. SQL Server evaluates the AND expression first unless parentheses change the grouping.

SELECT OrderID, OrderStatus, TotalAmount
FROM dbo.Orders
WHERE OrderStatus = 'Open'
   OR OrderStatus = 'Pending'
  AND TotalAmount > 1000;

SQL Server interprets this expression as:

WHERE OrderStatus = 'Open'
   OR (OrderStatus = 'Pending' AND TotalAmount > 1000);

As a result, all Open orders are returned regardless of amount, while Pending orders are returned only when their total is greater than 1000.

If the requirement is to return Open or Pending orders only when the amount exceeds 1000, use parentheses:

SELECT OrderID, OrderStatus, TotalAmount
FROM dbo.Orders
WHERE (OrderStatus = 'Open' OR OrderStatus = 'Pending')
  AND TotalAmount > 1000;

Parentheses make the intended logic clear to both SQL Server and anyone reading the query. They are especially important in long filters with multiple groups of conditions.

SQL Server OR Truth Table

SQL Server uses three-valued logic. A predicate can evaluate to TRUE, FALSE, or UNKNOWN. The OR truth table is:

Condition A Condition B A OR B
TRUE TRUE TRUE
TRUE FALSE TRUE
TRUE UNKNOWN TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE
FALSE UNKNOWN UNKNOWN
UNKNOWN TRUE TRUE
UNKNOWN FALSE UNKNOWN
UNKNOWN UNKNOWN UNKNOWN

An OR expression is true if at least one operand is true. If neither operand is true and one or both are unknown, the result is unknown. A WHERE clause returns only rows for which its complete predicate is true; false and unknown rows are filtered out.

Using OR with NULL Values

Comparisons with NULL generally produce UNKNOWN, because NULL represents an unknown or missing value. Do not test for NULL with = NULL or <> NULL. Use IS NULL and IS NOT NULL.

SELECT EmployeeID, EmployeeName, ManagerID
FROM dbo.Employees
WHERE Department = 'Sales'
   OR ManagerID IS NULL;

This query returns employees in Sales or employees who do not have a manager assigned. A row can match either condition or both.

For example, this expression does not correctly identify missing manager IDs:

-- Incorrect NULL comparison
WHERE ManagerID = NULL

Use this instead:

WHERE ManagerID IS NULL

Using OR in a HAVING Clause

The HAVING clause filters groups after the GROUP BY operation. You can use OR to keep groups that meet one of several aggregate conditions.

SELECT CustomerID,
       COUNT(*) AS OrderCount,
       SUM(TotalAmount) AS TotalSales
FROM dbo.Orders
GROUP BY CustomerID
HAVING COUNT(*) >= 10
    OR SUM(TotalAmount) > 10000;

This query returns customers who have at least ten orders or total sales greater than 10000. A customer can qualify by order count, sales amount, or both.

Using OR in a JOIN Condition

An OR expression can appear in an ON clause to match rows through alternative relationships. For example, a contact may be associated with a customer through either a primary or an alternate customer identifier:

SELECT c.ContactID, c.ContactName, cu.CustomerName
FROM dbo.Contacts AS c
INNER JOIN dbo.Customers AS cu
  ON c.PrimaryCustomerID = cu.CustomerID
  OR c.AlternateCustomerID = cu.CustomerID;

This joins a contact when either identifier matches a customer. Be careful: if both identifiers match the same or different customer rows, the join can produce multiple result rows. Check whether that behavior is intended and consider using separate joins or a normalized relationship table for complex matching rules.

OR with NOT and Other Predicates

You can combine OR with NOT to express alternative exclusion rules. For example:

SELECT ProductID, ProductName, IsDiscontinued
FROM dbo.Products
WHERE NOT (IsDiscontinued = 1)
   OR UnitsInStock > 0;

This returns products that are not discontinued or that still have stock. Because the first condition can include products regardless of stock, make sure this matches the actual business requirement. For more readable logic, use explicit predicates and parentheses.

OR and NOT IN with NULL

Be careful when combining OR with NOT IN. If the list or subquery used by NOT IN contains a NULL value, the comparison can evaluate to UNKNOWN and unexpectedly return no rows. When checking that no matching related row exists, NOT EXISTS is often a safer option.

SELECT c.CustomerID, c.CustomerName
FROM dbo.Customers AS c
WHERE c.City = 'Dhaka'
   OR NOT EXISTS (
       SELECT 1
       FROM dbo.Orders AS o
       WHERE o.CustomerID = c.CustomerID
   );

This example returns customers located in Dhaka or customers who have no matching order. The subquery is correlated to the current customer row.