SQL OR Operator

In sql, OR operator is used to compare a data with more than one condition and it will return a 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 sql server.

 

SELECT column1, column2 FROM tablename WHERE column1 ='somevalue' OR column2='Somevalue'

We will check this with an example for that create a “EmployeeDetails” table by using following script in 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 above sql script our table “EmployeeDetails” will create and result will be like as shown below

 

Newly created employeedetails table in sql server

Now run following examples to check OR operator in sql server.

SQL OR Operator Example1

In following sql query we are checking multiple conditions with OR operator. It will return records whatever it satisfy either of one condition or both conditions.

 

SELECT * FROM EmployeeDetails WHERE Location='guntur' OR Salary>40000

When we execute above OR operator example, we will get the result like 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 like as shown below.

 

sql or operator example with where clause output or result

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

 
 
 

Topics Covered