# SQL OR: Return Rows That Match Any Condition

- URL: https://betteranalyst.com/sql/or

SQL guide · Filtering

The `OR` operator combines conditions so that a row is returned when at least one of them is true. Where `AND` narrows a result, `OR` widens it: every extra branch can only add rows.

It is typically used inside `WHERE`, but it works in any boolean context such as `HAVING`, `ON` clauses and `CASE` expressions. When several `OR` branches test the same column against different values, the `IN` operator is shorter and easier to read.

Under three-valued logic, true OR unknown is true, while false OR unknown is unknown. That means a row where one branch is true is kept even if another branch involves a NULL, but a row where every branch is false or unknown is dropped.

## Basic syntax

```
SELECT column1, column2
FROM table_name
WHERE condition1
   OR condition2
   OR condition3;
```

Each branch is a full boolean expression. `OR` has lower precedence than `AND`, so `a OR b AND c` is read as `a OR (b AND c)`. Use parentheses whenever both operators appear in one condition. Remember that each additional branch can only add rows, so a long chain of `OR` conditions is a sign that the query may be returning more than intended. Test the combined logic on a small sample before using it in a destructive statement.

## SQL OR examples

### Either of two values

A row qualifies if the customer is in either country. This returns customers from the United Kingdom or Ireland.

```
SELECT customer_id, name, country
FROM customers
WHERE country = 'United Kingdom'
   OR country = 'Ireland';
```

### Conditions on different columns

The branches do not have to test the same column. This finds orders that are either unusually large or still unpaid, which a finance team might review together.

```
SELECT order_id, total, payment_status
FROM orders
WHERE total > 10000
   OR payment_status = 'unpaid';
```

### OR inside parentheses with AND

The parentheses make the `OR` a single unit, so the date filter applies whichever department matches. Without them the date rule would apply to only the second department.

```
SELECT employee_id, name, department
FROM employees
WHERE (department = 'Sales' OR department = 'Support')
  AND hire_date >= '2025-01-01';
```

### Replace many ORs with IN

Three or more equality tests on one column read better as `IN`. The two queries below return exactly the same rows.

```
-- Long form
SELECT * FROM products
WHERE category = 'Books' OR category = 'Games' OR category = 'Music';

-- Shorter equivalent
SELECT * FROM products
WHERE category IN ('Books', 'Games', 'Music');
```

## Where SQL dialects differ

**PostgreSQL:** Boolean columns can be used directly, as in `WHERE is_vip OR total > 1000`. The planner can sometimes turn an `OR` on indexed columns into a bitmap OR scan.

**MySQL:** Accepts `||` as a synonym for `OR` unless the `PIPES_AS_CONCAT` mode is on, in which case `||` concatenates strings. Use the keyword to stay portable.

**SQL Server:** Each side must be a predicate rather than a bit value, so write `is_vip = 1 OR total > 1000`. Large `OR` lists across different columns can prevent efficient index use.

**SQLite:** Numeric values are treated as true when non-zero, so `WHERE flag_a OR flag_b` works on integer columns.

## Common mistakes

- Mixing `OR` with `AND` without parentheses. Because `AND` binds tighter, the result often returns more rows than expected.
- Writing `WHERE status = 'open' OR 'pending'`. Every branch needs its own comparison, or use `IN`.
- Using `OR` to test across NULLs, such as `col = 1 OR col <> 1`, and expecting every row. Rows with NULL in `col` still fail both branches.
- Relying on `OR` over many different columns for performance-sensitive queries. It can force a full scan; consider splitting into a `UNION`.

## Related SQL guides

- [SQL WHERE](https://betteranalyst.com/sql/where)
- [SQL AND](https://betteranalyst.com/sql/and)
- [SQL IN](https://betteranalyst.com/sql/in)
- [SQL NOT](https://betteranalyst.com/sql/not)
- [SQL UNION](https://betteranalyst.com/sql/union)
- [SQL CASE](https://betteranalyst.com/sql/case)

[Browse all SQL guides](https://betteranalyst.com/sql)

## Need help writing the SQL?

Describe your tables and the result you want, then review the generated query against your database before running it.

[Try the AI SQL generator](https://betteranalyst.com/ai-sql-query-generator)
