DevPath · Learn to code ESPTEN

Advanced filtering

Logical and comparison operators

Comparison operators

Inside WHERE you compare values with these operators:

Operator Meaning
= equal
<> not equal
< less than
<= less than or equal
> greater than
>= greater than or equal
SELECT name, price FROM products WHERE price >= 100;
SELECT name FROM products WHERE category <> 'audio';

Logical operators: AND, OR, NOT

They combine several conditions in the same WHERE:

-- Peripherals that also cost less than 30
SELECT * FROM products
WHERE category = 'peripherals' AND price < 30;

-- Audio or display products
SELECT * FROM products
WHERE category = 'audio' OR category = 'displays';

-- Everything that is NOT audio
SELECT * FROM products
WHERE NOT category = 'audio';

Precedence and parentheses

AND has higher priority than OR (it is evaluated first). When you mix the two, use parentheses to make the grouping clear and avoid surprises:

-- Without parentheses, AND "binds" first:
--   category='audio' OR (category='displays' AND price < 200)
SELECT * FROM products
WHERE category = 'audio' OR category = 'displays' AND price < 200;

-- With parentheses you control the order:
SELECT * FROM products
WHERE (category = 'audio' OR category = 'displays') AND price < 200;

Practical rule: as soon as you mix AND and OR, add parentheses. The query becomes more readable and you make your intention explicit.

Put this into practice

DevPath is a hands-on course: you read the theory here; in the app you put it into practice with exercises that really run, offline.

Start free in the app →
IN, BETWEEN, LIKE and IS NULL →