Bird
Raised Fist0
MySQLquery~10 mins

CONCAT and CONCAT_WS in MySQL - Step-by-Step Execution

Choose your learning style10 modes available
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.
Execution Sample
MySQL
SELECT CONCAT('Hello', ' ', 'World');
SELECT CONCAT_WS('-', '2024', NULL, '06', '15');
These queries join strings: CONCAT joins all including NULL as empty, CONCAT_WS joins with separator skipping NULLs.
Execution Table
StepFunctionArgumentsNULL HandlingResult
1CONCAT'Hello', ' ', 'World'No NULLs, all included'Hello World'
2CONCAT_WS'-', '2024', NULL, '06', '15'NULL skipped'2024-06-15'
3CONCAT'A', NULL, 'B'NULL treated as empty string'AB'
4CONCAT_WS',' , NULL, 'X', NULL, 'Y'NULL skipped'X,Y'
5CONCAT_WSNULL, 'X', 'Y'Separator NULL returns NULLNULL
6CONCATNULL, NULLNULL treated as empty strings''
7CONCAT_WS'-', NULL, NULLAll NULLs skipped, no strings''
💡 All arguments processed; NULLs handled differently by CONCAT and CONCAT_WS; final string returned.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7
Result String'''Hello World''2024-06-15''AB''X,Y'NULL''''
Key Moments - 3 Insights
Why does CONCAT_WS skip NULL values but CONCAT treats NULL as empty strings?
CONCAT_WS is designed to ignore NULLs to avoid extra separators, as shown in execution_table rows 2 and 4, while CONCAT treats NULL as empty strings to join all arguments directly, as in rows 1 and 3.
What happens if the separator in CONCAT_WS is NULL?
If the separator is NULL, CONCAT_WS returns NULL immediately without concatenation, as shown in execution_table row 5.
Why does CONCAT return an empty string when all arguments are NULL?
CONCAT treats each NULL as an empty string, so joining NULL and NULL results in an empty string, as shown in execution_table row 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 2. What is the output of CONCAT_WS('-', '2024', NULL, '06', '15')?
A'2024-NULL-06-15'
B'2024-06-15'
C'2024--06-15'
D'2024 06 15'
💡 Hint
Check how CONCAT_WS handles NULLs in the 'NULL Handling' column of row 2.
According to the execution_table, what does CONCAT('A', NULL, 'B') return?
A'AB'
B'A NULL B'
C'A B'
DNULL
💡 Hint
Look at row 3 and see how CONCAT treats NULL arguments.
In which step does CONCAT_WS return NULL because the separator is NULL?
AStep 4
BStep 6
CStep 5
DStep 7
💡 Hint
Check the 'Separator NULL returns NULL' note in the 'NULL Handling' column.
Concept Snapshot
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'.
Full Transcript
This lesson shows how CONCAT and CONCAT_WS work in MySQL. CONCAT joins all strings given, treating NULL as empty strings, so NULL does not stop the join. CONCAT_WS joins strings with a separator but skips any NULL values to avoid extra separators. If the separator itself is NULL in CONCAT_WS, the result is NULL. Examples show how these functions handle NULLs differently. This helps when combining text fields or literals in queries.

Practice

(1/5)
1. What is the main difference between CONCAT and CONCAT_WS in MySQL?
easy
A. CONCAT returns NULL if any argument is NULL; CONCAT_WS skips NULLs and uses a separator.
B. CONCAT adds a separator between strings; CONCAT_WS joins strings without separator.
C. CONCAT only works with numbers; CONCAT_WS only works with strings.
D. CONCAT converts all inputs to uppercase; CONCAT_WS converts to lowercase.

Solution

  1. Step 1: Understand CONCAT behavior

    CONCAT joins strings directly but returns NULL if any input is NULL.
  2. Step 2: Understand CONCAT_WS behavior

    CONCAT_WS joins strings with a separator and ignores NULL values instead of returning NULL.
  3. Final Answer:

    CONCAT returns NULL if any argument is NULL; CONCAT_WS skips NULLs and uses a separator. -> Option A
  4. Quick Check:

    Difference in NULL handling and separator use = CONCAT returns NULL if any argument is NULL; CONCAT_WS skips NULLs and uses a separator. [OK]
Hint: Remember: CONCAT returns NULL if any NULL; CONCAT_WS skips NULLs [OK]
Common Mistakes:
  • Thinking CONCAT_WS adds no separator
  • Assuming CONCAT_WS returns NULL if any argument is NULL
  • Confusing which function skips NULLs
2. Which of the following is the correct syntax to join first_name and last_name with a space using CONCAT_WS?
easy
A. CONCAT_WS(' ', first_name + last_name)
B. CONCAT_WS(first_name, last_name, ' ')
C. CONCAT_WS(first_name + ' ' + last_name)
D. CONCAT_WS(' ', first_name, last_name)

Solution

  1. Step 1: Identify CONCAT_WS syntax

    CONCAT_WS takes the separator as the first argument, then the strings to join.
  2. Step 2: Match correct argument order

    CONCAT_WS(' ', first_name, last_name) correctly uses a space as separator, then first_name and last_name as strings.
  3. Final Answer:

    CONCAT_WS(' ', first_name, last_name) -> Option D
  4. Quick Check:

    Separator first, then strings = CONCAT_WS(' ', first_name, last_name) [OK]
Hint: Separator is always first argument in CONCAT_WS [OK]
Common Mistakes:
  • Putting separator last instead of first
  • Using + operator inside CONCAT_WS
  • Passing concatenated string as single argument
3. Given a table 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;

Assuming the row with id=1 has first_name='John', middle_name=NULL, and last_name='Doe'.
medium
A. 'John NULL Doe'
B. 'John Doe'
C. 'John Doe'
D. NULL

Solution

  1. Step 1: Understand CONCAT_WS with NULL values

    CONCAT_WS skips NULL values and joins remaining strings with the separator.
  2. Step 2: Apply to given data

    Since middle_name is NULL, it is skipped. The result joins 'John' and 'Doe' with a space.
  3. Final Answer:

    'John Doe' -> Option B
  4. Quick Check:

    NULL skipped, space separator used = 'John Doe' [OK]
Hint: CONCAT_WS skips NULLs, so no extra spaces for NULL columns [OK]
Common Mistakes:
  • Expecting NULL to appear as text
  • Assuming NULL causes entire result to be NULL
  • Thinking NULL inserts extra spaces
4. Identify the error in this query:
SELECT CONCAT('Name: ', first_name, middle_name, last_name) AS full_name FROM users;
medium
A. The query will return NULL for full_name if any column is NULL.
B. The syntax is incorrect; CONCAT requires only two arguments.
C. NULL cannot be used inside CONCAT; it causes a syntax error.
D. The query will concatenate NULL as the string 'NULL'.

Solution

  1. Step 1: Understand CONCAT behavior with NULL

    CONCAT returns NULL if any argument is NULL.
  2. Step 2: Analyze the query

    The query uses CONCAT on columns that may contain NULL, so the entire result for full_name will be NULL if any column is NULL.
  3. Final Answer:

    The query will return NULL for full_name if any column is NULL. -> Option A
  4. Quick Check:

    CONCAT returns NULL if any argument is NULL = The query will return NULL for full_name if any column is NULL. [OK]
Hint: CONCAT returns NULL if any argument is NULL, no error [OK]
Common Mistakes:
  • Thinking CONCAT throws syntax error with NULL
  • Expecting NULL to appear as 'NULL' string
  • Assuming CONCAT accepts unlimited arguments without NULL effect
5. You want to create a full address string from columns 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?
hard
A. SELECT CONCAT_WS(', ', street, city) || ', ' || zipcode AS full_address FROM addresses;
B. SELECT CONCAT(street, ', ', city, ', ', zipcode) AS full_address FROM addresses;
C. SELECT CONCAT_WS(', ', street, city, zipcode) AS full_address FROM addresses;
D. SELECT CONCAT(street, city, zipcode) AS full_address FROM addresses;

Solution

  1. Step 1: Understand handling of NULLs and separators

    CONCAT_WS joins strings with a separator and skips NULL values, avoiding extra separators.
  2. Step 2: Analyze each option

    SELECT CONCAT_WS(', ', street, city, zipcode) AS full_address FROM addresses; uses 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.
  3. Final Answer:

    SELECT CONCAT_WS(', ', street, city, zipcode) AS full_address FROM addresses; -> Option C
  4. Quick Check:

    Use CONCAT_WS to skip NULLs and add separators correctly = SELECT CONCAT_WS(', ', street, city, zipcode) AS full_address FROM addresses; [OK]
Hint: Use CONCAT_WS with separator to skip NULLs and avoid extra commas [OK]
Common Mistakes:
  • Using CONCAT which returns NULL if any NULL argument
  • Trying to concatenate with || which is not MySQL syntax
  • Manually adding separators causing extra commas
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard