0
0
SQLquery~5 mins

WHERE with OR operator in SQL

Choose your learning style9 modes available
Introduction
The WHERE clause with OR lets you find rows that match at least one of several conditions. It helps you get more flexible search results.
Finding customers who live in city A or city B.
Selecting products that are either in stock or on sale.
Getting employees who work in department X or have a manager role.
Searching for orders placed in January or February.
Filtering students who passed math or science exams.
Syntax
SQL
SELECT column1, column2 FROM table_name WHERE condition1 OR condition2;
The OR operator checks if either condition1 or condition2 is true for each row.
You can use more than two conditions by adding more OR operators.
Examples
This finds employees who work in Sales or Marketing.
SQL
SELECT * FROM employees WHERE department = 'Sales' OR department = 'Marketing';
This gets product names where price is less than 10 or stock is more than 100.
SQL
SELECT name FROM products WHERE price < 10 OR stock > 100;
This selects orders with any of these three statuses.
SQL
SELECT * FROM orders WHERE status = 'Pending' OR status = 'Processing' OR status = 'Shipped';
Sample Program
This creates a fruits table, adds some fruits, then selects fruits that are either Red or Yellow in color.
SQL
CREATE TABLE fruits (id INT, name VARCHAR(20), color VARCHAR(20));
INSERT INTO fruits VALUES (1, 'Apple', 'Red');
INSERT INTO fruits VALUES (2, 'Banana', 'Yellow');
INSERT INTO fruits VALUES (3, 'Grape', 'Green');
INSERT INTO fruits VALUES (4, 'Cherry', 'Red');

SELECT * FROM fruits WHERE color = 'Red' OR color = 'Yellow';
OutputSuccess
Important Notes
Remember that OR returns true if any condition is true, so it can return more rows than AND.
Use parentheses to group conditions if you mix OR with AND to avoid confusion.
OR can slow down queries if used with many conditions; indexing helps speed it up.
Summary
WHERE with OR finds rows matching any of the given conditions.
Use OR to widen your search criteria in queries.
Combine multiple OR conditions to include many possibilities.