Challenge - 5 Problems
String Length and Position Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find the length of a string
What is the output of the following SQL query in PostgreSQL?
PostgreSQL
SELECT LENGTH('Hello, World!') AS length;
Attempts:
2 left
💡 Hint
The LENGTH function counts all characters including punctuation and spaces.
✗ Incorrect
The string 'Hello, World!' has 13 characters including the comma and space.
❓ query_result
intermediate2:00remaining
Find position of a substring
What is the result of this query?
PostgreSQL
SELECT POSITION('cat' IN 'concatenate') AS pos;
Attempts:
2 left
💡 Hint
POSITION returns the starting index of the substring if found, else 0.
✗ Incorrect
The substring 'cat' starts at the 4th character in 'concatenate'.
📝 Syntax
advanced2:00remaining
Identify the syntax error in string length query
Which option will cause a syntax error when trying to get the length of a string in PostgreSQL?
Attempts:
2 left
💡 Hint
Check if the function call syntax is correct with parentheses.
✗ Incorrect
Option A misses parentheses around the string argument, causing syntax error.
❓ query_result
advanced2:00remaining
Find position of substring not present
What does this query return?
PostgreSQL
SELECT POSITION('dog' IN 'concatenate') AS pos;
Attempts:
2 left
💡 Hint
POSITION returns 0 if substring is not found.
✗ Incorrect
Since 'dog' is not in 'concatenate', POSITION returns 0.
❓ optimization
expert2:00remaining
Optimize query to find length of trimmed string
Which query efficiently returns the length of a string after removing spaces from both ends?
Attempts:
2 left
💡 Hint
TRIM removes spaces from both ends in one step.
✗ Incorrect
Option D uses TRIM which removes spaces from both ends efficiently and then measures length.