Ravindra BagaleCourses & study guides Track your progress

4. WHERE: Keep Only the Rows You Need

4.4 AND, OR and NOT — Combine Conditions Safely

Why. Real filters need more than one condition.

What. AND = all must match. OR = any may match. NOT = reverse a condition.

How. Use parentheses when you mix AND and OR.

Steps

  1. Pune AND amount >= 800.
  2. Pune OR Mumbai.
  3. NOT city = 'Pune'.

Examples

Example 1 · dataset: raja_coffee

SELECT order_id, city, amount_inr
FROM orders
WHERE city = 'Pune' AND amount_inr >= 800
ORDER BY amount_inr DESC;

Output

order_idcityamount_inr
1016Pune1780.0
1013Pune1680.0
1004Pune1050.0
1006Pune890.0
1001Pune840.0

Meaning. Both conditions true: Pune and large amount.

Ravindra Bagale's Tip

When OR surprises you with too many rows, check parentheses.