Skip
Arish's avatar

27. Combining AND and OR


Using AND and OR Together

Use parentheses to group conditions correctly.

The Problem

sql
1-- Ambiguous without parentheses
2SELECT * FROM students
3WHERE course = 'Python' OR course = 'JavaScript'
4AND marks > 80;

The Solution: Parentheses

sql
1-- Clear intention
2SELECT * FROM students
3WHERE (course = 'Python' OR course = 'JavaScript')
4AND marks > 80;

Operator Precedence

AND is evaluated before OR. Use parentheses to be explicit.

Example

High-scoring Python or JavaScript students:

sql
1SELECT * FROM students
2WHERE (course = 'Python' OR course = 'JavaScript')
3AND marks > 80;