Introduction to SQL Server OR operator

The SQL Server OR is a logical operator that allows you to combine two Boolean expressions. It returns TRUE when either of the conditions evaluates to TRUE.
The following shows the syntax of the OR operator:
In this syntax, the boolean_expression is any valid Boolean expression that evaluates to true, false, and unknown.
The following table shows the result of the OR operator when you combine TRUEFALSE, and UNKNOWN:
When you use more than one logical operator in a statement, SQL Server evaluates the OR operators after the AND operator. However, you can use the parentheses to change the order of evaluation.

SQL Server OR operator examples

See the following production.roducts table from the sample database.

A) Using OR operator example

The following example finds the products whose list price is less than 200 or greater than 6,000:
SQL Server OR example

B) Using multiple OR operators example

The following statement finds the products whose brand id is 1, 2, or 4:
You can replace multiple OR operators by the IN operator as shown in the following query:

C) Using OR operator with AND operator example

Consider the following example:
SQL Server OR with AND operator
In this example, we used both OR and AND operators. As always, SQL Server evaluated the AND operator first. Therefore, the query returned the products whose brand id is 2 and the list price is greater than 500 or the products whose brand id is 1.
To find the products whose brand id is 1 or 2 and list price is greater than 500, you use the parentheses as shown in the following query:
In this tutorial, you have learned how to use the SQL Server OR operator to form a condition by combining two Boolean expressions.

Post a Comment