Fix Names in a Table - Problem

You are given a table Users with user information where names contain mixed case characters.

Problem: Fix all names so that only the first character is uppercase and the rest are lowercase.

Requirements:

  • Only the first character should be uppercase
  • All remaining characters should be lowercase
  • Return results ordered by user_id

Table Schema

Users
Column Name Type Description
user_id PK int Primary key - unique user identifier
name varchar User name with mixed case characters
Primary Key: user_id
Note: Names consist of only lowercase and uppercase characters

Input & Output

Example 1 — Mixed Case Names
Input Table:
user_id name
1 aLice
2 bOB
3 CHARLIE
Output:
user_id name
1 Alice
2 Bob
3 Charlie
💡 Note:

The query transforms each name by making the first character uppercase and all remaining characters lowercase. aLice becomes Alice, bOB becomes Bob, and CHARLIE becomes Charlie.

Example 2 — Single Character Names
Input Table:
user_id name
1 a
2 B
Output:
user_id name
1 A
2 B
💡 Note:

For single character names, only the first character transformation applies. The SUBSTRING(name, 2) returns empty string, so we just get the uppercase first character.

Constraints

  • 1 ≤ user_id ≤ 1000
  • name consists of only lowercase and uppercase characters
  • 1 ≤ name.length ≤ 100

Visualization

Tap to expand
Fix Names in a Table INPUT user_id name 1 aLICE 2 bOB 3 CHARLIE 4 david Users Table Names have inconsistent capitalization Table: Users Columns: user_id, name Fix: Proper Case names ALGORITHM STEPS 1 Extract First Char SUBSTRING(name, 1, 1) 2 Uppercase First UPPER(first_char) 3 Lowercase Rest LOWER(SUBSTRING(name,2)) 4 Concatenate CONCAT(upper, lower) SELECT user_id, CONCAT( UPPER(SUBSTR(name,1,1)), LOWER(SUBSTR(name,2)) ) AS name ORDER BY user_id; FINAL RESULT user_id name 1 Alice 2 Bob 3 Charlie 4 David OK - Names Fixed! Transformation Applied: aLICE --> Alice bOB --> Bob CHARLIE --> Charlie david --> David Key Insight: Use string functions UPPER(), LOWER(), and SUBSTR()/SUBSTRING() to manipulate character casing. CONCAT combines the uppercase first character with the lowercase remainder. ORDER BY ensures proper sorting. Works in MySQL, PostgreSQL (use SUBSTRING), SQL Server (use UPPER + LOWER with SUBSTRING). TutorialsPoint - Fix Names in a Table | Optimal Solution
Asked in
Amazon 23 Google 18 Microsoft 15
23.4K Views
Medium Frequency
~8 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen