Brightlane's display system builds customer labels by combining a first name and a middle name. Some customers have no middle name on record.
Write a query to return the result of using CONCAT to combine 'Sarah' with a SQL NULL.
Output:
- A single row with one column,
display_name, containing the concatenated string.CONCATtreats a missing input as an empty string, so the result is the present value alone.
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('Sarah', NULL) AS display_name The shape
CONCAT('Sarah', NULL) returns 'Sarah' because CONCAT treats a NULL argument as an empty string and concatenates around it. The missing middle name is simply omitted from the output rather than poisoning it.
Clause by clause
SELECT CONCAT('Sarah', NULL) AS display_nameevaluates theCONCATcall and labels the resulting columndisplay_name.CONCATwalks its argument list and concatenates each non-NULL value in order, substituting an empty string for any NULL it encounters. The first argument'Sarah'is included; the second argument NULL contributes nothing; the combined result is the first value alone. There is noFROMbecause both arguments are literals.
Why this and not 'Sarah' || NULL
'Sarah' || NULL would return NULL, not 'Sarah'. The || operator propagates NULL through the result the moment either side is NULL, so a missing middle name would erase the whole label. CONCAT is the right tool here precisely because the display system needs the present value to come through whenever any component happens to be missing. The choice between || and CONCAT is a NULL-semantics decision: || when missing should invalidate the result, CONCAT when missing should be skipped.
You practiced CONCAT(...) treating a missing input as an empty string — the right tool when a missing argument should be omitted from the concatenated output rather than poisoning it.