Fix Names in a Table - Problem
You are working with a Users database table that has inconsistent name formatting. The names contain a mix of uppercase and lowercase letters, making the data look unprofessional and inconsistent.
Your task is to standardize all names so that only the first character is uppercase and the rest are lowercase (proper case formatting).
| Column Name | Type |
|---|---|
| user_id | int |
| name | varchar |
user_id is the primary key for this table.
Goal: Transform names like "aLiCe", "bOB", or "CHARLIE" into proper case format: "Alice", "Bob", "Charlie".
Output: Return the result table ordered by user_id in ascending order.
Input & Output
example_1.sql โ Basic Example
$
Input:
Users table:
| user_id | name |
|---------|-------|
| 1 | aLice |
| 2 | bOB |
| 3 | charlie |
โบ
Output:
| user_id | name |
|---------|----------|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
๐ก Note:
Each name is transformed to have only the first character uppercase and the rest lowercase, maintaining the original user_id order.
example_2.sql โ Mixed Case Names
$
Input:
Users table:
| user_id | name |
|---------|----------|
| 1 | JOHN |
| 2 | mArY |
| 3 | dAvId |
โบ
Output:
| user_id | name |
|---------|-------|
| 1 | John |
| 2 | Mary |
| 3 | David |
๐ก Note:
All uppercase and mixed case names are properly formatted with only the first letter capitalized.
example_3.sql โ Single Character Names
$
Input:
Users table:
| user_id | name |
|---------|------|
| 1 | a |
| 2 | B |
| 3 | c |
โบ
Output:
| user_id | name |
|---------|------|
| 1 | A |
| 2 | B |
| 3 | C |
๐ก Note:
Single character names are handled correctly, ensuring they are uppercase.
Constraints
- 1 โค user_id โค 106
- 1 โค name.length โค 100
- name consists of only lowercase and uppercase English letters
- The result should be ordered by user_id in ascending order
Visualization
Tap to expand
Understanding the Visualization
1
Identify the Problem
Names in the database have mixed capitalization patterns
2
Extract First Character
Use SUBSTRING to get the first letter of each name
3
Transform Characters
Apply UPPER to first char, LOWER to remaining chars
4
Combine Results
Use CONCAT to merge the transformed parts
5
Sort Output
Order results by user_id for consistent output
Key Takeaway
๐ฏ Key Insight: SQL string functions can efficiently transform text data in bulk, making database cleanup operations fast and reliable
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code