Precedence & Parentheses
Learn the order SQL evaluates NOT, AND and OR, and use parentheses to make mixed conditions mean what you intend.
As soon as one WHERE has both AND and OR, the question becomes: which part is grouped with which? SQL does not read left-to-right — it follows operator precedence, just like maths does × before +. Getting this wrong is one of the most common bugs in real queries.
Definition
Operator precedence is the fixed order in which SQL evaluates operators in an expression when no parentheses are present. For conditions the order is: comparison operators (
=,<>,>, …) first, then NOT, then AND, then OR last. Parentheses( )override this order — whatever is inside them is evaluated first.
Precedence table
| Priority | Operators | Think of it like |
|---|---|---|
| 1 (highest) | ( ) parentheses | brackets in maths |
| 2 | arithmetic * / % then + - | maths |
| 3 | comparisons = <> != > >= < <= | "build each test" |
| 4 | NOT | flip one test |
| 5 | AND | like multiplication |
| 6 (lowest) | OR | like addition |
So A OR B AND C is read as A OR (B AND C) — exactly like a + b × c is a + (b × c).
Syntax
sql
SELECT column_list
FROM table_name
WHERE (condition1 OR condition2)
AND condition3;Example — the bug: no parentheses
Goal: "MI or RCB players who scored more than 600".
sql
SELECT name, team, country, runs
FROM players
WHERE team = 'MI' OR team = 'RCB' AND runs > 600;| name | team | country | runs |
|---|---|---|---|
| Virat Kohli | RCB | India | 750 |
| Rohit Sharma | MI | India | 500 |
| Jasprit Bumrah | MI | India | 20 |
Rohit (500) and Bumrah (20) sneak in! Because AND runs before OR, SQL read it as:
team = 'MI' OR (team = 'RCB' AND runs > 600) → every MI player, plus RCB players over 600.
Example — the fix: add parentheses
sql
SELECT name, team, country, runs
FROM players
WHERE (team = 'MI' OR team = 'RCB')
AND runs > 600;| name | team | country | runs |
|---|---|---|---|
| Virat Kohli | RCB | India | 750 |
Now the team test is evaluated as a group first, and then every row must also have more than 600 runs.
Example — same words, different groups
sql
-- A: Indian, and (MI or 700+)
SELECT name, team, runs
FROM players
WHERE country = 'India'
AND (team = 'MI' OR runs > 700);| name | team | runs |
|---|---|---|
| Virat Kohli | RCB | 750 |
| Rohit Sharma | MI | 500 |
| Jasprit Bumrah | MI | 20 |
sql
-- B: (Indian and MI), or 700+
SELECT name, team, runs
FROM players
WHERE country = 'India' AND team = 'MI'
OR runs > 700;| name | team | runs |
|---|---|---|
| Virat Kohli | RCB | 750 |
| Rohit Sharma | MI | 500 |
| Jos Buttler | RR | 850 |
| Jasprit Bumrah | MI | 20 |
Query B lets Jos Buttler (England) in, because runs > 700 stands on its own side of the OR.
Common mistakes
- ❌ Relying on memory of precedence in mixed AND/OR conditions. ✅ Always put parentheses around OR groups when AND is also present — it costs nothing and makes intent clear.
- ❌ Indenting to "show" grouping: indentation means nothing to SQL. Only parentheses do.
- ❌ Unbalanced brackets:
WHERE (team = 'MI' OR team = 'RCB' AND runs > 600→ syntax error.
Tip: Often the cleanest fix is
IN:WHERE team IN ('MI', 'RCB') AND runs > 600— no OR, no precedence trap.
Interview tip
Q: What does WHERE a = 1 OR b = 2 AND c = 3 mean?
A: a = 1 OR (b = 2 AND c = 3), because AND has higher precedence than OR. If you meant (a = 1 OR b = 2) AND c = 3, you must write the parentheses.
Key points to remember
- Precedence order: parentheses → arithmetic → comparisons → NOT → AND → OR.
- AND is evaluated before OR, so A OR B AND C means A OR (B AND C).
- Parentheses override precedence — the bracketed part is evaluated first.
- Mixing AND and OR without brackets is a classic bug that silently returns extra rows.
- Indentation does not group conditions; only parentheses do.
- IN ('MI', 'RCB') is often a cleaner replacement for an OR group.
Try it yourself
Bug vs fix: MI or RCB with 600+ runs
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Same conditions, different grouping
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: fix the brackets
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
