SQL OR Operator

In SQL, the OR operator is useful for comparing data with more than one condition, and it will return records when either of the conditions is TRUE.

 

Generally, we will use this operator in the WHERE clause, and the syntax of the 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. Create an “EmployeeDetails” table by using the 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 return the below result.

 

Newly created employeedetails table in sql server

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

SQL OR Operator Example1

In the following SQL query, we check multiple conditions with the OR operator. It will return records that satisfy 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 below result.

 

Result of sql sever or operator example

SQL OR Operator Example2

In the following SQL query, we check multiple conditions (empname, salary) with the OR operator. It will return the records that satisfy either one condition or both conditions.

 

SELECT * FROM EmployeeDetails WHERE empname = 'rohini' OR Salary > 40000

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