CONCAT and CONCAT_WS in MySQL - Time & Space Complexity
When using CONCAT and CONCAT_WS in MySQL, it's helpful to know how the time to combine strings grows as you add more parts.
We want to understand how the work changes when we join more pieces of text together.
Analyze the time complexity of the following code snippet.
SELECT CONCAT(first_name, ' ', last_name, ' - ', city) AS full_info
FROM users;
SELECT CONCAT_WS('-', country, state, city, street) AS address
FROM addresses;
This code joins several columns into one string for each row, either with CONCAT or CONCAT_WS.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Concatenating multiple string parts for each row.
- How many times: Once per row, joining all given parts in order.
As you add more parts to join, the work grows roughly in a straight line with the number of parts.
| Number of Parts (n) | Approx. Operations |
|---|---|
| 3 | 3 string joins per row |
| 5 | 5 string joins per row |
| 10 | 10 string joins per row |
Pattern observation: The time grows linearly as you add more parts to join.
Time Complexity: O(n)
This means the time to join strings grows in a straight line with the number of parts you combine.
[X] Wrong: "Adding more parts to CONCAT won't affect performance much because it's just joining strings."
[OK] Correct: Each additional part means more work to join, so time grows steadily with more parts.
Understanding how string joining scales helps you write efficient queries and explain your reasoning clearly in interviews.
"What if we used CONCAT_WS with a very long list of parts? How would the time complexity change compared to using CONCAT?"
Practice
CONCAT and CONCAT_WS in MySQL?Solution
Step 1: Understand
CONCATbehaviorCONCATjoins strings directly but returns NULL if any input is NULL.Step 2: Understand
CONCAT_WSbehaviorCONCAT_WSjoins strings with a separator and ignores NULL values instead of returning NULL.Final Answer:
CONCATreturns NULL if any argument is NULL;CONCAT_WSskips NULLs and uses a separator. -> Option AQuick Check:
Difference in NULL handling and separator use =CONCATreturns NULL if any argument is NULL;CONCAT_WSskips NULLs and uses a separator. [OK]
- Thinking CONCAT_WS adds no separator
- Assuming CONCAT_WS returns NULL if any argument is NULL
- Confusing which function skips NULLs
CONCAT_WS?Solution
Step 1: Identify
CONCAT_WSsyntaxCONCAT_WStakes the separator as the first argument, then the strings to join.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.Final Answer:
CONCAT_WS(' ', first_name, last_name) -> Option DQuick Check:
Separator first, then strings = CONCAT_WS(' ', first_name, last_name) [OK]
- Putting separator last instead of first
- Using + operator inside CONCAT_WS
- Passing concatenated string as single argument
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'.Solution
Step 1: Understand
CONCAT_WSwith NULL valuesCONCAT_WSskips NULL values and joins remaining strings with the separator.Step 2: Apply to given data
Sincemiddle_nameis NULL, it is skipped. The result joins 'John' and 'Doe' with a space.Final Answer:
'John Doe' -> Option BQuick Check:
NULL skipped, space separator used = 'John Doe' [OK]
- Expecting NULL to appear as text
- Assuming NULL causes entire result to be NULL
- Thinking NULL inserts extra spaces
SELECT CONCAT('Name: ', first_name, middle_name, last_name) AS full_name FROM users;Solution
Step 1: Understand CONCAT behavior with NULL
CONCATreturns NULL if any argument is NULL.Step 2: Analyze the query
The query usesCONCATon columns that may contain NULL, so the entire result for full_name will be NULL if any column is NULL.Final Answer:
The query will return NULL for full_name if any column is NULL. -> Option AQuick Check:
CONCAT returns NULL if any argument is NULL = The query will return NULL for full_name if any column is NULL. [OK]
- Thinking CONCAT throws syntax error with NULL
- Expecting NULL to appear as 'NULL' string
- Assuming CONCAT accepts unlimited arguments without NULL effect
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?Solution
Step 1: Understand handling of NULLs and separators
CONCAT_WSjoins strings with a separator and skips NULL values, avoiding extra separators.Step 2: Analyze each option
SELECT CONCAT_WS(', ', street, city, zipcode) AS full_address FROM addresses; usesCONCAT_WSwith ', ' separator and all columns, so NULL zipcode is skipped without extra commas. SELECT CONCAT(street, ', ', city, ', ', zipcode) AS full_address FROM addresses; usesCONCATwhich 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.Final Answer:
SELECT CONCAT_WS(', ', street, city, zipcode) AS full_address FROM addresses; -> Option CQuick Check:
Use CONCAT_WS to skip NULLs and add separators correctly = SELECT CONCAT_WS(', ', street, city, zipcode) AS full_address FROM addresses; [OK]
- Using CONCAT which returns NULL if any NULL argument
- Trying to concatenate with || which is not MySQL syntax
- Manually adding separators causing extra commas
Ready to prove it?
Pass the test and this pattern moves unknown
