Open In App

SQL Server OR Operator

Last Updated : 01 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Logical operators are used for combining multiple boolean expressions which are a combination of results of multiple conditions which are formed by the comparators. Some of the logical operators are AND, OR, EXISTS, IN, LIKE, BETWEEN, etc, Logical operators are very frequently used and are very handy for testing multiple conditions. The logical operator OR is used for combining multiple boolean expressions and it returns true only when any one of the conditions is true. It is used primarily with WHERE, CASE, and HAVING clauses by specifying multiple boolean expressions that return any one of the following values True, or False.

OR Operator Table

A

B

A OR B

True

True

True

True

False

True

False

True

True

Explanation: In this table, we are performing an operation with an OR Operator on two conditions A and B. You will get the False as a result, when both conditions A and B are wrong. You will get True when one of two conditions is True.

Syntax:

SELECT * FROM table_name 
WHERE condition1 OR condition2 OR ....

Explanation: condition1 and condition2 are conditions that return boolean values on evaluation.

The precedence order of the logical operators is as follows NOT > AND > OR where there are no parenthesis.

How to Use the OR Operator Step by Step

For Understanding the OR Operator in SQL Server in more depth manner, we need a table for executing operations or queries. So here we have a CoursesActive table. If you don’t know how to create table in SQL Server then you can refer to this for How to Create a Table in SQL Server

The Result Look Like:

CoursesActiveTable2

CoursesActive Table

Using Single OR operator

SELECT * FROM CoursesActive
WHERE courseId = 1001
OR
courseName = 'Data Structures And Algorithms'

The Result Looks Like:

SingleOrOptr

After Query run Our Table Looks

Explanation: In this query two condition are used where one condition is courseId> 1001 and the other is courseName = ‘Data Structures And Algorithms’ when any of the condition is true there is only one row.

Using Multiple OR Operator

SELECT * FROM CoursesActive
WHERE courseId = 1001
OR courseId =1002
OR courseId = 1003

The Result Looks Like:

MutlipleOROptr

After Query run Our Table Looks

Explanation : In this query two OR operators are used with three condition courseId = 1001, courseId =1002, courseId = 1003 when any row with any of the three conditions is true there are only 3 rows.

Using OR With Other Logical Operator

SELECT * FROM CoursesActive
WHERE courseId = 1001
OR courseId =1002
OR courseId = 1003
OR courseId = 1004
AND courseCost>1500

The Result Looks Like:

ORwithOtherLogical

After Query run Our Table Looks

Explanation : In this query there are combination of there OR and one AND operator with there conditions which are courseId = 1001 or courseId =1002 or courseId = 1003 or courseId = 1004 and courseCost>1500 the AND operator has the higher precedence so that will be evaluated first there is 1 row with courseId = 1004 , courseCost>1500 and remaining three rows with the remaining three conditions.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads