Brightlane's address formatter builds location strings by combining a city, region, and country.
Write a query to return the result of combining 'Toronto', 'Ontario', and 'Canada' with CONCAT_WS using ', ' as the separator.
Output:
- A single row with one column,
location, containing the concatenated string with', 'between every pair of components.
Schema · ecommerce 5 tables
Run previews · Check grades
Write a query, then run it to see results here.
Worked solution Try it yourself first
SELECT
CONCAT_WS(', ', 'Toronto', 'Ontario', 'Canada') AS location The shape
CONCAT_WS(', ', 'Toronto', 'Ontario', 'Canada') takes the separator as its first argument and joins the remaining values with that separator between every pair. The result is 'Toronto, Ontario, Canada' with one ', ' between each component and none at the ends.
Clause by clause
SELECT CONCAT_WS(', ', 'Toronto', 'Ontario', 'Canada') AS locationevaluates the call and labels the resulting columnlocation. The first argument', 'is the separator; the remaining arguments are the values to combine.CONCAT_WSwalks the value list and inserts the separator between each adjacent pair, but never before the first value or after the last. There is noFROMbecause all four arguments are literals.
Why this and not 'Toronto' || ', ' || 'Ontario' || ', ' || 'Canada'
Both forms return the same string on this input. CONCAT_WS says the separator once and the values once, which scales cleanly to four, five, or ten components without re-typing the delimiter at each junction. The || chain repeats the separator literal between every pair, which is more to type and more to misread. CONCAT_WS is the right shape whenever the same separator is being inserted between every pair of values.
You practiced CONCAT_WS(separator, ...) — concatenate values with a single separator inserted between every pair, no manual separator-management needed.