UPPER and LOWER in MySQL - Time & Space 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?
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 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.
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) |
|---|---|
| 10 | 10 character changes |
| 100 | 100 character changes |
| 1000 | 1000 character changes |
Pattern observation: The work grows directly with the number of characters; double the characters, double the work.
Time Complexity: O(n)
This means the time to convert text grows in a straight line with the length of the text.
[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.
Knowing how string functions like UPPER and LOWER scale helps you explain performance when working with text data in databases.
"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?"
Practice
UPPER('hello') return?Solution
Step 1: Understand the UPPER function
The UPPER function converts all letters in the input text to uppercase letters.Step 2: Apply UPPER to 'hello'
Applying UPPER to 'hello' changes all letters to uppercase, resulting in 'HELLO'.Final Answer:
'HELLO' -> Option DQuick Check:
UPPER('hello') = 'HELLO' [OK]
- Thinking UPPER only capitalizes first letter
- Confusing UPPER with LOWER
- Expecting mixed case output
name to lowercase in a SELECT query?Solution
Step 1: Recall correct function syntax
MySQL functions use the format FUNCTION_NAME(argument), so LOWER(name) is correct.Step 2: Check each option
SELECT LOWER(name) FROM users; uses correct syntax: LOWER(name). Options A, B, and D use invalid syntax forms.Final Answer:
SELECT LOWER(name) FROM users; -> Option AQuick Check:
Correct function call syntax = SELECT LOWER(name) FROM users; [OK]
- Omitting parentheses around column
- Using dot or bracket notation incorrectly
- Writing function name in lowercase without parentheses
products with a column category containing values 'Books', 'electronics', 'TOYS', what will the query SELECT DISTINCT UPPER(category) FROM products; return?Solution
Step 1: Understand UPPER and DISTINCT
UPPER converts all text to uppercase. DISTINCT removes duplicates from the result set.Step 2: Apply UPPER to each category value
'Books' -> 'BOOKS', 'electronics' -> 'ELECTRONICS', 'TOYS' -> 'TOYS'. All become uppercase.Step 3: Collect distinct uppercase values
All three are unique after conversion, so the result is ['BOOKS', 'ELECTRONICS', 'TOYS'].Final Answer:
['BOOKS', 'ELECTRONICS', 'TOYS'] -> Option BQuick Check:
UPPER + DISTINCT = uppercase unique values [OK]
- Expecting original case values
- Ignoring DISTINCT effect
- Mixing uppercase and lowercase in output
SELECT LOWER('MySQL') FROM dual. It returns an error. What is the likely cause?Solution
Step 1: Understand the use of 'dual'
'dual' is a special table in Oracle but not present by default in MySQL.Step 2: Check MySQL behavior
MySQL does not require 'FROM dual' for selecting expressions; using it causes an error if 'dual' table is missing.Final Answer:
The table 'dual' does not exist in MySQL by default -> Option AQuick Check:
MySQL lacks default 'dual' table [OK]
- Assuming 'dual' exists like in Oracle
- Thinking LOWER is unsupported
- Blaming missing semicolon
users table whose email ends with '@example.com', ignoring case differences. Which query correctly uses UPPER or LOWER to achieve this?Solution
Step 1: Understand case-insensitive matching
To ignore case, convert both sides to the same case before comparing.Step 2: Apply UPPER to email and pattern
Using UPPER(email) and matching with uppercase pattern '%@EXAMPLE.COM' ensures case-insensitive match.Step 3: Check options
SELECT * FROM users WHERE UPPER(email) LIKE '%@EXAMPLE.COM'; correctly uses UPPER on email and uppercase pattern. SELECT * FROM users WHERE LOWER(email) LIKE '%@Example.com'; mismatches case in pattern. SELECT * FROM users WHERE email LIKE '%@example.com'; is case-sensitive. SELECT * FROM users WHERE LOWER(email) LIKE '%@EXAMPLE.COM'; mismatches case in pattern.Final Answer:
SELECT * FROM users WHERE UPPER(email) LIKE '%@EXAMPLE.COM'; -> Option CQuick Check:
Match cases on both sides using UPPER or LOWER [OK]
- Not matching case of pattern and column
- Assuming LIKE is case-insensitive by default
- Mixing LOWER with uppercase pattern
Ready to prove it?
Pass the test and this pattern moves unknown
