AND & OR
Combine several conditions: AND needs all of them TRUE, OR needs at least one TRUE.
Real questions usually have more than one rule. "Indian players and more than 400 runs." "MI players or anyone with 800+ runs." The logical operators AND and OR glue simple conditions together into one bigger condition.
Definition
A logical operator combines (or reverses) Boolean conditions and returns TRUE, FALSE or UNKNOWN.
- AND returns TRUE only when all the combined conditions are TRUE.
- OR returns TRUE when at least one of the combined conditions is TRUE.
In simple words: AND makes the filter stricter (fewer rows — every test must pass), OR makes it looser (more rows — any one test is enough).
Syntax
sql
SELECT column_list
FROM table_name
WHERE condition1 AND condition2; -- both must be TRUE
SELECT column_list
FROM table_name
WHERE condition1 OR condition2; -- at least one must be TRUEYou can chain as many as you like: c1 AND c2 AND c3, c1 OR c2 OR c3.
Truth tables
| A | B | A AND B | A OR B |
|---|---|---|---|
| TRUE | TRUE | TRUE | TRUE |
| TRUE | FALSE | FALSE | TRUE |
| FALSE | TRUE | FALSE | TRUE |
| FALSE | FALSE | FALSE | FALSE |
| TRUE | UNKNOWN | UNKNOWN | TRUE |
| FALSE | UNKNOWN | FALSE | UNKNOWN |
(UNKNOWN comes from NULL. Since WHERE keeps only TRUE, UNKNOWN rows are dropped.)
Example — AND: India AND runs > 400
sql
SELECT *
FROM players
WHERE country = 'India'
AND runs > 400;| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 2 | Rohit Sharma | MI | India | 500 |
How it works: each row must pass both tests. MS Dhoni is Indian but has 350 runs → fails the runs test. Jos Buttler has 850 runs but is from England → fails the country test.
Example — OR: MI players OR 800+ runs
sql
SELECT *
FROM players
WHERE team = 'MI'
OR runs >= 800;| id | name | team | country | runs |
|---|---|---|---|---|
| 2 | Rohit Sharma | MI | India | 500 |
| 3 | Jos Buttler | RR | England | 850 |
| 6 | Jasprit Bumrah | MI | India | 20 |
Rows pass if either condition is true: Rohit and Bumrah pass because of MI, Buttler passes because of 850 runs. OR returns more rows.
Example — the impossible AND
sql
SELECT name, team
FROM players
WHERE team = 'MI'
AND team = 'CSK';Result: no rows. A single row's team can't be MI and CSK at the same time. When you mean "players from MI and CSK" in English, SQL needs OR (or IN): team = 'MI' OR team = 'CSK'.
Common mistakes
- ❌ Translating English "and" directly: "MI and CSK players" →
team = 'MI' AND team = 'CSK'(0 rows). ✅team = 'MI' OR team = 'CSK'orteam IN ('MI', 'CSK'). - ❌
WHERE team = 'MI' OR 'CSK'→ each side of OR must be a full condition. ✅team = 'MI' OR team = 'CSK'. - ❌
WHERE runs > 400 AND < 800→ the column must be repeated. ✅runs > 400 AND runs < 800(or BETWEEN). - ❌ Mixing AND and OR without brackets — see the Precedence & Parentheses topic.
Interview tip
Q: A query with 2 conditions joined by AND returns 10 rows. If you change AND to OR, can it return fewer rows?
A: No. Every row that passes A AND B also passes A OR B, so OR returns the same or more rows.
Key points to remember
- AND returns TRUE only when every condition is TRUE — it narrows the result.
- OR returns TRUE when at least one condition is TRUE — it widens the result.
- Each side of AND / OR must be a complete condition: team = 'MI' OR team = 'CSK'.
- English 'MI and CSK players' means SQL OR (or IN), because one row has only one team.
- TRUE OR UNKNOWN = TRUE, but FALSE OR UNKNOWN = UNKNOWN; FALSE AND anything = FALSE.
- You can chain any number of conditions with AND / OR.
Try it yourself
AND: India AND runs > 400
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
OR: MI players OR 800+ runs
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
AND can be impossible
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: overseas stars
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.
