
179 views
MySQL OR
The MySQL OR
operator is a logical operator used to combine two or more conditions in a SQL query. It is often used in the WHERE
clause of a query to filter rows based on multiple conditions. The OR
operator requires that at least one of the specified conditions is true for a row to be included in the result set.
The basic syntax for using the OR
operator is as follows:
SELECT columns
FROM table
WHERE condition1 OR condition2 OR ...;
Here’s how it works:
condition1
,condition2
, and so on are the conditions you want to combine with theOR
operator.- If any of the specified conditions are true for a row, the row is included in the result set.
- If all the conditions are false for a row, the row is excluded from the result set.
Here are some examples to illustrate the use of the OR
operator:
- Match all records where either the
category
is “Electronics” or theprice
is greater than $500:
SELECT *
FROM products
WHERE category = 'Electronics' OR price > 500;
- Retrieve customers with the name “John” or an email that ends with “@example.com”:
SELECT *
FROM customers
WHERE first_name = 'John' OR email LIKE '%@example.com';
- Find employees who work in either the “Sales” department or have a salary greater than $50,000:
SELECT *
FROM employees
WHERE department = 'Sales' OR salary > 50000;
The OR
operator is a valuable tool for building complex queries that involve multiple conditions. It allows you to specify that at least one of the conditions must be met for a row to be included in the query result, making it useful for creating inclusive filters based on various criteria.