In SQL, the OR operator is useful to compare data with more than one condition and it will return records when either of the condition is TRUE.
Generally, we will use this operator in WHERE clause and the syntax of OR operator in SQL will be like as shown below.
Following is the syntax of defining an OR operator in the SQL server.
SELECT column1, column2 FROM tablename WHERE column1 ='somevalue' OR column2='Somevalue'
We will check this with an example, for that create an “EmployeeDetails” table by using following script in the SQL database.
create table EmployeeDetails(empid int, empname varchar(50),designation varchar(50),salary int,Location varchar(50))
insert into EmployeeDetails
select * from EmployeeDetails
Once we run the above SQL script, the “EmployeeDetails” table will create and result will be like as shown below
Now run the following examples to check OR operator in SQL server.
In the following SQL query, we are checking multiple conditions with the OR operator. It will return records whatever it satisfies either of one condition or both conditions.
SELECT * FROM EmployeeDetails WHERE Location='guntur' OR Salary>40000
When we execute the above OR operator example, we will get the result as shown below.
In following SQL query we are checking multiple conditions (empname, salary) with OR operator. It will return the records whatever it satisfy either of one condition or both conditions.
SELECT * FROM EmployeeDetails WHERE empname='rohini' OR Salary>40000
When we execute above OR operator example, we will get the result as shown below.
This is how we can use OR operator in the SQL server based on requirements.