Bird
Raised Fist0
PostgreSQLquery~20 mins

TRIM, LTRIM, RTRIM variations in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Trim Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of TRIM with both leading and trailing characters
What is the output of the following SQL query?

SELECT TRIM(BOTH 'x' FROM 'xxhelloxx');
PostgreSQL
SELECT TRIM(BOTH 'x' FROM 'xxhelloxx');
A'hello'
B'helloxx'
C'xxhelloxx'
D'xxhello'
Attempts:
2 left
💡 Hint
TRIM removes specified characters from both ends of the string.
❓ query_result
intermediate
2:00remaining
Result of LTRIM removing multiple characters
What is the result of this query?

SELECT LTRIM('abc123abc', 'abc');
PostgreSQL
SELECT LTRIM('abc123abc', 'abc');
A'123abc'
B'bc123abc'
C'123'
D'abc123abc'
Attempts:
2 left
💡 Hint
LTRIM removes all characters in the set from the start until a character not in the set is found.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in RTRIM usage
Which option contains a syntax error when trying to remove trailing spaces using RTRIM in PostgreSQL?
ASELECT RTRIM(' hello ', ' ');
BSELECT RTRIM(' hello ', '');
CSELECT RTRIM(' hello ');
DSELECT RTRIM(' hello ', ' ');
Attempts:
2 left
💡 Hint
The characters to remove cannot be an empty string.
❓ query_result
advanced
2:00remaining
Effect of TRIM with no characters specified
What is the output of this query?

SELECT TRIM(' hello ');

Note: No characters specified after TRIM.
PostgreSQL
SELECT TRIM('  hello  ');
A'hello '
B' hello '
C'hello'
D' hello'
Attempts:
2 left
💡 Hint
When no characters are specified, TRIM removes spaces from both ends.
🧠 Conceptual
expert
3:00remaining
Understanding TRIM variations with multiple characters
Given the string 'xyxzyhellozzyx', which TRIM function call will remove all leading and trailing 'x', 'y', and 'z' characters?

Choose the correct query.
ASELECT TRIM(TRAILING 'xyz' FROM 'xyxzyhellozzyx');
BSELECT TRIM(BOTH 'xzy' FROM 'xyxzyhellozzyx');
CSELECT TRIM(LEADING 'xyz' FROM 'xyxzyhellozzyx');
DSELECT TRIM(BOTH 'xyz' FROM 'xyxzyhellozzyx');
Attempts:
2 left
💡 Hint
The order of characters in the trim set does not matter; BOTH removes from both ends.

Practice

(1/5)
1. What does the TRIM function do by default in PostgreSQL?
easy
A. Removes spaces only from the start of a string
B. Removes all spaces inside the string
C. Removes spaces only from the end of a string
D. Removes spaces from both the start and end of a string

Solution

  1. Step 1: Understand the TRIM function behavior

    The TRIM function removes unwanted characters from a string. By default, it removes spaces.
  2. Step 2: Identify which sides are affected

    By default, TRIM removes spaces from both the start (left) and end (right) of the string.
  3. Final Answer:

    Removes spaces from both the start and end of a string -> Option D
  4. Quick Check:

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

Solution

  1. Step 1: Identify function for leading spaces

    LTRIM removes spaces only from the start (left side) of the string.
  2. Step 2: Check syntax correctness

    SELECT LTRIM(' hello'); correctly removes leading spaces. RTRIM removes trailing spaces, TRIM removes both sides, and SELECT TRIM(LEADING ' hello'); uses invalid syntax (missing FROM).
  3. Final Answer:

    SELECT LTRIM(' hello'); -> Option C
  4. Quick Check:

    LTRIM removes leading spaces = A [OK]
Hint: Use LTRIM to remove spaces only from the start [OK]
Common Mistakes:
  • Using RTRIM instead of LTRIM for leading spaces
  • Confusing TRIM default with LTRIM
  • Using incorrect syntax like TRIM(' hello') without LEADING
3. What is the output of this query?
SELECT TRIM(BOTH 'x' FROM 'xxhelloxx');
medium
A. 'xxhello'
B. 'hello'
C. 'helloxx'
D. 'xxhelloxx'

Solution

  1. Step 1: Understand TRIM with BOTH and character

    TRIM(BOTH 'x' FROM string) removes all 'x' characters from both start and end of the string.
  2. Step 2: Apply trimming to 'xxhelloxx'

    Removing 'x' from start removes 'xx', from end removes 'xx', leaving 'hello'.
  3. Final Answer:

    'hello' -> Option B
  4. Quick Check:

    TRIM BOTH 'x' removes 'xx' both ends = A [OK]
Hint: TRIM(BOTH 'char' FROM str) removes char from both ends [OK]
Common Mistakes:
  • Removing characters only from one side
  • Assuming TRIM removes characters inside the string
  • Confusing default TRIM with TRIM(BOTH 'char' FROM ...)
4. Identify the error in this query:
SELECT LTRIM('hello  ', ' ');
medium
A. The query is correct and will remove leading spaces
B. LTRIM removes leading spaces, so it should be RTRIM
C. The second argument should be a string of characters to remove, but space is valid
D. LTRIM cannot take a second argument

Solution

  1. Step 1: Check LTRIM function signature

    LTRIM(string, characters) removes any characters in the second argument from the start of the string.
  2. Step 2: Analyze the query behavior

    The query tries to remove spaces from the start of 'hello '. Since 'hello ' has no leading spaces, nothing is removed. The second argument ' ' is valid.
  3. Step 3: Identify the error

    There is no syntax error, and the query is valid. It removes leading spaces if any exist. To remove trailing spaces, RTRIM should be used, but this query is correct for leading spaces.
  4. Final Answer:

    The query is correct and will remove leading spaces -> Option A
  5. Quick Check:

    LTRIM second arg valid, removes leading spaces if present = D [OK]
Hint: LTRIM removes leading chars; trailing spaces need RTRIM [OK]
Common Mistakes:
  • Thinking LTRIM removes trailing spaces
  • Believing second argument is invalid
  • Expecting spaces removed from end with LTRIM
5. Given a table users with a column username containing values like ' alice ', 'bob ', and ' carol', which query correctly removes all leading and trailing spaces from usernames?
hard
A. SELECT TRIM(username) FROM users;
B. SELECT LTRIM(username) FROM users;
C. SELECT RTRIM(username) FROM users;
D. SELECT TRIM(LEADING FROM username) FROM users;

Solution

  1. Step 1: Understand the goal

    We want to remove spaces from both the start and end of the username strings.
  2. Step 2: Check each function's behavior

    TRIM removes spaces from both ends. LTRIM removes only leading spaces. RTRIM removes only trailing spaces. TRIM(LEADING FROM ...) removes only leading spaces.
  3. Final Answer:

    SELECT TRIM(username) FROM users; -> Option A
  4. Quick Check:

    TRIM removes spaces both ends = D [OK]
Hint: Use TRIM to clean spaces from both ends in one step [OK]
Common Mistakes:
  • Using LTRIM or RTRIM alone to remove both sides
  • Using TRIM(LEADING FROM ...) which removes only start spaces
  • Not trimming spaces at all
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