Learn SQL/Beginner/Filtering Data/SQL Comparison Operators

SQL Comparison Operators

The six symbols WHERE conditions are built from.

What & Why

Every basic WHERE condition uses one of six comparison operators. The operator tells PostgreSQL exactly how to compare a row’s value with the value in your condition.

OperatorMeaning
=equal to
!= or <>not equal to
>greater than
<less than
>=greater than or equal to
<=less than or equal to

See How It Works

Start with these four rows from marketing.campaigns. The condition keeps campaigns whose channel is not google_ads.

idnamechannelspendstart_date
1Spring Launchgoogle_ads55000.002024-01-15
2Retention Webinaremail45000.002024-02-10
3Finance Retargetinglinkedin50000.002024-03-12
4Enterprise Searchgoogle_ads60000.002024-04-01
EXAMPLE QUERY
SELECT name FROM marketing.campaigns WHERE channel != 'google_ads';
RESULT — channel != 'google_ads'
name
Retention Webinar
Finance Retargeting

The two google_ads rows fail the not-equal comparison, so only email and linkedin campaigns remain.

Now You Try

Practice this concept

Marketing wants low-score leads that need cleanup. Return leads with lead score below 40.

Available schema
marketing

Prefix tables with marketing.table_name.

idsourcelead_score
marketing.leads
ColumnType
idinteger
campaign_idinteger
emailtext
created_attimestamp with time zone
qualified_attimestamp with time zone
converted_attimestamp with time zone
lead_scoreinteger
sourcetext
countrytext
archive_statustext
query.sql
Beginner business practice

Sign up free to try it on a real business scenario