SQL OR Operator

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.

Syntax of SQL OR Operator

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

values(1,'suresh','software engineer',25000,'chennai'),

(2,'rohini','AEO',15000,'chennai'),

(3,'madhavsai','business analyst',50000,'nagpur'),

(4,'mahendra','CA',75000,'guntur'),

(5,'sateesh','Doctor',65000,'guntur')

 

select * from EmployeeDetails

Once we run the above SQL script, the “EmployeeDetails” table will create and result will be like as shown below

 

Newly created employeedetails table in sql server

Now run the following examples to check OR operator in SQL server.

SQL OR Operator Example1

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.

 

Result of sql sever or operator example

SQL OR Operator Example2

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.

 

sql or operator example with where clause output or result

This is how we can use OR operator in the SQL server based on requirements.