Basic SQL Functions: UPPER, LOWER, LENGTH
You'll learn how to transform text data in SQL queries using built-in string functions that change letter casing and measure text length.
What Are String Functions?
SQL provides built-in functions that let you manipulate and transform data during the query itself, without changing the stored data. Think of them like filters on a photo app—they change how you view the data temporarily, but the original stays untouched in your database.
Three fundamental string functions work like this:
UPPER() - Convert to Uppercase
The UPPER() function transforms all letters in a string to uppercase:
SELECT UPPER(first_name) FROM users;
If first_name contains "alice", the result displays "ALICE". Great for case-insensitive comparisons!
LOWER() - Convert to Lowercase
The LOWER() function does the opposite—converts everything to lowercase:
SELECT LOWER(email) FROM users WHERE LOWER(email) LIKE '%gmail.com';
This ensures you catch "User@GMAIL.COM" and "user@gmail.com" alike.
LENGTH() - Count Characters
The LENGTH() function returns the number of characters in a string:
SELECT username, LENGTH(username) FROM users WHERE LENGTH(username) > 15;
This counts every character, including spaces. Perfect for validation checks or finding unusually long entries.
Practical Combinations
You can combine these functions with other SQL features you've learned:
SELECT DISTINCT UPPER(country) FROM customers ORDER BY UPPER(country);
This removes duplicate countries, converts them to uppercase for consistency, and sorts them alphabetically.
Key Takeaway: UPPER(), LOWER(), and LENGTH() let you transform string data on-the-fly in queries for formatting, comparison, and validation—without modifying the actual stored values.