Learn SQL/Beginner/Filtering Data/SQL Parentheses in Conditions

SQL Parentheses in Conditions

Make the grouping explicit — never rely on a reader (or yourself, next month) remembering precedence rules.

What & Why

Wrapping part of a condition in parentheses forces that grouping explicitly, regardless of default precedence. The previous lesson's ambiguous query becomes unambiguous the moment it's parenthesized.

See How It Works

idnamechannelspendstart_dateend_datestatustarget_segment
1Spring Launchgoogle_ads55000.002024-01-152024-03-31activesmb
2Retention Webinaremail45000.002024-02-102024-04-15activeenterprise
3Finance Retargetinglinkedin50000.002024-03-122024-05-31activeenterprise
4Enterprise Searchgoogle_ads60000.002024-04-012024-06-30activeenterprise
EXAMPLE QUERY
-- explicit: matches what the query intends
SELECT name FROM marketing.campaigns
WHERE (channel = 'linkedin' OR channel = 'email') AND spend > 40000;
RESULT — the grouped condition is explicit
name
Retention Webinar
Finance Retargeting

Both campaigns belong to one of the grouped channels and have spend above 40000.

Now You Try

Practice this concept

Marketing wants only active campaigns that run through either content or webinar channels.

Available schema
marketing

Prefix tables with marketing.table_name.

namechannelstatus
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
query.sql
Beginner business practice

Sign up free to try it on a real business scenario