-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFix Names in a Table.sql
More file actions
25 lines (24 loc) · 1.13 KB
/
Fix Names in a Table.sql
File metadata and controls
25 lines (24 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- Problem Statement: Fix the names so that only the first character is uppercase and the rest are lowercase.
-- Given Table: Users
--
-- +----------------+---------+
-- | Column Name | Type |
-- +----------------+---------+
-- | user_id | int |
-- | name | varchar |
-- +----------------+---------+
-- user_id is the primary key (column with unique values) for this table.
-- This table contains the ID and the name of the user. The name consists of only lowercase and uppercase characters.
--
-- Approach:
-- 1. Modify Name Format: We use the CONCAT, UPPER, and LOWER functions to transform the name column.
-- - UPPER(SUBSTRING(name, 1, 1)): This converts the first character of the name to uppercase.
-- - LOWER(SUBSTRING(name FROM 2)): This converts the rest of the characters to lowercase.
-- - CONCAT function is used to concatenate the modified first character with the modified rest of the name.
--
-- 2. Order by user_id: We order the result table by user_id.
--
-- SQL Solution:
SELECT user_id, CONCAT(UPPER(SUBSTRING(name, 1, 1)), LOWER(SUBSTRING(name FROM 2))) AS name
FROM Users
ORDER BY user_id;