Learn SQL/Intermediate/Set Operations/SQL Set Operation Column Compatibility

SQL Set Operation Column Compatibility

Both queries must return the same number of columns, in compatible types — the one hard rule every set operation shares.

What & Why

Unlike a JOIN, a set operation isn't matching rows by a key — it's stacking them, which means the two queries' output shapes have to line up exactly. Same number of columns, and each corresponding column needs a compatible type (an integer column can typically pair with another integer or numeric column, but not directly with text).

See How It Works

BUSINESS QUESTION

Marketing wants campaign and lead milestones aligned to the same three-column contract.

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
idcampaign_idemailcreated_atqualified_atconverted_atlead_scoresourcecountry
3011ana@example.com2024-01-21 09:10:00+002024-01-22 11:00:00+002024-02-02 10:00:00+0086google_adsUS
3021ben@example.com2024-01-24 12:40:00+00NULLNULL52google_adsCA
3032chloe@example.com2024-02-16 08:30:00+002024-02-18 14:20:00+00NULL74emailGB
3043dev@example.com2024-03-20 17:15:00+002024-03-21 09:00:00+002024-04-04 16:00:00+0091linkedinUS
EXAMPLE QUERY
SELECT
  name AS entity_name,
  start_date::timestamp AS occurred_at,
  'campaign'::text AS entity_type
FROM marketing.campaigns
UNION ALL
SELECT
  email AS entity_name,
  created_at AT TIME ZONE 'UTC' AS occurred_at,
  'lead'::text AS entity_type
FROM marketing.leads
ORDER BY occurred_at, entity_name;
RESULT — exact output from the displayed Queryflo rows
entity_nameoccurred_atentity_type
Spring Launch2024-01-15 00:00:00campaign
ana@example.com2024-01-21 09:10:00lead
ben@example.com2024-01-24 12:40:00lead
Retention Webinar2024-02-10 00:00:00campaign
chloe@example.com2024-02-16 08:30:00lead
Finance Retargeting2024-03-12 00:00:00campaign
dev@example.com2024-03-20 17:15:00lead
Enterprise Search2024-04-01 00:00:00campaign

Both branches return the same text, UTC timestamp, and text column shape.

Now You Try

Practice this concept

Combine campaign starts and lead creation events into one text, UTC timestamp, and text result contract.

Available schema
marketing

Prefix tables with marketing.table_name.

entity_nameoccurred_atentity_type
marketing.campaigns
ColumnType
idinteger
nametext
channeltext
spendnumeric
start_datedate
end_datedate
statustext
target_segmenttext
legacy_idtext
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
Intermediate business practice

Sign up free to try it on a real business scenario