SQL CONCAT_WS

CONCAT 'With Separator' — joins values with a shared delimiter between each one, without manually inserting it between every pair.

What & Why

CONCAT_WS(separator, a, b, c, ...) joins values together with the given separator automatically placed between each pair — no need to manually type the delimiter between every value like plain CONCAT or || requires.

See How It Works

BUSINESS QUESTION

Marketing wants one readable lead source and country label.

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
  id AS lead_id,
  CONCAT_WS(' · ', source, country) AS lead_origin
FROM marketing.leads
ORDER BY lead_id;
RESULT — exact output from the displayed Queryflo rows
lead_idlead_origin
301google_ads · US
302google_ads · CA
303email · GB
304linkedin · US

CONCAT_WS joins the actual source and country values for each lead.

Now You Try

Practice this concept

Marketing wants one readable lead source and country label.

Available schema
marketing

Prefix tables with marketing.table_name.

lead_idlead_origin
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