runtime
05 / 88

lesson 05 Filtering & sorting · beginner

Combine conditions

Combine filters with AND, OR and parentheses.

the idea

AND requires both conditions. OR requires either. AND binds more tightly than OR, so parentheses make mixed conditions explicit.

tables in this lesson

The challenges use this data. Each column shows its type underneath; → marks a link to another table.

products4 columns · 6 rows
idinteger · keynametextcategorytextpricereal
1NotebookStationery12
2Desk lampHome45
3KeyboardTech80
4Pen setStationery8
5HeadphonesTech120

showing 5 of 6 rows

example

The idea in one query. Press Run to see what it returns; running examples never affects your progress.

example.sql
SELECT name, price
FROM products
WHERE category = 'Tech' AND price < 100;

Watch for: Without parentheses, a price filter may apply to only one side of OR.

build it step by step

Each step changes one thing. Run them in order and watch the result change.

  1. Require both conditions. Category and price are separate columns. AND keeps a row only when both are true.

    step-1.sql
    SELECT name, category, price
    FROM products;
  2. Run the reference. AND requires both conditions. OR requires either. AND binds more tightly than OR, so parentheses make mixed conditions explicit.

    step-2.sql
    SELECT name, price
    FROM products
    WHERE category = 'Tech' AND price < 100;

challenge 1

Show name and price for Tech products costing less than 100.

Return namepriceany row order

challenge-1.sql
 
To master this lesson, solve both challenges without hints and pass the concept check. 3 of 3 to go.