OR operator in SQL

Spread the love

OR Operator –

What is the difference between AND and OR operator?

Just like AND Operator, OR operator in SQL helps you filter data based on some conditions but OR operator show results if either conditions separated by OR is True. Unlike AND operator which shows results only if all the conditions are True.

Syntax of OR Operator –

SELECT column1, column2
FROM table_name
WHERE condition1 OR condition2 OR condition3

Employee Table –

Let’s say we want to get all the employee who are from either department 10 or 30.

SELECT *
FROM emp
WHERE DEPTNO = 10 OR DEPTNO = 30;

The above query return all the employee who works in either department 10 or 30. But here if I apply AND then no results will be returned as no employee works in both the departments.


SELECT *
FROM emp
WHERE DEPTNO = 10 AND DEPTNO = 30;

And if you are going to use AND and OR operators both in a SQL query then use parenthesis to make it clear what are you trying to do. IF you don’t then SQL will produce wrong result.

SELECT *
FROM emp
WHERE (DEPTNO = 10 OR DEPTNO = 30) AND SAL >= 1000;

Rating: 1 out of 5.

Leave a Reply