BBampaa

Lessons › SQL › Chapter 2: Filtering

SQL lesson

FILTERING with WHERE

WHERE keeps only the rows that match a rule, like category = 'Electronics'. The rest are left out. The rule is a column, a comparison and a value. Text goes in single quotes. Numbers don't.

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

Example

Keep only the rows you want

SELECT *
FROM orders
WHERE category = 'Electronics'

Result:

order_idcategorystatusrevenue
O2001Electronicsshipped149.99
O2003Electronicsdelivered299.0

Step by step

  1. Keeps only the rows that match. The rest are left out.
    SELECT * FROM orders WHERE category = 'Electronics'
  2. A column, a comparison, then the value to match.
    SELECT * FROM orders WHERE category = 'Electronics'
  3. Text goes in single quotes. Numbers don't: revenue > 100.
    SELECT * FROM orders WHERE category = 'Electronics'

Practice

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

1. Using orders, find the orders where revenue is greater than 150.
SELECT * FROM orders WHERE revenue > 150
2. Using products, find the products where price is less than 50.
SELECT * FROM products WHERE price < 50
3. Using orders, find the orders where category is 'Clothing'.
SELECT * FROM orders WHERE category = 'Clothing'
4. Using inventory, find the rows where stock_count is less than 50.
SELECT * FROM inventory WHERE stock_count < 50
5. Using customers, show the full_name and email of the customer whose customer_id is 'C103'.
SELECT full_name, email FROM customers WHERE customer_id = 'C103'

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