✏️ Explanatory Question
Level: Intermediate — Tests your knowledge of string joining and, crucially, NULL handling.
Both functions join two or more strings into one, but they differ in how they handle separators and NULL values.
CONCAT_WS() is NULL-safe. A single NULL won't wipe out the whole result — it simply gets skipped, and the separator is not duplicated around it.
| Feature | CONCAT() | CONCAT_WS() |
|---|---|---|
| Separator | None (manual) | First argument |
| NULL handling | Returns NULL if any arg is NULL | Skips NULL values |
| Best for | Simple joins | CSV, full names, addresses |
CONCAT('A', NULL, 'B')CONCAT_WS('-', 'A', NULL, 'B')-- CONCAT: manual separator, NULL breaks it
SELECT CONCAT('My', 'SQL'); -- MySQL
SELECT CONCAT(first_name, ' ', last_name) -- needs manual space
FROM employees;
SELECT CONCAT('A', NULL, 'B'); -- NULL
-- CONCAT_WS: separator handled once, NULL-safe
SELECT CONCAT_WS(' ', first_name, middle_name, last_name) AS full_name
FROM employees; -- skips NULL middle_name cleanly
-- Build a comma-separated address
SELECT CONCAT_WS(', ', street, city, state, pincode) AS address
FROM customers;