CONCAT and CONCAT_WS in SQL - Time & Space Complexity
We want to understand how the time it takes to join strings grows as we add more strings.
How does the work change when we use CONCAT or CONCAT_WS with more inputs?
Analyze the time complexity of the following SQL snippet.
SELECT CONCAT(col1, col2, col3, col4) AS combined
FROM my_table;
SELECT CONCAT_WS('-', col1, col2, col3, col4) AS combined_with_sep
FROM my_table;
This code joins multiple columns into one string, either directly or with a separator.
Look at what repeats when joining strings.
- Primary operation: Combining each string piece one by one.
- How many times: Once for each string argument in the function.
As you add more strings, the work to join them grows roughly in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 string joins |
| 100 | About 100 string joins |
| 1000 | About 1000 string joins |
Pattern observation: The time grows steadily as you add more strings to join.
Time Complexity: O(n)
This means the time to join strings grows directly with the number of strings you combine.
[X] Wrong: "Joining many strings happens instantly no matter how many there are."
[OK] Correct: Each string adds a little more work, so more strings mean more time.
Knowing how string joining scales helps you write efficient queries and understand performance in real projects.
"What if we used CONCAT_WS with a very long separator string? How would that affect the time complexity?"
Practice
CONCAT and CONCAT_WS in SQL?Solution
Step 1: Understand
CONCATbehaviorCONCATjoins text parts directly without any separator.Step 2: Understand
CONCAT_WSbehaviorCONCAT_WSjoins text parts but inserts a separator you specify between them.Final Answer:
CONCATjoins strings directly;CONCAT_WSadds a separator between strings. -> Option AQuick Check:
Difference = separator usage [OK]
- Thinking CONCAT_WS adds no separator
- Confusing which function adds separators
- Assuming CONCAT works only on numbers
first_name and last_name with a space using CONCAT_WS?Solution
Step 1: Recall
The first argument is the separator string, followed by the columns to join.CONCAT_WSsyntaxStep 2: Match syntax to options
CONCAT_WS(' ', first_name, last_name) correctly uses a space as separator and then the two columns.Final Answer:
CONCAT_WS(' ', first_name, last_name) -> Option CQuick Check:
Separator first, then columns [OK]
- Putting separator last instead of first
- Using operators inside CONCAT_WS arguments
- Passing concatenated string as single argument
users with columns city = 'Paris', country = 'France', what is the result of this query?SELECT CONCAT(city, ', ', country) AS location FROM users;
Solution
Step 1: Understand CONCAT arguments
CONCAT joins city, then the string ', ', then country exactly as given.Step 2: Combine values
city = 'Paris', separator = ', ', country = 'France' results in 'Paris, France'.Final Answer:
'Paris, France' -> Option AQuick Check:
CONCAT joins exactly as given [OK]
- Ignoring spaces inside separator string
- Assuming CONCAT adds spaces automatically
- Confusing CONCAT_WS with CONCAT
SELECT CONCAT_WS(first_name, ' ', last_name) FROM employees;
Solution
Step 1: Check CONCAT_WS syntax
First argument must be the separator string, but here first_name is used instead.Step 2: Identify correct usage
Separator should be a string like ' ', then columns follow.Final Answer:
Missing separator argument as first parameter -> Option BQuick Check:
Separator must be first argument [OK]
- Putting column name as separator
- Swapping argument order
- Assuming CONCAT_WS works like CONCAT
street, city, and zipcode separated by commas. Which query correctly uses CONCAT_WS to achieve this, and what will be the output if street = '123 Main St', city = NULL, zipcode = '90210'?Solution
Step 1: Understand CONCAT_WS behavior with NULLs
CONCAT_WS skips NULL values and does not add extra separators for them.Step 2: Apply to given data
With city = NULL, CONCAT_WS(', ', street, city, zipcode) returns '123 Main St, 90210' without extra commas.Final Answer:
SELECT CONCAT_WS(', ', street, city, zipcode) AS address FROM table; Output: '123 Main St, 90210' -> Option DQuick Check:
CONCAT_WS skips NULLs cleanly [OK]
- Expecting NULL to appear as text
- Assuming CONCAT_WS adds separators for NULLs
- Using CONCAT which does not skip NULLs
Ready to prove it?
Pass the test and this pattern moves unknown
