SUBSTRING and LEFT/RIGHT in MySQL - Time & Space Complexity
When working with text data in databases, we often extract parts of strings using functions like SUBSTRING, LEFT, and RIGHT.
We want to understand how the time it takes to run these functions changes as the size of the input text grows.
Analyze the time complexity of the following code snippet.
SELECT
SUBSTRING(description, 1, 10) AS first_ten_chars,
LEFT(description, 5) AS first_five_chars,
RIGHT(description, 5) AS last_five_chars
FROM products;
This code extracts parts of the 'description' text from each product: the first 10 characters, the first 5 characters, and the last 5 characters.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Extracting a substring from each string in the 'description' column.
- How many times: Once per row in the 'products' table.
Extracting a fixed-length substring takes time proportional to the length of the substring, not the full string.
| Input Size (length of description) | Approx. Operations per row |
|---|---|
| 10 | About 10 operations (extracting 10 chars) |
| 100 | Still about 10 operations (only first 10 chars extracted) |
| 1000 | Still about 10 operations (fixed substring length) |
Pattern observation: The time to extract a fixed substring stays about the same no matter how long the full string is.
Time Complexity: O(k * n)
This means the time grows linearly with the number of rows (n), and with the fixed substring length (k), which is usually small and constant.
[X] Wrong: "Extracting a substring takes time proportional to the full string length."
[OK] Correct: Since we only extract a fixed number of characters, the operation depends on that fixed length, not the entire string size.
Understanding how string functions scale helps you write efficient queries and explain performance in interviews confidently.
"What if we extract a substring whose length depends on the full string length? How would the time complexity change?"
Practice
LEFT('Hello World', 5) return?Solution
Step 1: Understand LEFT function
The LEFT function extracts a specified number of characters from the start (left side) of a string.Step 2: Apply LEFT to 'Hello World'
Extracting 5 characters from the start of 'Hello World' gives 'Hello'.Final Answer:
'Hello' -> Option BQuick Check:
LEFT('Hello World', 5) = 'Hello' [OK]
- Confusing LEFT with RIGHT function
- Expecting the whole string instead of part
- Using wrong number of characters
Solution
Step 1: Recall SUBSTRING syntax
SUBSTRING(string, start_position, length) extracts length characters starting at start_position.Step 2: Check each option
SUBSTRING('abcdef', 2, 3) uses SUBSTRING with start=2 and length=3, which is correct. LEFT and RIGHT do not accept three arguments.Final Answer:
SUBSTRING('abcdef', 2, 3) -> Option CQuick Check:
Correct SUBSTRING syntax = SUBSTRING('abcdef', 2, 3) [OK]
- Using LEFT or RIGHT with three arguments
- Swapping start and length values
- Starting count from zero instead of one
SELECT SUBSTRING('Database', 5, 3) AS part1, LEFT('Database', 4) AS part2, RIGHT('Database', 2) AS part3;Solution
Step 1: Calculate SUBSTRING('Database', 5, 3)
Starting at position 5: 'D'(1), 'a'(2), 't'(3), 'a'(4), 'b'(5), 'a'(6), 's'(7), 'e'(8). Characters 5 to 7 are 'b', 'a', 's' -> 'bas'.Step 2: Calculate LEFT('Database', 4) and RIGHT('Database', 2)
LEFT takes first 4 chars: 'D', 'a', 't', 'a' -> 'Data'. RIGHT takes last 2 chars: 's', 'e' -> 'se'.Final Answer:
part1='bas', part2='Data', part3='se' -> Option AQuick Check:
SUBSTRING, LEFT, RIGHT results match part1='bas', part2='Data', part3='se' [OK]
- Counting start position from zero
- Mixing up LEFT and RIGHT results
- Extracting wrong substring length
SELECT LEFT('Example', 3, 2) AS result;Solution
Step 1: Check LEFT function syntax
LEFT(string, length) accepts only two arguments: the string and number of characters from the left.Step 2: Analyze the query
The query uses three arguments in LEFT, which is invalid syntax and causes an error.Final Answer:
LEFT function does not accept three arguments -> Option AQuick Check:
LEFT syntax error = LEFT function does not accept three arguments [OK]
- Adding extra arguments to LEFT or RIGHT
- Confusing LEFT with SUBSTRING syntax
- Ignoring syntax errors in function calls
full_name with values like 'John Smith'. Which query extracts the last 5 characters of full_name to get the last name part using MySQL functions?Solution
Step 1: Understand the requirement
We want the last 5 characters of the full_name, which is the last name part 'Smith' in 'John Smith'.Step 2: Choose the correct function
RIGHT(string, length) extracts characters from the end. RIGHT(full_name, 5) returns last 5 characters.Step 3: Check other options
LEFT extracts from start, SUBSTRING with negative start is invalid in MySQL, and SUBSTRING with fixed start 6 may fail if names vary.Final Answer:
SELECT RIGHT(full_name, 5) AS last_name FROM users; -> Option DQuick Check:
RIGHT gets last characters = SELECT RIGHT(full_name, 5) AS last_name FROM users; [OK]
- Using LEFT instead of RIGHT for last characters
- Using SUBSTRING with negative start (not supported)
- Assuming fixed position for last name
Ready to prove it?
Pass the test and this pattern moves unknown
