DevLearningTools

LEARN · SQL

SQL OR

OR combines multiple WHERE conditions so that a row matches if at least one of them is true. Learn the syntax, and why parentheses matter once OR is mixed with AND.

Where AND requires every condition to be true, OR requires only one. A row matches if at least one of the OR'd conditions is true, even if the others are false.

Learning Objectives

  • Combine conditions with OR, matching when at least one is true.
  • Chain more than two conditions with OR.
  • Use parentheses to control evaluation order once AND and OR are mixed in the same query.

Condition A

department = 'Engineering'

OR Condition B

department = 'Research'

Matching Rows

Rows where at least one is true

OR: At Least One Condition Must Be True

Only one of the conditions needs to be true. This row matches if it's in Engineering, or in Research, or (hypothetically) both, OR doesn't exclude a row for also matching the other condition.

MySQL / MariaDB / PostgreSQL / SQLite / SQL Server / Oracle
SELECT first_name, department
FROM employees
WHERE department = 'Engineering' OR department = 'Research';

AND vs OR: The Difference

The core difference: AND is restrictive, every extra condition filters the result down further. OR is permissive, every extra condition opens the result up further. Picking the wrong one is a common, silent mistake, a query meant to narrow results down can accidentally explode in size if OR was used where AND was meant, or vice versa.

AND
  • Every single condition must be true
  • Adding another AND condition can only narrow the results, never widen them
  • Use when a row has to satisfy all the requirements at once
OR
  • At least one condition must be true
  • Adding another OR condition can only widen the results, never narrow them
  • Use when a row matching any of several possibilities is good enough

Mixing AND and OR: Parentheses Matter

AND evaluates before OR, the same way multiplication evaluates before addition in arithmetic. The first query above actually reads as (department = 'Engineering' AND salary > 95000) OR department = 'Research', every Research employee matches regardless of salary, which may or may not be the intent. Parentheses remove the ambiguity and make the intent explicit to the next reader.

Ambiguous without parentheses
SELECT * FROM employees
WHERE department = 'Engineering' AND salary > 95000 OR department = 'Research';
Explicit, and almost certainly what was meant
SELECT * FROM employees
WHERE (department = 'Engineering' AND salary > 95000) OR department = 'Research';
NOTE

This evaluation order, AND before OR, is standard SQL and identical across MySQL, MariaDB, PostgreSQL, SQLite, SQL Server, and Oracle. No dialect changes it.

Try It Yourself

Write a query returning employees who are either in Engineering with a salary over 95000, or in Research with a salary over 85000.

Answer
SELECT first_name, department, salary
FROM employees
WHERE (department = 'Engineering' AND salary > 95000)
   OR (department = 'Research' AND salary > 85000);

Frequently Asked Questions

Can I combine more than two conditions with OR?

Yes, any number of conditions can be chained with OR, a row matches if any single one of them is true.

Is OR slower than AND?

Not inherently, but an OR condition can sometimes prevent a database from using an index as efficiently as an equivalent AND condition can, worth knowing for performance-sensitive queries, though rarely a concern at a beginner stage.

Common Beginner Mistakes

Mixing AND and OR without parentheses and assuming left-to-right evaluation

SQL evaluates AND before OR, not strictly left to right. A OR B AND C means A OR (B AND C), not (A OR B) AND C. Always add explicit parentheses when a query mixes both.

Using a long OR chain instead of IN

WHERE department = 'Engineering' OR department = 'Research' works, but it's repetitive and easy to mistype as the list grows. The IN operator, covered in a later lesson in this module, says the same thing more clearly.

Best Practices

  • Add parentheses any time a condition mixes AND and OR, even when confident about the default evaluation order.
  • Reach for IN (...) instead of a chain of OR = comparisons once checking against more than two or three values.
  • Keep each individual condition simple and readable, split an overly complex WHERE across multiple lines.

Interview Questions

Why is WHERE department = 'Engineering' AND salary > 95000 OR department = 'Research' probably a bug?

Without parentheses, AND binds tighter than OR, so this reads as (department = 'Engineering' AND salary > 95000) OR department = 'Research', returning every Research employee regardless of salary, likely not the intent. The fix is explicit parentheses around whichever grouping was actually meant.

Rewrite WHERE department = 'Engineering' OR department = 'Research' OR department = 'Marketing' more concisely.

WHERE department IN ('Engineering', 'Research', 'Marketing'); — the IN operator, covered in a later lesson, expresses the same "any of these" logic as a chain of OR'd equality checks, more concisely.

Summary

This lesson covered matching when at least one condition is true with OR, and why parentheses are required once AND and OR are mixed in the same WHERE clause, since AND evaluates first by default.

What's Next?

The next lesson covers NOT, reversing a condition's result.

</>

Ready to Practice SQL?

Write and run real SQL queries online, right in your browser, no install or signup required. Pick a playground based on which database you want to practice on.

SQLite / PostgreSQL

Fast, no server, no ads.

Open DB Fiddle ↗

MySQL / SQL Server

Switch databases with one dropdown.

Open SQL Fiddle ↗

Both open a third-party SQL playground in a new tab. External site terms and privacy policy apply.