Bird
Raised Fist0
MySQLquery~20 mins

TRIM, LTRIM, RTRIM in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Trim Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of TRIM function on padded string
What is the output of the following MySQL query?
SELECT TRIM('  Hello World  ') AS trimmed_text;
MySQL
SELECT TRIM('  Hello World  ') AS trimmed_text;
A' Hello World '
B'Hello World'
C'Hello World '
D' Hello World'
Attempts:
2 left
💡 Hint
TRIM removes spaces from both sides of the string.
❓ query_result
intermediate
2:00remaining
Output of LTRIM on string with leading spaces
What will this MySQL query return?
SELECT LTRIM('   DataBase') AS left_trimmed;
MySQL
SELECT LTRIM('   DataBase') AS left_trimmed;
A'DataBase'
B'DataBase '
C' DataBase'
D'Data Base'
Attempts:
2 left
💡 Hint
LTRIM removes spaces only from the left side.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in RTRIM usage
Which option contains a syntax error when using RTRIM in MySQL?
ASELECT RTRIM('Test String ') AS trimmed;
BSELECT RTRIM('Test String ') trimmed;
CSELECT RTRIM('Test String ') AS trimmed_string;
DSELECT RTRIM 'Test String ' AS trimmed;
Attempts:
2 left
💡 Hint
Check the function call syntax with parentheses.
❓ query_result
advanced
2:00remaining
Result of nested TRIM functions
What is the output of this query?
SELECT TRIM(TRAILING 'x' FROM LTRIM('xxxHello Worldxxx')) AS result;
MySQL
SELECT TRIM(TRAILING 'x' FROM LTRIM('xxxHello Worldxxx')) AS result;
A'xxxHello World'
B'Hello Worldxxx'
C'xxHello World'
D'Hello World'
Attempts:
2 left
💡 Hint
LTRIM removes spaces, not 'x'. TRIM with TRAILING removes 'x' only from the right side.
🧠 Conceptual
expert
2:00remaining
Understanding difference between TRIM, LTRIM, and RTRIM
Given the string ' SQL Query ', which function(s) will remove spaces only from the right side?
Choose the correct statement.
ATRIM removes spaces only from the right side.
BLTRIM removes spaces only from the right side.
CRTRIM removes spaces only from the right side.
DLTRIM and RTRIM both remove spaces only from the right side.
Attempts:
2 left
💡 Hint
Think about what each function does: left, right, or both sides.

Practice

(1/5)
1. What does the TRIM function do in MySQL?
easy
A. Removes spaces or specified characters from both ends of a string
B. Removes spaces only from the start of a string
C. Removes spaces only from the end of a string
D. Replaces spaces inside the string with underscores

Solution

  1. 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.
  2. Step 2: Compare with LTRIM and RTRIM

    LTRIM removes only from the left, RTRIM only from the right, but TRIM does both ends.
  3. Final Answer:

    Removes spaces or specified characters from both ends of a string -> Option A
  4. Quick Check:

    TRIM = both ends removal [OK]
Hint: TRIM cleans both sides of spaces or chars [OK]
Common Mistakes:
  • Confusing TRIM with LTRIM or RTRIM
  • Thinking TRIM removes spaces inside the string
  • Assuming TRIM only removes spaces from one side
2. Which of the following is the correct syntax to remove spaces from the left side of a string ' hello' in MySQL?
easy
A. RTRIM(' hello')
B. LTRIM(' hello')
C. TRIM(' hello')
D. REMOVE_LEFT(' hello')

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    LTRIM(' hello') -> Option B
  4. Quick Check:

    LTRIM = left side spaces removed [OK]
Hint: Use LTRIM() to remove left spaces [OK]
Common Mistakes:
  • Using RTRIM instead of LTRIM
  • Using TRIM when only left spaces needed
  • Using non-existent functions like REMOVE_LEFT
3. What is the output of this query?
SELECT CONCAT('[', TRIM(' abc '), ']') AS result;
medium
A. [abc]
B. [ abc ]
C. [abc ]
D. [ abc]

Solution

  1. Step 1: Understand TRIM effect on the string

    TRIM removes spaces from both the start and end of ' abc ', resulting in 'abc'.
  2. Step 2: Apply CONCAT with brackets

    Concatenating '[' + 'abc' + ']' results in '[abc]'.
  3. Final Answer:

    [abc] -> Option A
  4. Quick Check:

    TRIM removes spaces both ends = '[abc]' [OK]
Hint: TRIM removes spaces both ends, so output is '[abc]' [OK]
Common Mistakes:
  • Thinking TRIM removes spaces only from one side
  • Ignoring spaces inside the string
  • Confusing CONCAT output formatting
4. Identify the error in this query:
SELECT LTRIM(' test') FROM dual WHERE RTRIM('test ') = 'test';
medium
A. No error, query runs correctly
B. RTRIM cannot be used in WHERE clause
C. LTRIM and RTRIM functions are misspelled
D. Using FROM dual is invalid in MySQL

Solution

  1. 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.
  2. Step 2: Verify function usage and WHERE clause

    LTRIM and RTRIM are valid functions and can be used in WHERE clauses in MySQL.
  3. Final Answer:

    Using FROM dual is invalid in MySQL -> Option D
  4. Quick Check:

    FROM dual is Oracle-specific, not MySQL [OK]
Hint: MySQL does not use FROM dual [OK]
Common Mistakes:
  • Assuming FROM dual works in MySQL
  • Thinking RTRIM can't be in WHERE clause
  • Misspelling function names
5. You have a table 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?
hard
A. UPDATE users SET username = LTRIM(username);
B. UPDATE users SET username = RTRIM(username);
C. UPDATE users SET username = TRIM(username);
D. UPDATE users SET username = REPLACE(username, ' ', '');

Solution

  1. 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.
  2. Step 2: Check LTRIM and RTRIM options

    LTRIM removes only leading spaces, RTRIM only trailing spaces. Neither handles both ends alone.
  3. Step 3: Check REPLACE option

    REPLACE removes all spaces inside the string, not just at ends, which is incorrect here.
  4. Final Answer:

    UPDATE users SET username = TRIM(username); -> Option C
  5. Quick Check:

    TRIM cleans both ends simply [OK]
Hint: Use TRIM() to clean both ends in one step [OK]
Common Mistakes:
  • Using LTRIM which only removes leading spaces
  • Using RTRIM which only removes trailing spaces
  • Using REPLACE which removes all spaces
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