Bird
Raised Fist0
SQLquery~10 mins

TRIM, LTRIM, RTRIM in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - TRIM, LTRIM, RTRIM
Start with string
↓
Apply LTRIM?
Yes→Remove leading spaces
↓
Apply RTRIM?
Yes→Remove trailing spaces
↓
Apply TRIM?
Yes→Remove both sides spaces
↓
Return original string
↓
Output trimmed string
The flow shows starting with a string, then optionally removing spaces from the left (LTRIM), right (RTRIM), or both sides (TRIM), returning the cleaned string.
Execution Sample
SQL
SELECT TRIM('  hello  ') AS trimmed,
       LTRIM('  hello  ') AS left_trimmed,
       RTRIM('  hello  ') AS right_trimmed;
This query removes spaces from both sides, left side only, and right side only of the string ' hello '.
Execution Table
StepFunctionInput StringActionOutput String
1TRIM' hello 'Remove spaces from both sides'hello'
2LTRIM' hello 'Remove spaces from left side only'hello '
3RTRIM' hello 'Remove spaces from right side only' hello'
4EndAll functions appliedOutputs returned
💡 All trimming functions applied and outputs produced.
Variable Tracker
VariableStartAfter TRIMAfter LTRIMAfter RTRIM
string' hello ''hello''hello '' hello'
Key Moments - 2 Insights
Why does LTRIM only remove spaces on the left and not on the right?
LTRIM is designed to remove spaces only from the start (left side) of the string, as shown in execution_table row 2 where the right spaces remain intact.
What happens if the string has no spaces on one side?
The trimming function for that side does nothing, so the string remains unchanged on that side, as seen in execution_table row 3 where RTRIM removes only trailing spaces.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output of LTRIM on ' hello ' at step 2?
A'hello'
B'hello '
C' hello'
D' hello '
💡 Hint
Check the Output String column in row 2 of execution_table.
At which step does the function remove spaces from both sides?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the Function and Action columns in execution_table.
If the input string had no trailing spaces, what would RTRIM output be?
AString with left spaces removed
BString with both sides trimmed
CSame as input string
DEmpty string
💡 Hint
RTRIM only removes trailing spaces; if none exist, string stays the same (see key_moments explanation).
Concept Snapshot
TRIM removes spaces from both sides of a string.
LTRIM removes spaces only from the left side.
RTRIM removes spaces only from the right side.
Use SELECT TRIM(column), LTRIM(column), RTRIM(column) to clean strings.
Useful for cleaning user input or formatting data.
Full Transcript
This visual execution shows how SQL functions TRIM, LTRIM, and RTRIM work on a string with spaces. Starting with the string ' hello ', TRIM removes spaces from both sides resulting in 'hello'. LTRIM removes only the left spaces, keeping right spaces intact as 'hello '. RTRIM removes only the right spaces, keeping left spaces intact as ' hello'. The execution table tracks each step and output. The variable tracker shows how the string changes after each function. Key moments clarify common confusions about which side spaces are removed from. The quiz tests understanding by referencing the execution table and variable states. The snapshot summarizes usage and purpose of these functions.

Practice

(1/5)
1. What does the SQL function LTRIM do to a string?
easy
A. Removes all spaces inside the string
B. Removes spaces only from the end (right side) of the string
C. Removes spaces from both the start and end of the string
D. Removes spaces only from the start (left side) of the string

Solution

  1. Step 1: Understand LTRIM function purpose

    LTRIM removes spaces only from the left side (start) of a string.
  2. Step 2: Compare with other trim functions

    Unlike RTRIM (right side) or TRIM (both sides), LTRIM targets only the start.
  3. Final Answer:

    Removes spaces only from the start (left side) of the string -> Option D
  4. Quick Check:

    LTRIM = left side trim [OK]
Hint: LTRIM trims left side spaces only [OK]
Common Mistakes:
  • Confusing LTRIM with RTRIM
  • Thinking LTRIM removes spaces from both sides
  • Assuming LTRIM removes spaces inside the string
2. Which of the following is the correct syntax to remove trailing spaces from a column named name in SQL?
easy
A. SELECT TRIM(name) FROM table;
B. SELECT LTRIM(name) FROM table;
C. SELECT RTRIM(name) FROM table;
D. SELECT REMOVE_TRAIL(name) FROM table;

Solution

  1. Step 1: Identify function to remove trailing spaces

    RTRIM removes spaces from the right (end) side of a string.
  2. Step 2: Check syntax correctness

    SELECT RTRIM(name) FROM table; is valid SQL syntax for this purpose.
  3. Final Answer:

    SELECT RTRIM(name) FROM table; -> Option C
  4. Quick Check:

    RTRIM trims right side spaces [OK]
Hint: RTRIM removes trailing (right) spaces [OK]
Common Mistakes:
  • Using LTRIM instead of RTRIM for trailing spaces
  • Using TRIM which removes both sides, not just trailing
  • Using non-existent function REMOVE_TRAIL
3. Given the string value ' Hello World ', what will the result of SELECT TRIM(' Hello World '); be?
medium
A. 'Hello World'
B. ' Hello World'
C. 'Hello World '
D. 'HelloWorld'

Solution

  1. Step 1: Understand TRIM function behavior

    TRIM removes spaces from both the start and end of the string.
  2. Step 2: Apply TRIM to the given string

    Spaces before and after 'Hello World' are removed, but spaces inside remain.
  3. Final Answer:

    'Hello World' -> Option A
  4. Quick Check:

    TRIM removes spaces both sides [OK]
Hint: TRIM removes spaces both sides, not inside [OK]
Common Mistakes:
  • Expecting spaces inside the string to be removed
  • Confusing TRIM with LTRIM or RTRIM
  • Including quotes incorrectly in output
4. You wrote the query SELECT LTRIM(name) FROM users; but it does not remove spaces from the end of the names. What is the issue?
medium
A. The column name should be in quotes for LTRIM to work
B. LTRIM only removes spaces from the start, not the end
C. You should use TRIM to remove spaces only from the end
D. LTRIM removes spaces from both sides, so query is correct

Solution

  1. Step 1: Understand LTRIM function limitation

    LTRIM removes spaces only from the left (start) side of a string.
  2. Step 2: Identify why trailing spaces remain

    Spaces at the end (right side) are not removed by LTRIM, so they remain.
  3. Final Answer:

    LTRIM only removes spaces from the start, not the end -> Option B
  4. Quick Check:

    LTRIM trims left side only [OK]
Hint: LTRIM does not remove trailing spaces [OK]
Common Mistakes:
  • Assuming LTRIM removes spaces from both sides
  • Using quotes incorrectly around column names
  • Confusing TRIM and LTRIM functions
5. You have a table products with a column code that sometimes contains spaces at both ends. You want to update the table to remove all leading and trailing spaces from code. Which SQL statement correctly does this?
hard
A. UPDATE products SET code = TRIM(code);
B. UPDATE products SET code = LTRIM(code);
C. UPDATE products SET code = RTRIM(code);
D. UPDATE products SET code = REMOVE_SPACES(code);

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: Evaluate other options

    LTRIM removes only leading spaces, RTRIM only trailing spaces. Neither alone removes both sides.
  3. Step 3: Check for invalid functions

    REMOVE_SPACES is not a valid SQL function.
  4. Final Answer:

    UPDATE products SET code = TRIM(code); -> Option A
  5. Quick Check:

    TRIM removes both sides spaces [OK]
Hint: Use TRIM to clean both sides in one step [OK]
Common Mistakes:
  • Using LTRIM which removes only leading spaces
  • Using RTRIM which removes only trailing spaces
  • Using invalid function REMOVE_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