Bird
Raised Fist0
PostgreSQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the PostgreSQL substring function do when given a string and a start position?
easy
A. Returns the length of the string
B. Extracts part of the string starting from the given position to the end
C. Replaces part of the string starting from the given position
D. Converts the string to uppercase

Solution

  1. Step 1: Understand substring function purpose

    The substring function extracts a portion of a string starting at a specified position.
  2. Step 2: Analyze given parameters

    When only a start position is given, substring returns from that position to the end of the string.
  3. Final Answer:

    Extracts part of the string starting from the given position to the end -> Option B
  4. Quick Check:

    substring extracts substring from position [OK]
Hint: Substring cuts string from start position to end if length missing [OK]
Common Mistakes:
  • Confusing substring with overlay
  • Thinking substring replaces text
  • Assuming substring returns string length
2. Which of the following is the correct syntax to replace part of a string using the overlay function in PostgreSQL?
easy
A. overlay(string placing new_substring from start for length)
B. overlay(string from start for length placing new_substring)
C. overlay(string for length from start placing new_substring)
D. overlay(new_substring placing string from start for length)

Solution

  1. Step 1: Recall overlay syntax

    The correct syntax is overlay(string placing new_substring from start for length).
  2. Step 2: Match options to syntax

    overlay(string placing new_substring from start for length) matches the correct order: string placing new_substring from start for length.
  3. Final Answer:

    overlay(string placing new_substring from start for length) -> Option A
  4. Quick Check:

    Overlay syntax = string placing new_substring from start for length [OK]
Hint: Remember: overlay(string placing new from start for length) [OK]
Common Mistakes:
  • Mixing order of 'from' and 'placing'
  • Omitting 'for length' keyword
  • Swapping string and new_substring positions
3. What is the result of this query?
SELECT overlay('abcdef' placing 'XYZ' from 2 for 3);
medium
A. 'abXYZf'
B. 'aXYZdef'
C. 'XYZdef'
D. 'aXYZef'

Solution

  1. Step 1: Understand overlay parameters

    We replace 3 characters starting at position 2 in 'abcdef' with 'XYZ'. Positions start at 1.
  2. Step 2: Apply replacement

    Characters at positions 2,3,4 ('b','c','d') are replaced by 'XYZ', resulting in 'aXYZef'.
  3. Final Answer:

    'aXYZef' -> Option D
  4. Quick Check:

    Overlay replaces substring from pos 2 length 3 with 'XYZ' [OK]
Hint: Overlay replaces exact length starting at position [OK]
Common Mistakes:
  • Replacing wrong substring length
  • Starting replacement at wrong position
  • Appending instead of replacing
4. Identify the error in this query:
SELECT overlay('hello' placing 'y' for 2 from 3);
medium
A. String literals not quoted
B. Missing length parameter
C. Incorrect order of 'for' and 'from' keywords
D. Overlay function does not exist

Solution

  1. Step 1: Check overlay syntax order

    The correct syntax is overlay(string placing new_substring from start for length). The query uses 'placing' then 'for' then 'from', which is wrong order.
  2. Step 2: Identify error cause

    The keywords 'for' and 'from' are swapped, causing syntax error.
  3. Final Answer:

    Incorrect order of 'for' and 'from' keywords -> Option C
  4. Quick Check:

    Overlay syntax order matters: from then for then placing [OK]
Hint: Overlay syntax: from before for before placing [OK]
Common Mistakes:
  • Swapping 'for' and 'from' keywords
  • Omitting quotes around strings
  • Using overlay without required keywords
5. Given the string 'PostgreSQL', which query correctly replaces 'gre' with 'XYZ' using overlay and then extracts the substring starting at position 5 with length 4?
hard
A. SELECT substring(overlay('PostgreSQL' placing 'XYZ' from 5 for 3) from 5 for 4);
B. SELECT substring(overlay('PostgreSQL' from 5 for 3 placing 'XYZ') from 5 for 4);
C. SELECT overlay(substring('PostgreSQL' from 5 for 3) placing 'XYZ' from 5 for 4);
D. SELECT overlay('PostgreSQL' from 5 for 4 placing 'XYZ');

Solution

  1. Step 1: Replace 'gre' with 'XYZ' using overlay

    'gre' starts at position 5 and has length 3. Correct overlay syntax is overlay(string placing new_substring from start for length). SELECT substring(overlay('PostgreSQL' placing 'XYZ' from 5 for 3) from 5 for 4); matches this.
  2. Step 2: Extract substring from position 5 length 4

    Apply substring on the result starting at position 5 for length 4. SELECT substring(overlay('PostgreSQL' placing 'XYZ' from 5 for 3) from 5 for 4); applies substring correctly after overlay.
  3. Final Answer:

    SELECT substring(overlay('PostgreSQL' placing 'XYZ' from 5 for 3) from 5 for 4); -> Option A
  4. Quick Check:

    Overlay then substring with correct syntax = SELECT substring(overlay('PostgreSQL' placing 'XYZ' from 5 for 3) from 5 for 4); [OK]
Hint: Overlay syntax: placing after string, then substring after overlay [OK]
Common Mistakes:
  • Mixing overlay keyword order
  • Applying substring before overlay
  • Wrong positions or lengths in functions
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