OR operator in SQL
The keyword 'OR' is a reserved keyword in SQL and it can not be used for general-purpose writing. The keyword OR is a SQL logical operator which is used to make a logical decision using SQL query.
It is very simple, let's understand by taking a general life example.
Suppose you are organizing a seminar, in the seminar, there are 100 people from 5 cities (London, Delhi, New York, Tokyo, Paris). Now you want to know how many people are from either London or New York City. You can ask them to raise their hands if they are from the city of London OR New York.
Here you can see you just used OR word to filter the people who are either from London or from New York. In the same way, we use the OR operator in SQL queries to make a logical decision. Let's see how you can use the OR operator in SQL queries
OR Operator Syntex in SQL
select * from tableName where columnName=Value1 OR columnName=Value2 OR columnName=Value3...
Use of OR operator in SQL
Let's suppose you have a table tblEmployee with three columns ( emp_id, emp_name, city_name)
tblEmployee [emp_id, emp_name, city_name]
Your company has 5 offices in cities London, Delhi, New York, Tokyo, and Paris. There are a total of 100 employees across all of your company's offices. Let's find our employees based on their cities.
Q. 1: write a SQL query to get all employees of the company
Ans: select * from tblEmployee
Q. 2: write SQL query to get all employees from Asia
Ans: select * from tblEmployee where city_name='Delhi' OR city_name='Tokyo'
Q.3: write SQL query to get all employees from Delhi, London, and New York
Ans: select * from tblEmployee where city_name='Delhi' OR city_name='London' OR city_name='New York'
Q.4: write SQL query to get all employees from Delhi along with all employees whose name is Adam.
Ans: select * from tblEmployee where city_name='Delhi' OR emp_name='Adam'
Now you can see how you can use the OR operator in real SQL queries, you can use the multiple times OR operators in a single SQL query according to your need.
Important Points About OR Operator in SQL
- In SQL OR operator is case-insensitive, you can also use or, Or, oR keywords there is no issue.
- Not only OR operators, but all operators in SQL are also case-insensitive.
- OR operator can be used multiple times in a single SQL query.