Last Updated: 02 July, 2023
In MySQL, the OR condition is used to combine multiple conditions in a query, and it returns true if any of the individual conditions are true. The OR operator allows us to create more flexible and inclusive queries by considering multiple criteria.
The syntax for using the OR condition in a MySQL query is as follows:
SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR condition3 ...;
For example, if we have a table named employees and we want to retrieve all the employees who are either from the "Sales" department or have a salary greater than 50000, we can use the OR condition as follows:
SELECT emp_id, emp_name, department, salary FROM employees WHERE department = 'Sales' OR salary > 50000;
This query will return all the employees who meet either of the two conditions: they belong to the "Sales" department or their salary is greater than 50000.
We can also combine the OR condition with other conditions using parentheses to control the logical grouping.
For example:
SELECT employee_id, first_name, last_name, department FROM employees WHERE (department = 'Sales' OR department = 'Marketing') AND salary > 50000;
In the above query, we use parentheses to group the OR conditions and combine them with the AND condition. This query will retrieve records where the department is either "Sales" or "Marketing" and the salary is greater than 50000.
That's all, guys. I hope this MySQL article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com