Concept Flow - CONCAT and CONCAT_WS
Start with strings
Apply CONCAT or CONCAT_WS
Check each argument
Join strings
Return combined string
The functions take strings, check each for NULL, then join them into one string, returning the combined result.
SELECT CONCAT('Hello', ' ', 'World'); SELECT CONCAT_WS('-', '2024', NULL, '06', '15');
| Step | Function | Arguments | NULL Handling | Result |
|---|---|---|---|---|
| 1 | CONCAT | 'Hello', ' ', 'World' | No NULLs, all included | 'Hello World' |
| 2 | CONCAT_WS | '-', '2024', NULL, '06', '15' | NULL skipped | '2024-06-15' |
| 3 | CONCAT | 'A', NULL, 'B' | NULL treated as empty string | 'AB' |
| 4 | CONCAT_WS | ',' , NULL, 'X', NULL, 'Y' | NULL skipped | 'X,Y' |
| 5 | CONCAT_WS | NULL, 'X', 'Y' | Separator NULL returns NULL | NULL |
| 6 | CONCAT | NULL, NULL | NULL treated as empty strings | '' |
| 7 | CONCAT_WS | '-', NULL, NULL | All NULLs skipped, no strings | '' |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | After Step 4 | After Step 5 | After Step 6 | After Step 7 |
|---|---|---|---|---|---|---|---|---|
| Result String | '' | 'Hello World' | '2024-06-15' | 'AB' | 'X,Y' | NULL | '' | '' |
CONCAT(str1, str2, ...) joins all strings, treating NULL as empty.
CONCAT_WS(separator, str1, str2, ...) joins strings with separator, skipping NULLs.
If separator is NULL in CONCAT_WS, result is NULL.
Useful for combining columns or literals into one string.
NULL handling differs: CONCAT includes empty, CONCAT_WS skips.
Syntax: CONCAT('a', NULL, 'b') -> 'ab'; CONCAT_WS('-', 'a', NULL, 'b') -> 'a-b'.CONCAT and CONCAT_WS in MySQL?CONCAT behaviorCONCAT joins strings directly but returns NULL if any input is NULL.CONCAT_WS behaviorCONCAT_WS joins strings with a separator and ignores NULL values instead of returning NULL.CONCAT returns NULL if any argument is NULL; CONCAT_WS skips NULLs and uses a separator. -> Option ACONCAT returns NULL if any argument is NULL; CONCAT_WS skips NULLs and uses a separator. [OK]CONCAT_WS?CONCAT_WS syntaxCONCAT_WS takes the separator as the first argument, then the strings to join.users with columns first_name, middle_name, and last_name, what is the output of this query?SELECT CONCAT_WS(' ', first_name, middle_name, last_name) AS full_name FROM users WHERE id = 1;id=1 has first_name='John', middle_name=NULL, and last_name='Doe'.CONCAT_WS with NULL valuesCONCAT_WS skips NULL values and joins remaining strings with the separator.middle_name is NULL, it is skipped. The result joins 'John' and 'Doe' with a space.SELECT CONCAT('Name: ', first_name, middle_name, last_name) AS full_name FROM users;CONCAT returns NULL if any argument is NULL.CONCAT on columns that may contain NULL, so the entire result for full_name will be NULL if any column is NULL.street, city, and zipcode in a table, separated by commas. Some rows have NULL in zipcode. Which query correctly creates the address without extra commas for NULL values?CONCAT_WS joins strings with a separator and skips NULL values, avoiding extra separators.CONCAT_WS with ', ' separator and all columns, so NULL zipcode is skipped without extra commas. SELECT CONCAT(street, ', ', city, ', ', zipcode) AS full_address FROM addresses; uses CONCAT which returns NULL if any argument is NULL, causing missing addresses. SELECT CONCAT_WS(', ', street, city) || ', ' || zipcode AS full_address FROM addresses; uses invalid syntax with || operator in MySQL. SELECT CONCAT(street, city, zipcode) AS full_address FROM addresses; concatenates without separators and returns NULL if any NULL present.Pass the test and this pattern moves unknown