SQL OR Condition in SQL Queries
SQL OR Condition in SQL Queries
The SQL OR condition is a powerful operator used in SQL queries to fetch records when at least one of the specified conditions is met. It can be used in various SQL statements, such as SELECT, INSERT, UPDATE, and DELETE. This operator is particularly useful when filtering data based on multiple criteria.
Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here
SQL OR Syntax
SELECT columns FROM table_name WHERE condition_1 OR condition_2;
Understanding SQL OR with Examples
To better understand the SQL OR condition, consider the following emp table:
ID | First_Name | Last_Name | Department | Location |
---|---|---|---|---|
1 | Rajesh | Sharma | HR | Delhi |
2 | Pooja | Verma | IT | Mumbai |
3 | Arjun | Thakur | IT | Kolkata |
4 | Nisha | Gupta | IT | Kolkata |
5 | Vikram | Singh | Marketing | Pune |
6 | Simran | Mehta | Finance | Bangalore |
7 | Anjali | Kapoor | Finance | Bangalore |
SQL OR Condition with SELECT Statement
Example 1: Fetch Employees from IT Department or Located in Kolkata
SELECT * FROM emp WHERE Department = "IT" OR Location = "Kolkata";
Result:
ID | First_Name | Last_Name | Department | Location |
---|---|---|---|---|
2 | Pooja | Verma | IT | Mumbai |
3 | Arjun | Thakur | IT | Kolkata |
4 | Nisha | Gupta | IT | Kolkata |
Explanation: The query retrieves all records where the Department is IT or Location is Kolkata.
Example 2: Fetch Employees from Marketing Department or Located in Noida
SELECT * FROM emp WHERE Department = "Marketing" OR Location = "Noida";
Result:
ID | First_Name | Last_Name | Department | Location |
---|---|---|---|---|
5 | Vikram | Singh | Marketing | Pune |
7 | Anjali | Kapoor | Finance | Bangalore |
Explanation: The query retrieves all employees who belong to the Marketing department or are located in Noida.
SQL OR Condition with UPDATE Statement
Example 1: Change the location of marketing employees whose last name is “Thakur” to “Chennai.”
UPDATE emp SET Location = "Chennai" WHERE Department = "Marketing" OR Last_Name = "Thakur";
Updated Table:
ID | First_Name | Last_Name | Department | Location |
---|---|---|---|---|
1 | Rajesh | Sharma | HR | Delhi |
2 | Pooja | Verma | IT | Mumbai |
3 | Arjun | Thakur | IT | Chennai |
4 | Nisha | Gupta | IT | Kolkata |
5 | Vikram | Singh | Marketing | Chennai |
6 | Simran | Mehta | Finance | Bangalore |
7 | Anjali | Kapoor | Finance | Bangalore |
Explanation: Since the condition is based on an OR operator, employees either in Marketing or with the last name ‘Thakur’ got their location updated to Chennai.
Example 2: Change the Department to ‘HR’ for Finance Employees whose First Name is ‘Anjali’
UPDATE emp SET Department = "HR" WHERE Department = "Finance" OR First_Name = "Anjali";
Updated Table:
ID | First_Name | Last_Name | Department | Location |
---|---|---|---|---|
1 | Rajesh | Sharma | HR | Delhi |
2 | Pooja | Verma | IT | Mumbai |
3 | Arjun | Thakur | IT | Chennai |
4 | Nisha | Gupta | IT | Kolkata |
5 | Vikram | Singh | Marketing | Chennai |
6 | Simran | Mehta | HR | Bangalore |
7 | Anjali | Kapoor | HR | Bangalore |
Explanation: Employees in Finance or those whose first name is Anjali had their department changed to HR.
SQL OR Condition with DELETE Statement
Example 1: Delete Employees whose Last Name is ‘Kapoor’ or Located in Bangalore
DELETE FROM emp WHERE Last_Name = "Kapoor" OR Location = "Bangalore";
Updated Table:
ID | First_Name | Last_Name | Department | Location |
---|---|---|---|---|
1 | Rajesh | Sharma | HR | Delhi |
2 | Pooja | Verma | IT | Mumbai |
3 | Arjun | Thakur | IT | Chennai |
4 | Nisha | Gupta | IT | Kolkata |
5 | Vikram | Singh | Marketing | Chennai |
Explanation: Since Kapoor and employees in Bangalore meet the OR condition, those records were deleted.
Example 2: Delete Employees from Marketing Department or Located in Chennai
DELETE FROM emp WHERE Department = "Marketing" OR Location = "Chennai";
Final Updated Table:
ID | First_Name | Last_Name | Department | Location |
---|---|---|---|---|
1 | Rajesh | Sharma | HR | Delhi |
2 | Pooja | Verma | IT | Mumbai |
4 | Nisha | Gupta | IT | Kolkata |
Explanation: Since employees from Marketing and those located in Chennai met the OR condition, they were deleted.
Download New Real Time Projects :-Click here
Complete Advance AI topics:-Â CLICK HERE
Conclusion
The SQL OR condition provides flexibility when filtering records in SQL queries. Whether using SELECT, UPDATE, or DELETE statements, the OR operator ensures that data is retrieved, modified, or removed when at least one condition is met. This makes it a powerful tool in database management and decision-making processes.
Stay tuned to UpdateGadh for more SQL tutorials and database management tips!
sql where multiple conditions
multiple if condition in sql select query
sql case when multiple conditions
having clause in sql
sql if statement
joins in sql
case statement in sql
sql conditional select
sql condition in sql queries w3schools
sql condition in sql queries oracle
SQL OR Condition in SQL Queries
Post Comment