TRIM, LTRIM, RTRIM in MySQL - Time & Space Complexity
When we use TRIM, LTRIM, or RTRIM in MySQL, we want to remove spaces from text.
We ask: How does the time to do this change as the text gets longer?
Analyze the time complexity of the following code snippet.
SELECT TRIM(BOTH ' ' FROM column_name) AS trimmed_text
FROM example_table;
SELECT LTRIM(column_name) AS left_trimmed
FROM example_table;
SELECT RTRIM(column_name) AS right_trimmed
FROM example_table;
This code removes spaces from both sides, left side, or right side of text in a column.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each character of the text string to find spaces to remove.
- How many times: Once per character in the string, from left or right depending on the function.
As the text gets longer, the function checks more characters to trim spaces.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the length of the text.
Time Complexity: O(n)
This means the time to trim spaces grows linearly with the length of the text.
[X] Wrong: "TRIM functions remove spaces instantly no matter the text length."
[OK] Correct: The function must check characters one by one, so longer text takes more time.
Understanding how string functions scale helps you write efficient queries and explain performance clearly.
"What if we used TRIM to remove a different character instead of space? How would the time complexity change?"
Practice
TRIM function do in MySQL?Solution
Step 1: Understand the purpose of TRIM
The TRIM function is designed to remove unwanted spaces or characters from the start and end of a string.Step 2: Compare with LTRIM and RTRIM
LTRIM removes only from the left, RTRIM only from the right, but TRIM does both ends.Final Answer:
Removes spaces or specified characters from both ends of a string -> Option AQuick Check:
TRIM = both ends removal [OK]
- Confusing TRIM with LTRIM or RTRIM
- Thinking TRIM removes spaces inside the string
- Assuming TRIM only removes spaces from one side
' hello' in MySQL?Solution
Step 1: Identify the function for left-side trimming
LTRIM is the MySQL function that removes spaces from the start (left side) of a string.Step 2: Check other options
RTRIM removes spaces from the right side, TRIM removes from both ends, and REMOVE_LEFT is not a valid MySQL function.Final Answer:
LTRIM(' hello') -> Option BQuick Check:
LTRIM = left side spaces removed [OK]
- Using RTRIM instead of LTRIM
- Using TRIM when only left spaces needed
- Using non-existent functions like REMOVE_LEFT
SELECT CONCAT('[', TRIM(' abc '), ']') AS result;Solution
Step 1: Understand TRIM effect on the string
TRIM removes spaces from both the start and end of ' abc ', resulting in 'abc'.Step 2: Apply CONCAT with brackets
Concatenating '[' + 'abc' + ']' results in '[abc]'.Final Answer:
[abc] -> Option AQuick Check:
TRIM removes spaces both ends = '[abc]' [OK]
- Thinking TRIM removes spaces only from one side
- Ignoring spaces inside the string
- Confusing CONCAT output formatting
SELECT LTRIM(' test') FROM dual WHERE RTRIM('test ') = 'test';Solution
Step 1: Check the use of FROM dual in MySQL
MySQL does not require or support the FROM dual syntax; it is used in Oracle.Step 2: Verify function usage and WHERE clause
LTRIM and RTRIM are valid functions and can be used in WHERE clauses in MySQL.Final Answer:
Using FROM dual is invalid in MySQL -> Option DQuick Check:
FROM dual is Oracle-specific, not MySQL [OK]
- Assuming FROM dual works in MySQL
- Thinking RTRIM can't be in WHERE clause
- Misspelling function names
users with a column username that may have extra spaces at the start or end. Which query correctly updates the table to remove all leading and trailing spaces from username?Solution
Step 1: Understand how to remove spaces from both ends
TRIM removes spaces from both the start and end of a string in one call.Step 2: Check LTRIM and RTRIM options
LTRIM removes only leading spaces, RTRIM only trailing spaces. Neither handles both ends alone.Step 3: Check REPLACE option
REPLACE removes all spaces inside the string, not just at ends, which is incorrect here.Final Answer:
UPDATE users SET username = TRIM(username); -> Option CQuick Check:
TRIM cleans both ends simply [OK]
- Using LTRIM which only removes leading spaces
- Using RTRIM which only removes trailing spaces
- Using REPLACE which removes all spaces
Ready to prove it?
Pass the test and this pattern moves unknown
