0
0
PostgreSQLquery~20 mins

String length and position functions in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Length and Position Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
AError
B12
C13
D14
Attempts:
2 left
💡 Hint
The LENGTH function counts all characters including punctuation and spaces.
query_result
intermediate
2:00remaining
Find position of a substring
What is the result of this query?
PostgreSQL
SELECT POSITION('cat' IN 'concatenate') AS pos;
A4
B3
C1
D0
Attempts:
2 left
💡 Hint
POSITION returns the starting index of the substring if found, else 0.
📝 Syntax
advanced
2: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?
ASELECT LENGTH 'test string';
BSELECT LENGTH('test string');
CSELECT LENGTH('test string') AS len;
DSELECT LENGTH('test string')
Attempts:
2 left
💡 Hint
Check if the function call syntax is correct with parentheses.
query_result
advanced
2:00remaining
Find position of substring not present
What does this query return?
PostgreSQL
SELECT POSITION('dog' IN 'concatenate') AS pos;
A1
B0
CNULL
DError
Attempts:
2 left
💡 Hint
POSITION returns 0 if substring is not found.
optimization
expert
2:00remaining
Optimize query to find length of trimmed string
Which query efficiently returns the length of a string after removing spaces from both ends?
ASELECT LENGTH(REPLACE(' example ', ' ', '')) AS len;
BSELECT LENGTH(' example ') - 2 AS len;
CSELECT LENGTH(LTRIM(RTRIM(' example '))) AS len;
DSELECT LENGTH(TRIM(' example ')) AS len;
Attempts:
2 left
💡 Hint
TRIM removes spaces from both ends in one step.