BBampaa

Lessons › SQL › Chapter 2: Filtering

SQL lesson

Combining WHERE with AND and OR

AND keeps a row only if both rules are true. OR keeps it if either rule is true. When you mix them, put brackets around the part that goes together, so SQL reads it the way you mean.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Two rules at once

SELECT *
FROM orders
WHERE category = 'Electronics' AND revenue > 200

Result:

order_idcustomer_idcategoryrevenueorder_date
O2001C101Electronics299.992024-03-01

Step by step

  1. Keeps a row only if both rules are true.
    SELECT * FROM orders WHERE (category = 'Electronics' OR category = 'Clothing') AND status = 'shipped'
  2. Keeps a row if at least one rule is true.
    SELECT * FROM orders WHERE (category = 'Electronics' OR category = 'Clothing') AND status = 'shipped'
  3. Brackets are worked out first, like in maths.
    SELECT * FROM orders WHERE (category = 'Electronics' OR category = 'Clothing') AND status = 'shipped'

Practice

Try each one in your head first, then open the answer.

1. Using orders, find the orders where category is 'Electronics' AND revenue is less than 200.
SELECT * FROM orders WHERE category = 'Electronics' AND revenue < 200
2. Using products, find the Electronics products that cost more than 200.
SELECT * FROM products WHERE category = 'Electronics' AND price > 200

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed