SQL Practice Logo

SQLPractice Online

Complex WHERE Conditions & Boolean Logic: Functions

Module: Advanced Filtering

-- Simple AND

SELECT * FROM customers WHERE age > 18 AND status = 'active';

-- Simple OR

SELECT * FROM orders WHERE status = 'pending' OR status = 'processing';

-- AND with OR (needs parentheses)

SELECT * FROM products

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

AND price < 1000;

-- Multiple conditions

SELECT * FROM customers

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

AND age BETWEEN 18 AND 65

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

AND NOT is_blocked = true;

-- Complex nested logic

SELECT * FROM orders

WHERE (

(status = 'pending' AND created_date > '2024-01-01')

OR (status = 'processing' AND priority = 'high')

) AND total > 100;

AND requires all conditions true

OR requires at least one condition true

NOT negates condition

Precedence: NOT > AND > OR

Parentheses override precedence

Evaluation left to right within same precedence

Core references in this topic include WHERE, =, <, >, <=, >=. Learn what each one does, when to use it, and the execution or engine rules that matter.

WHERE

Filters rows before projection and sorting. It decides which rows continue through the query pipeline.

SELECT ... FROM table WHERE condition;

Most performance issues start with a weak WHERE clause or a missing supporting index.

=

Returns rows where the left and right values are exactly equal.

column = value

Use with exact matches. Do not use = NULL.

<, >, <=, >=

Range comparison operators for less-than, greater-than, and inclusive boundary checks.

salary >= 80000

AND

Requires every condition in the boolean expression to evaluate to TRUE.