Summary -
In this topic, we described about the OR Condition with detailed example.
OR Condition is used to specify multiple conditions in WHERE clause to fetch matched rows or values which satisfy any one of the condition from specified conditions. OR Condition used in SELECT, UPDATE, DELETE statements.
SELECT Syntax -
SELECT column1, column2, …, columnN
FROM table_name
[WHERE condition1 OR condition2 OR … OR conditionN];
UPDATE Syntax –
UPDATE table_name
SET column1 = value1, column2 = value2, …, columnN = valueN
[WHERE condition1 OR condition2 OR … OR conditionN];
DELETE Syntax –
DELETE FROM table_name
[WHERE condition1 OR condition2 OR … OR conditionN];
- column1, column2, …, columnN – Specifies the column names.
- condition1, condition2, …, conditionN – Specifies the conditions that are used to filter the rows.
- table_name – Specifies the name of the table.
Example -
Let us consider the employee_details table as follows –
employee_details -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
Scenario – Fetch rows using OR condition.
Requirement1 – Fetch all details of employees whose manager is "Employee1" or employee id is 006. The query was as follows –
SELECT * FROM employee_details
WHERE manager_id = "Employee1" OR emp_id = 006;
By executing above, we can get results as shown below –
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
Requirement2 – Fetch all details whose department is 2000 or salary is 25000. The query was as follows –
SELECT * FROM employee_details WHERE dept_id = 2000 OR NOT salary=25000;
By executing above, we can get results as shown below –
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 14000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
Requirement3– Fetch all employee details whose emp_id is 005 or designation is "Manager" and dept is 2000. The query was as follows –
SELECT * FROM employee_details WHERE emp_id = 005
OR (designation = "Manager" AND dept_id = 2000);
By executing above, we can get results as shown below –
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
Scenario – Update the rows using OR condition.
Requirement - Update the employee salary whose employee id is 008 or designation is "Clerk". The query was as follows –
UPDATE employee_details set salary =20000
WHERE emp_id = 008 OR designation = "Clerk";
By executing above query, we can update the salary to 20000 for the employees whose emp_id is 008 or designation is "Clerk". The table rows after the update is was as follows-
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 20000.00 | 1000 |
008 | Employee8 | Salesman | Employee4 | 2019-09-09 | 20000.00 | 2000 |
009 | Employee9 | Salesman | Employee4 | 2019-10-08 | 13000.00 | 2000 |
Scenario – Deleting a row using OR condition.
Requirement – Delete employee details whose designation is "Salesman" or emp_id is 009. The query was as follows –
DELETE FROM Employee details WHERE designation = "Salesman" OR emp_id = 009;
By executing above query, we can delete employee details whose designation is "Salesman" or emp_id is 009. The output was as follows -
emp_id | emp_name | designation | manager_id | date_of_hire | salary | dept_id |
---|---|---|---|---|---|---|
001 | Employee1 | Director | 2019-07-11 | 45000.00 | 1000 | |
002 | Employee2 | Director | 2019-07-11 | 40000.00 | 2000 | |
003 | Employee3 | Manager | Employee1 | 2019-07-11 | 27000.00 | 1000 |
004 | Employee4 | Manager | Employee2 | 2019-10-08 | 25000.00 | 2000 |
005 | Employee5 | Analyst | Employee3 | 2019-07-11 | 20000.00 | 1000 |
006 | Employee6 | Analyst | Employee3 | 2019-10-08 | 18000.00 | 1000 |
007 | Employee7 | Clerk | Employee3 | 2019-07-11 | 15000.00 | 1000 |