0
0
PostgreSQLquery~20 mins

Substring and overlay functions in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Substring and Overlay Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of SUBSTRING with position and length
What is the output of the following SQL query?

SELECT SUBSTRING('PostgreSQL Database', 5, 6);
PostgreSQL
SELECT SUBSTRING('PostgreSQL Database', 5, 6);
A'greSQL'
B'greSQL D'
C'greSQL '
D'SQL Da'
Attempts:
2 left
💡 Hint
Remember SUBSTRING(string, start_position, length) extracts length characters starting at start_position.
query_result
intermediate
2:00remaining
Output of OVERLAY replacing substring
What is the output of this SQL query?

SELECT OVERLAY('Hello World' PLACING 'SQL' FROM 7 FOR 5);
PostgreSQL
SELECT OVERLAY('Hello World' PLACING 'SQL' FROM 7 FOR 5);
A'Hello SQLld'
B'Hello SQLd'
C'Hello SQLrld'
D'Hello SQL'
Attempts:
2 left
💡 Hint
OVERLAY replaces a substring starting at position FROM for length FOR with the PLACING string.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in SUBSTRING usage
Which option contains a syntax error when using the SUBSTRING function in PostgreSQL?
ASELECT SUBSTRING('abcdef' FROM 2 TO 3);
BSELECT SUBSTRING('abcdef', 2, 3);
CSELECT SUBSTRING('abcdef' FROM 2 FOR 3);
DSELECT SUBSTRING('abcdef' FROM 2);
Attempts:
2 left
💡 Hint
Check the correct syntax for specifying length in SUBSTRING with FROM and FOR.
optimization
advanced
2:00remaining
Optimizing string replacement with OVERLAY
You want to replace the first 4 characters of 'Database' with 'Info'. Which query is the most efficient and correct?
ASELECT OVERLAY('Database' PLACING 'Info' FROM 1 FOR 5);
BSELECT OVERLAY('Database' PLACING 'Info' FROM 0 FOR 4);
CSELECT OVERLAY('Database' PLACING 'Info' FROM 1 FOR 4);
DSELECT OVERLAY('Database' PLACING 'Info' FROM 2 FOR 4);
Attempts:
2 left
💡 Hint
Positions in strings start at 1 in PostgreSQL.
🧠 Conceptual
expert
2:00remaining
Understanding behavior of SUBSTRING with negative start position
What happens when you run this query?

SELECT SUBSTRING('Example' FROM -3 FOR 2);
PostgreSQL
SELECT SUBSTRING('Example' FROM -3 FOR 2);
AReturns 'am'
BReturns an empty string
CRaises an error
DReturns 'Ex'
Attempts:
2 left
💡 Hint
Check how SUBSTRING handles negative start positions in PostgreSQL.