0
0
MySQLquery~5 mins

UPPER and LOWER in MySQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: UPPER and LOWER
O(n)
Understanding Time Complexity

We want to understand how the time it takes to change text to uppercase or lowercase grows as the text gets longer.

How does the work increase when we use UPPER or LOWER on bigger text?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

SELECT UPPER(name) FROM employees;
SELECT LOWER(description) FROM products;

This code changes all letters in the 'name' or 'description' columns to uppercase or lowercase for every row.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: For each row, the function processes each character in the text string.
  • How many times: Once for every character in every row's text.
How Execution Grows With Input

As the text length grows, the work grows too because each character needs to be changed.

Input Size (characters per row)Approx. Operations (per row)
1010 character changes
100100 character changes
10001000 character changes

Pattern observation: The work grows directly with the number of characters; double the characters, double the work.

Final Time Complexity

Time Complexity: O(n)

This means the time to convert text grows in a straight line with the length of the text.

Common Mistake

[X] Wrong: "UPPER and LOWER run instantly no matter how long the text is."

[OK] Correct: Each character must be checked and changed, so longer text takes more time.

Interview Connect

Knowing how string functions like UPPER and LOWER scale helps you explain performance when working with text data in databases.

Self-Check

"What if we applied UPPER or LOWER only to a fixed small part of the text instead of the whole string? How would the time complexity change?"