SQL JOINs with Comparison Operators
A join condition doesn't have to be equality — >, <, >=, and <= all work perfectly well inside ON, opening up genuinely useful matching patterns.
What & Why
Every join so far in this series has matched rows using = — an "equi-join." Nothing about ON requires that. Any condition that evaluates to true or false is valid, including >, <, >=, and <=.
See How It Works
BUSINESS QUESTION
Attach campaigns that were active when each lead was created.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
| id | name | channel | spend | start_date | end_date | status | target_segment |
|---|---|---|---|---|---|---|---|
| 1 | Spring Launch | google_ads | 55000.00 | 2024-01-15 | 2024-03-31 | active | smb |
| 2 | Retention Webinar | 45000.00 | 2024-02-10 | 2024-04-15 | active | enterprise | |
| 3 | Finance Retargeting | 50000.00 | 2024-03-12 | 2024-05-31 | active | enterprise | |
| 4 | Enterprise Search | google_ads | 60000.00 | 2024-04-01 | 2024-06-30 | active | enterprise |
EXAMPLE QUERY
SELECT
l.id AS lead_id,
c.name AS campaign_name,
l.created_at
FROM marketing.leads l
JOIN marketing.campaigns c
ON l.created_at::date BETWEEN c.start_date AND COALESCE(c.end_date, DATE '2024-12-31')
ORDER BY lead_id, campaign_name;RESULT — exact output from the displayed Queryflo rows
| lead_id | campaign_name | created_at |
|---|---|---|
| 301 | Spring Launch | 2024-01-21 09:10:00+00 |
| 302 | Spring Launch | 2024-01-24 12:40:00+00 |
| 303 | Retention Webinar | 2024-02-16 08:30:00+00 |
| 303 | Spring Launch | 2024-02-16 08:30:00+00 |
| 304 | Finance Retargeting | 2024-03-20 17:15:00+00 |
| 304 | Retention Webinar | 2024-03-20 17:15:00+00 |
| 304 | Spring Launch | 2024-03-20 17:15:00+00 |
The range predicate can match one lead date to several overlapping campaign windows.
Now You Try
Practice this concept
Return every lead-campaign date-range match from the displayed Queryflo rows, ordered by lead_id and campaign_name.
Available schema
marketingPrefix tables with marketing.table_name.
lead_idcampaign_namecreated_atmarketing.campaigns| Column | Type |
|---|---|
| id | integer |
| name | text |
| channel | text |
| spend | numeric |
| start_date | date |
| end_date | date |
| status | text |
| target_segment | text |
| legacy_id | text |
marketing.leads| Column | Type |
|---|---|
| id | integer |
| campaign_id | integer |
| text | |
| created_at | timestamp with time zone |
| qualified_at | timestamp with time zone |
| converted_at | timestamp with time zone |
| lead_score | integer |
| source | text |
| country | text |
| archive_status | text |
query.sql
Sign up free to try it on a real business scenario