BBampaa

Lessons › SQL › Chapter 2: Filtering

SQL lesson

Combining WHERE with AND and OR

Combining AND and OR lets you stack multiple conditions onto a single filter - AND means every condition must be true to keep a row, while OR means satisfying just one is enough.

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

Example

Filtering Retail Orders with WHERE, AND, and OR

SELECT * FROM orders
WHERE (category = 'Electronics' OR category = 'Clothing')
AND status = 'shipped'

Result:

order_idcategorystatusrevenueorder_date
O2001Electronicsshipped299.992024-03-01
O2005Clothingshipped62.02024-03-05

Step by step

  1. The asterisk symbol means all columns.
    SELECT *
  2. FROM tells SQL which table to pull the data from.
    SELECT *
    FROM orders
  3. WHERE keeps only the rows that match your condition.
    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 products where category is 'Beauty' OR category is 'Home'.
SELECT * FROM products WHERE category = 'Beauty' OR category = 'Home'
3. Using orders, find the orders where revenue is greater than 250 OR category is 'Clothing'.
SELECT * FROM orders WHERE revenue > 250 OR category = 'Clothing'
4. Using orders, find the Clothing orders worth more than 100, plus every order from customer 'C103'.
SELECT * FROM orders WHERE (category = 'Clothing' AND revenue > 100) OR customer_id = 'C103'
5. Using products, find the products in 'Electronics' or 'Clothing' that cost less than 100.
SELECT * FROM products WHERE (category = 'Electronics' OR category = 'Clothing') AND price < 100

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