- SQL Basics
- SQL Introduction
- SQL Syntax
- SQL Sample Database
- SQL SELECT
- SQL WHERE
- SQL ORDER BY
- SQL DISTINCT
- SQL LIMIT
- SQL FETCH
- SQL AND
- SQL OR
- SQL BETWEEN
- SQL IN
- SQL LIKE
- SQL IS NULL
- SQL Comparison Operators
- SQL Logical Operators
- SQL Alias
- SQL CASE
- Joins and Subqueries
- SQL INNER JOIN
- SQL LEFT JOIN
- SQL RIGHT JOIN
- SQL FULL OUTER JOIN
- SQL SELF JOIN
- SQL CROSS JOIN
- SQL Subquery
- SQL Correlated Subquery
- SQL UNION
- SQL INTERSECT
- SQL EXCEPT
- Aggregate Functions
- SQL AVG
- SQL COUNT
- SQL MAX
- SQL MIN
- SQL SUM
- SQL GROUP BY
- SQL HAVING
- SQL ROLLUP
- SQL CUBE
- SQL GROUPING SETS
- Database Management
- SQL CREATE DATABASE
- SQL ALTER DATABASE
- SQL DROP DATABASE
- SQL BACKUP DATABASE
- SQL SHOW DATABASES
- SQL SELECT DATABASE
- Table Management
- SQL CREATE TABLE
- SQL ALTER TABLE
- SQL ADD COLUMN
- SQL DROP COLUMN
- SQL DROP TABLE
- SQL TRUNCATE TABLE
- SQL SHOW TABLES
- SQL RENAME TABLE
- SQL Constraints
- SQL Primary Key
- SQL Foreign Key
- SQL UNIQUE Constraint
- SQL CHECK Constraint
- SQL NOT NULL Constraint
SQL OR Operator
The OR operator in SQL is a logical operator used to filter records in a WHERE clause based on multiple criteria. Unlike the AND operator, which requires every condition to be met, the OR operator is more inclusive. It ensures that a row is included in your result set if any one of the specified conditions evaluates to true.
Think of the OR operator as a way to "expand" your search. If you are looking for customers who live in "London" OR "Paris," you are telling the database: "I don't care which one is true, as long as at least one of them matches."
Basic Syntax of OR Operator
The basic structure of an OR query involves placing the operator between two or more logical expressions. If the first condition is false, SQL moves on to check the second, and so on.
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3;
condition1: The primary filter you want to apply.condition2: An alternative filter to check if the first one fails.- Result: Rows are returned if at least one condition is true. If all conditions are false, the row is excluded.
WHERE city = 'New York' OR 'Chicago' will cause an error. You must repeat the column name for every condition: WHERE city = 'New York' OR city = 'Chicago'.
Key Features of OR Operator
1. Combining Multiple Conditions
The OR operator is perfect for scenarios where you have various independent criteria that could make a record relevant to your report.
SELECT name, age, city
FROM employees
WHERE age < 25 OR city = 'Chicago';
- This query captures two distinct groups: all employees under 25 (regardless of their city) and all employees in Chicago (regardless of their age).
OR is powerful, using it across many different columns can sometimes slow down your query on very large datasets because the database may have to perform multiple index scans. Always check your query execution plan if performance becomes an issue.
2. Flexible Filtering
In business logic, OR is often used for "flagging" records that need attention, such as items that are either too expensive or completely out of stock.
SELECT product_name, price, stock
FROM products
WHERE price > 1000 OR stock = 0;
- This is useful for a dashboard: it shows products that represent high-value inventory or products that require immediate restocking.
3. Using OR with Comparison Operators
The OR operator is highly versatile and works seamlessly with all standard comparison operators like >, <, =, <> (Not Equal), and >=.
SELECT name, salary, position
FROM employees
WHERE position = 'Manager' OR salary >= 8000;
- This retrieves anyone with a 'Manager' title, but it also captures high-earning individual contributors who make $8,000 or more, even if they aren't managers.
Example Queries
1. Filter Employees by Age or City
This is a classic "either-or" scenario used for targeted internal communications or office-specific events.
SELECT name, age, city
FROM employees
WHERE age < 30 OR city = 'New York';
- This returns any employee who is a young professional (under 30) or anyone based in the New York headquarters.
2. Filter Products by Price or Stock
Retailers often use this logic to find "budget" items or items running low to create clearance sales.
SELECT product_name, price, stock
FROM products
WHERE price < 50 OR stock < 20;
- This query identifies affordable products or those nearing depletion, helping inventory managers make quick decisions.
OR many times on the same column (e.g., WHERE id=1 OR id=2 OR id=3), use the IN operator instead. It is much cleaner and easier to read: WHERE id IN (1, 2, 3).
3. Using OR with Dates
Dates are frequently used with OR to find records outside of a specific range, such as finding "old" data or "future" scheduled items.
SELECT order_id, order_date, customer_id
FROM orders
WHERE order_date < '2024-01-01' OR order_date > '2024-06-30';
- This identifies orders placed in the previous year or orders placed in the second half of the current year, effectively skipping everything in the first half of 2024.
4. OR with Multiple Conditions
You aren't limited to just two conditions. You can chain as many OR statements as needed to widen your search net.
SELECT name, position, salary, city
FROM employees
WHERE position = 'Manager' OR city = 'Los Angeles' OR salary > 7000;
- This query is very broad: it will return a row if the person is a Manager, or if they live in LA, or if they earn a high salary. If a person meets all three, they still appear in the results.
Combining OR with AND
In real-world development, you will often need to combine OR and AND to build complex logic. This is where most bugs occur due to Operator Precedence.
SQL evaluates AND before OR. To ensure your query works as intended, you should use parentheses to group your OR conditions.
SELECT name, department, salary
FROM employees
WHERE department = 'HR' AND (salary > 6000 OR city = 'New York');
- How it works: The database first looks for employees who either have a salary over 6000 or live in New York. Then, from that group, it only picks those who are also in the 'HR' department.
- Without the parentheses, the logic would change completely!
AND and OR. If you don't, SQL will process all AND conditions first, which can lead to unexpected results and difficult-to-track bugs in your application.
Summary
The OR operator is a fundamental tool for broadening your SQL queries. While AND narrows your results down, OR expands them by allowing multiple paths for a record to be included. By mastering OR along with proper grouping (parentheses), you can handle complex business requirements and retrieve exactly the data you need.