REPLACE function in SQL - Time & Space Complexity
We want to understand how the time it takes to run the REPLACE function changes as the size of the text grows.
How does the work inside REPLACE grow when the input string gets longer?
Analyze the time complexity of the following code snippet.
SELECT REPLACE(description, 'old', 'new') AS updated_description
FROM products;
This code replaces every occurrence of the word 'old' with 'new' in the description column for all products.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning each character of the description string to find matches of 'old'.
- How many times: Once for each character in the string, repeated for every row in the table.
As the length of the description grows, the time to scan and replace grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 characters | About 10 checks |
| 100 characters | About 100 checks |
| 1000 characters | About 1000 checks |
Pattern observation: The work grows linearly as the string gets longer.
Time Complexity: O(n)
This means the time to run REPLACE grows in a straight line with the length of the input string.
[X] Wrong: "REPLACE runs instantly no matter how long the string is."
[OK] Correct: The function must check each character to find matches, so longer strings take more time.
Understanding how string functions like REPLACE scale helps you write efficient queries and explain performance clearly.
"What if we replaced a longer substring instead of a short one? How would the time complexity change?"
Practice
REPLACE function do?Solution
Step 1: Understand the purpose of REPLACE
The REPLACE function is designed to find all instances of a substring in a string and swap them with another substring.Step 2: Compare with other string functions
Unlike deleting or extracting substrings, REPLACE changes the content by substitution.Final Answer:
It replaces all occurrences of a specified substring within a string with another substring. -> Option DQuick Check:
REPLACE swaps substrings = A [OK]
- Thinking REPLACE deletes text without substitution
- Confusing REPLACE with substring extraction
- Assuming REPLACE changes case of text
REPLACE function?Solution
Step 1: Recall REPLACE function parameter order
The correct order is the original string first, then the substring to replace, then the new substring.Step 2: Verify each option
Only REPLACE(string, old_substring, new_substring) follows the correct order: REPLACE(string, old_substring, new_substring).Final Answer:
REPLACE(string, old_substring, new_substring) -> Option AQuick Check:
Correct parameter order = D [OK]
- Swapping old and new substring positions
- Putting new substring before old substring
- Confusing parameter order
SELECT REPLACE('banana', 'a', 'o');What is the output?
Solution
Step 1: Identify the string and substrings
The original string is 'banana'. We replace all 'a' with 'o'.Step 2: Replace all 'a' with 'o'
Replacing each 'a' in 'banana' results in 'bonono'.Final Answer:
'bonono' -> Option CQuick Check:
All 'a' replaced by 'o' = 'bonono' [OK]
- Replacing only first occurrence
- Not replacing any character
- Removing characters instead of replacing
SELECT REPLACE('hello world', 'l');Solution
Step 1: Check REPLACE function parameters
REPLACE requires three parameters: original string, old substring, new substring.Step 2: Identify missing parameter
The query only has two parameters: original string and old substring. The new substring is missing.Final Answer:
It is missing the new substring to replace with. -> Option AQuick Check:
Missing third parameter = A [OK]
- Using only two parameters
- Swapping parameter order
- Assuming REPLACE works with one substring only
products with a column description containing the text 'color' spelled as 'colour' in some rows. Which SQL query correctly updates all 'colour' to 'color' in the description column?Solution
Step 1: Understand the goal
We want to replace all occurrences of 'colour' with 'color' in the description column.Step 2: Check each query's correctness
UPDATE products SET description = REPLACE(description, 'colour', 'color'); correctly uses REPLACE with the column first, then old substring 'colour', then new substring 'color'. UPDATE products SET description = REPLACE('colour', 'color', description); has wrong parameter order. UPDATE products SET description = REPLACE(description, 'color', 'colour'); replaces 'color' with 'colour' (opposite). UPDATE products SET description = REPLACE(description, 'colour'); misses the new substring parameter.Final Answer:
UPDATE products SET description = REPLACE(description, 'colour', 'color'); -> Option BQuick Check:
Correct update syntax with REPLACE = B [OK]
- Swapping old and new substrings
- Missing new substring parameter
- Using wrong parameter order
Ready to prove it?
Pass the test and this pattern moves unknown
