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?"
Practice
UPPER('Hello World') return?Solution
Step 1: Understand the UPPER function
The UPPER function converts all letters in the input string to uppercase letters.Step 2: Apply UPPER to 'Hello World'
Converting each letter to uppercase results in 'HELLO WORLD'.Final Answer:
'HELLO WORLD' -> Option BQuick Check:
UPPER('Hello World') = 'HELLO WORLD' [OK]
- Confusing UPPER with LOWER
- Thinking it changes only first letter
- Assuming it reverses the string
name to lowercase in SQL?Solution
Step 1: Recall correct function syntax
The LOWER function is called with parentheses around the column name: LOWER(column_name).Step 2: Check each option
SELECT LOWER(name) FROM table; uses correct syntax: LOWER(name). Others have syntax errors or invalid notation.Final Answer:
SELECT LOWER(name) FROM table; -> Option AQuick Check:
Use LOWER(column) with parentheses [OK]
- Omitting parentheses
- Using dot notation like name.LOWER()
- Using square brackets inside function
users with a column username containing values: 'Alice', 'BOB', 'charlie', what will the query SELECT LOWER(username) FROM users; return?Solution
Step 1: Understand LOWER function effect on each username
LOWER converts all letters to lowercase regardless of original case.Step 2: Apply LOWER to each username
'Alice' -> 'alice', 'BOB' -> 'bob', 'charlie' -> 'charlie'.Final Answer:
['alice', 'bob', 'charlie'] -> Option CQuick Check:
LOWER converts all letters to lowercase [OK]
- Assuming only first letter changes
- Confusing LOWER with UPPER
- Expecting mixed case output
SELECT LOWER username FROM users;Solution
Step 1: Check function syntax
SQL functions require parentheses around arguments, so LOWER(username) is correct.Step 2: Identify missing parentheses
The query uses LOWER username without parentheses, causing syntax error.Final Answer:
Missing parentheses around column name -> Option DQuick Check:
Functions need parentheses [OK]
- Omitting parentheses for functions
- Assuming functions work like keywords
- Ignoring syntax errors
products where the category column equals 'Books', ignoring case differences. Which SQL WHERE clause correctly does this?Solution
Step 1: Understand case-insensitive comparison
To ignore case, convert both sides to the same case before comparing.Step 2: Apply LOWER and UPPER to category and compare both
Using WHERE LOWER(category) = 'books' AND UPPER(category) = 'BOOKS' ensures the category matches 'Books' ignoring case by checking both lowercase and uppercase.Final Answer:
WHERE LOWER(category) = 'books' AND UPPER(category) = 'BOOKS' -> Option AQuick Check:
Use both LOWER and UPPER on the column to ensure case-insensitive match [OK]
- Comparing without case conversion
- Mixing cases in comparison string
- Using only one case function
Ready to prove it?
Pass the test and this pattern moves unknown
