SQL OR Operator
The OR
operator is used to combine multiple conditions in a SQL WHERE
clause. It returns rows that satisfy at least one of the specified conditions.
Basic Syntax:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
Example 1: Using OR with Two Conditions
SELECT *
FROM employees
WHERE department = 'Sales' OR department = 'Marketing';
This query selects all columns from the “employees” table where the “department” is either ‘Sales’ or ‘Marketing’.
Example 2: Using OR with Multiple Conditions
SELECT *
FROM customers
WHERE country = 'USA' OR country = 'Canada' OR country = 'Mexico';
This query selects all columns from the “customers” table where the “country” is either ‘USA’, ‘Canada’, or ‘Mexico’.
Example 3: Combining AND and OR Operators
SELECT *
FROM products
WHERE category = 'Electronics' AND (price < 300 OR brand = 'BrandA');
This query selects all columns from the “products” table where the “category” is ‘Electronics’ and either the “price” is less than 300 or the “brand” is ‘BrandA’.
The OR
operator is useful for querying data where multiple conditions can be true. It allows for flexible filtering based on various criteria.