FORMAT and LPAD/RPAD in MySQL - Time & Space Complexity
We want to understand how the time it takes to run FORMAT and LPAD/RPAD functions changes as the input size grows.
How does the work these functions do grow when we give them bigger numbers or longer strings?
Analyze the time complexity of the following code snippet.
SELECT FORMAT(salary, 2) AS formatted_salary,
LPAD(name, 10, '*') AS padded_name_left,
RPAD(name, 10, '#') AS padded_name_right
FROM employees;
This code formats a number with two decimals and pads names on the left and right to a fixed length.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying FORMAT and LPAD/RPAD to each row in the employees table.
- How many times: Once per row, so the number of rows (n) times.
Each row requires a fixed amount of work to format and pad, so total work grows as we add more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 formatting and padding operations |
| 100 | 100 formatting and padding operations |
| 1000 | 1000 formatting and padding operations |
Pattern observation: The total work grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the number of rows increases.
[X] Wrong: "FORMAT and LPAD/RPAD take longer if the strings or numbers are bigger in length."
[OK] Correct: These functions do a fixed amount of work per row, so the main factor is how many rows there are, not the size of each string or number.
Understanding how simple formatting functions scale helps you explain query performance clearly and shows you can think about how data size affects work done.
"What if we added a nested function inside LPAD that itself loops over the string? How would the time complexity change?"
Practice
FORMAT(12345.6789, 2) do?Solution
Step 1: Understand FORMAT function usage
The FORMAT function formats a number with commas and rounds it to the specified decimal places.Step 2: Apply FORMAT(12345.6789, 2)
The number 12345.6789 is rounded to 2 decimals and commas are added, resulting in '12,345.68'.Final Answer:
Formats the number with commas and 2 decimal places, like '12,345.68'. -> Option AQuick Check:
FORMAT adds commas and decimals = A [OK]
- Thinking FORMAT rounds to integer only
- Confusing FORMAT with LPAD or RPAD
- Expecting no commas in output
Solution
Step 1: Recall LPAD syntax
LPAD(string, length, pad_string) pads the string on the left to the given length using pad_string.Step 2: Check each option
LPAD('123', 6, '0') uses correct types: string '123', length 6, pad '0'. Others have wrong types or missing arguments.Final Answer:
LPAD('123', 6, '0') -> Option DQuick Check:
LPAD needs string, length, pad string = C [OK]
- Using numbers instead of strings for pad character
- Missing the pad string argument
- Passing length as a string instead of number
SELECT LPAD('abc', 6, '*'), RPAD('abc', 6, '!');Solution
Step 1: Understand LPAD and RPAD behavior
LPAD pads on the left, RPAD pads on the right to reach total length 6.Step 2: Calculate LPAD('abc', 6, '*')
'abc' length is 3, so 3 '*' are added on left: '***abc'.Step 3: Calculate RPAD('abc', 6, '!')
Similarly, 3 '!' are added on right: 'abc!!!'.Final Answer:
'***abc', 'abc!!!' -> Option BQuick Check:
LPAD left stars, RPAD right exclamations = B [OK]
- Confusing LPAD and RPAD directions
- Not counting original string length
- Expecting padding to replace characters
SELECT LPAD('test', 4, '0');Solution
Step 1: Check LPAD length vs string length
LPAD pads string to length 4, but 'test' length is 4, so no padding needed.Step 2: Confirm behavior when length equals string length
LPAD returns original string if length equals string length, no error occurs.Final Answer:
No error; it returns 'test'. -> Option CQuick Check:
LPAD returns original if length equals string length = D [OK]
- Expecting error when length equals string length
- Thinking pad string must be numeric
- Confusing LPAD with RPAD requirements
Solution
Step 1: Format price with commas and 2 decimals
Use FORMAT(price, 2) to get string like '1,234.56'.Step 2: Pad formatted string on left to length 12 with '#'
Use LPAD(formatted_string, 12, '#') to add '#' on left if needed.Step 3: Check options
SELECT LPAD(FORMAT(price, 2), 12, '#') FROM products; correctly formats then pads left. SELECT FORMAT(LPAD(price, 12, '#'), 2) FROM products; tries to pad number before formatting (wrong). SELECT RPAD(FORMAT(price, 2), 12, '#') FROM products; pads right, not left. SELECT LPAD(price, 12, '#') FROM products; pads number without formatting.Final Answer:
SELECT LPAD(FORMAT(price, 2), 12, '#') FROM products; -> Option AQuick Check:
Format first, then LPAD left padding = A [OK]
- Padding before formatting number
- Using RPAD instead of LPAD for left padding
- Padding raw number without formatting
Ready to prove it?
Pass the test and this pattern moves unknown
