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.
| 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 |
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_id | lead_origin |
|---|---|
| 301 | google_ads · US |
| 302 | google_ads · CA |
| 303 | email · GB |
| 304 | linkedin · 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
marketingPrefix tables with marketing.table_name.
lead_idlead_originmarketing.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