UPPER and LOWER functions in SQL - Time & Space Complexity
We want to understand how the time it takes to change text case grows as the amount of text grows.
How does using UPPER or LOWER on many rows affect performance?
Analyze the time complexity of the following code snippet.
SELECT UPPER(name) AS upper_name
FROM customers
WHERE city = 'New York';
This query converts the 'name' column to uppercase for all customers in New York.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying UPPER function to each selected row's 'name' value.
- How many times: Once for each row returned by the WHERE filter.
As the number of matching rows grows, the total work grows proportionally because each row's text is converted.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 conversions |
| 100 | 100 conversions |
| 1000 | 1000 conversions |
Pattern observation: Doubling the number of rows doubles the work.
Time Complexity: O(n)
This means the time to convert text grows directly with the number of rows processed.
[X] Wrong: "UPPER or LOWER functions run instantly no matter how many rows there are."
[OK] Correct: Each row's text must be processed, so more rows mean more work and more time.
Understanding how simple functions like UPPER and LOWER scale helps you reason about query performance in real projects.
"What if we applied UPPER to a column with very long text strings? How would that affect the time complexity?"