OR operator combines
two conditions. When more than one logical operator is used in a statement, OR
operators are evaluated after AND operators. You can also change the order of
evaluation by using parentheses.
Syntax of OR Operator :
boolean_expression OR boolean_expression
boolean_expression
is valid
expression in sql server that returns TRUE, FALSE, or UNKNOWN.
Return type of or operator is boolean. OR returns TRUE when either of the
conditions is TRUE.
Below table
shows the result of the OR operator.
| TRUE | FALSE | UNKNOWN |
TRUE | TRUE | TRUE | TRUE |
FALSE | TRUE | FALSE | UNKNOWN |
UNKNOWN | TRUE | UNKNOWN | UNKNOWN |
Examples of OR operator :
Example 1 :
Using OR operator in where clause
SELECT ProductName, UnitPrice
FROM Products
WHERE UnitPrice = 10
OR UnitPrice = 15 OR UnitPrice = 20
Output
ProductName UnitPrice
Aniseed
Syrup 10.00
Sir Rodney's
Scones 10.00
Maxilaku 20.00
Outback
Lager 15.00
Röd Kaviar 15.00
Longlife
Tofu 10.00
Above
example displays product having unit price 10 or 15 or 20. You can achieve same
result set using IN operator. To view this example, please visit below link.
IN Operator
Example 2 : Use of Or operator in if statement
DECLARE @Number INT
SET @Number = 10
IF (@Number = 10 OR @Number > 10)
BEGIN
PRINT 'Number is greater than or equal to 10.'
END
ELSE
BEGIN
PRINT 'Number is less than 10.'
END
Output
Number is greater than or equal to 10.
Above example displays the use of or opertor to combine 2 expressions in if statement. Where variable number is compared with value 10. From 2 Condition of if statement, @Number = 10 condition returns TRUE and @Number > 10 condition returns FALSE. So it prints statement in if condition.