Combining conditions with OR #
While AND requires every condition to be true, OR only requires at least one of the conditions to be true.
Syntax #
SELECT
column_name
FROM
table_name
WHERE
condition_1
OR condition_2;
A row is included if condition_1 is true, if condition_2 is true, or if both are true.
Example #
The orders table has a status column that can be 'Pending', 'Shipped', 'Delivered', or 'Cancelled'. To find every order that still needs attention, we can look for the ones that are either 'Pending' or 'Cancelled':
SELECT
*
FROM
orders
WHERE
status = 'Pending'
OR status = 'Cancelled';
Result:
+----+-------------+------------+-----------+--------------+
| id | customer_id | order_date | status | shipped_date |
+----+-------------+------------+-----------+--------------+
| 1 | 3 | 2025-... | Cancelled | None |
| 4 | 1 | 2025-... | Cancelled | None |
| 5 | 7 | 2025-... | Cancelled | None |
| .. | ... | ... | ... | ... |
Any order whose status is 'Shipped' or 'Delivered' is excluded, since it satisfies neither condition.
Exercise #
Write a query that retrieves all columns from the orders table, keeping only the rows where status is 'Pending' or status is 'Cancelled'.
- The result has a
statuscolumn. - The result contains exactly the rows where
statusis'Pending'or'Cancelled', and no others.
Solution
SELECT
*
FROM
orders
WHERE
status = 'Pending'
OR status = 'Cancelled';
Basic Data Retrieval
Filtering Data
Aggregating Data
Joining Tables
Creating and Managing Tables
Transactions and Data Integrity