SQL Practice Logo

SQLPractice Online

Complex WHERE Conditions & Boolean Logic: Examples

Module: Advanced Filtering

AND Operator

basic

Find active customers over 18

SELECT customer_id, name, age, status

FROM customers

WHERE age > 18 AND status = 'active'

ORDER BY name;

customer_id | name | age | status

1 | Alice | 25 | active

2 | Bob | 30 | active

AND requires both conditions true. Only customers who are both over 18 AND active are returned.

All

OR with Parentheses

intermediate

Find electronics or computers under $1000

SELECT product_id, product_name, category, price

FROM products

WHERE (category = 'electronics' OR category = 'computers')

AND price < 1000

ORDER BY price;

product_id | product_name | category | price

1 | Mouse | electronics | 25.00

2 | Keyboard | electronics | 75.00

3 | Monitor | computers | 250.00

Parentheses ensure OR evaluates first, then AND filters by price. Without parentheses, logic would be wrong.

All

Complex Multi-Condition Filter

advanced

Find high-value active or trial customers in specific regions

SELECT customer_id, name, status, country, lifetime_value

FROM customers

WHERE (status = 'active' OR status = 'trial')

AND country IN ('US', 'CA', 'UK')

AND lifetime_value > 1000

AND NOT is_blocked = true

ORDER BY lifetime_value DESC;

customer_id | name | status | country | lifetime_value

1 | Alice | active | US | 5000.00

2 | Bob | trial | CA | 1500.00

Multiple conditions combined: status check, country list, value threshold, exclusion. Parentheses clarify OR logic.