MySQL OR Operator

MySQL OR Operator

MySQL OR Operator

The OR operator in MySQL is used to combine multiple conditions in a WHERE clause. It returns TRUE if any of the specified conditions are TRUE.

Syntax

SELECT column1, column2, ... FROM table_name WHERE condition1 OR condition2 OR ...;

1. Using OR with Multiple Conditions

Example

Retrieve employees who work in the "IT" department or earn a salary greater than 70,000.

SELECT id, name, department, salary FROM employees WHERE department = 'IT' OR salary > 70000;

Result

+----+-------+------------+--------+ | id | name | department | salary | +----+-------+------------+--------+ | 1 | Alice | IT | 70000 | | 2 | Bob | IT | 80000 | | 3 | Eve | HR | 90000 | +----+-------+------------+--------+

2. Combining OR with Other Operators

The OR operator can be used with comparison operators such as =, >, <, >=, <=, and <> (not equal).

Example

Get products priced below 100 or above 1,000:

SELECT product_id, product_name, price FROM products WHERE price < 100 OR price > 1000;

3. Using OR with Logical Operators

You can combine OR with AND or NOT for more complex conditions.

Example

Find customers from "New York" who have made purchases below 500 or are not VIP members:

SELECT customer_id, name, city, purchase_amount FROM customers WHERE city = 'New York' AND (purchase_amount < 500 OR is_vip = 0);

4. Practical Example

Sample Table: orders

+----+------------+-----------+--------+ | id | customer | product | amount | +----+------------+-----------+--------+ | 1 | Alice | Laptop | 1500 | | 2 | Bob | Monitor | 300 | | 3 | Charlie | Smartphone| 800 | | 4 | Alice | Tablet | 400 | +----+------------+-----------+--------+

Query: Get orders where the amount is greater than 1000 or the product is "Tablet":

SELECT id, customer, product, amount FROM orders WHERE amount > 1000 OR product = 'Tablet';

Result:

+----+----------+-----------+-------+ | id | customer | product | amount| +----+----------+-----------+-------+ | 1 | Alice | Laptop | 1500 | | 4 | Alice | Tablet | 400 | +----+----------+-----------+-------+

5. Using OR with Multiple Columns

You can use OR to evaluate conditions across multiple columns.

Example

Find employees who have "Manager" in their title or are in the "HR" department:

SELECT id, name, title, department FROM employees WHERE title LIKE '%Manager%' OR department = 'HR';

6. Common Errors

  1. Operator Precedence: MySQL evaluates AND before OR. Use parentheses to specify precedence.

    -- Without Parentheses SELECT * FROM orders WHERE product = 'Laptop' OR amount > 500 AND customer = 'Alice'; -- With Parentheses SELECT * FROM orders WHERE product = 'Laptop' OR (amount > 500 AND customer = 'Alice');
  2. Missing Parentheses: Forgetting parentheses in complex conditions can lead to unexpected results.

7. Best Practices

  1. Use Parentheses:
    When combining OR with AND, always use parentheses to clarify precedence.

  2. Avoid Overusing OR:
    For better performance, consider using IN for multiple equality checks instead of multiple OR conditions.

    -- Instead of this SELECT * FROM employees WHERE department = 'IT' OR department = 'HR' OR department = 'Finance'; -- Use this SELECT * FROM employees WHERE department IN ('IT', 'HR', 'Finance');

8. Performance Considerations

  • OR conditions can slow down queries when used excessively or without indexing.
  • Optimize queries by ensuring columns in OR conditions are indexed.

Let me know if you need more examples or further assistance!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close