Normalise Customer Phone Numbers
A carpentry shop's customer list holds phone numbers typed in many styles: (415) 555-0132, 415.555.0132, +1 415 555 0132 and so on. Write an UPDATE that rewrites them to the canonical form DDD-DDD-DDDD using these steps:
- Remove every space,
(,),.,-and+. - If the result is 11 characters long and starts with
1, drop that first1. - If what remains is exactly 10 digits (
0–9), store it as the first 3 digits, a hyphen, the next 3 digits, a hyphen, the last 4 digits.
Any other value, including NULL, must be left exactly as it was. Do not return anything; the judge checks client_id and phone of every row afterwards.
Tables
shop_clients — One row per customer of the carpentry shop.
| Column | Type | Description |
|---|---|---|
client_id (key) | INTEGER | Unique customer id |
client_name | TEXT | Customer name |
phone | TEXT | Phone number as typed at the counter; free text, may be NULL |
Example
shop_clients
| client_id | client_name | phone |
|---|---|---|
| 1 | Hollis Grant | (415) 555-0132 |
| 2 | Mae Duarte | 415.555.0177 |
| 3 | Ivo Lindqvist | +1 212 555 0148 |
| 4 | Priya Sen | 555-0199 |
| 5 | Cal Okafor | NULL |
| 6 | Nell Brandt | 3125550110 |
| 7 | Ezra Vos | 646-555-0101 |
Expected output
| client_id | phone |
|---|---|
| 1 | 415-555-0132 |
| 2 | 415-555-0177 |
| 3 | 212-555-0148 |
| 4 | 555-0199 |
| 5 | NULL |
| 6 | 312-555-0110 |
| 7 | 646-555-0101 |
Rows may be returned in any order. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Changing Data · problem 62 of 70 · SQLite
The idea
Split the job into a pure calculation and a guarded write. The calculation strips the punctuation with nested replace() calls, then drops a leading 1 when exactly 11 characters remain. Doing this in a subquery (or CTE) gives the cleaned value a name, digits, instead of repeating a six-level expression.
The write uses SQLite's UPDATE … FROM: each customer row is joined to its cleaned value and receives substr(digits, 1, 3) || '-' || …. The guard lives in WHERE: digits GLOB '[0-9]…' with ten classes means "exactly ten characters, each a digit". Rows that fail it, including NULL (every comparison is NULL) and text like 555CALLNOW that has the right length but letters, are never touched.
The alternative uses a WITH clause on the UPDATE, a correlated subquery in SET and an IN filter. That works on SQLite versions without UPDATE … FROM. Numbers already in canonical form pass the test and are rewritten to the same value, so running the statement twice is safe.
Solution
UPDATE shop_clients
SET phone = substr(c.digits, 1, 3) || '-' ||
substr(c.digits, 4, 3) || '-' ||
substr(c.digits, 7, 4)
FROM (
SELECT client_id,
CASE
WHEN length(stripped) = 11 AND stripped LIKE '1%' THEN substr(stripped, 2)
ELSE stripped
END AS digits
FROM (
SELECT client_id,
replace(replace(replace(replace(replace(replace(phone, ' ', ''), '(', ''), ')', ''), '.', ''), '-', ''), '+', '') AS stripped
FROM shop_clients
)
) AS c
WHERE c.client_id = shop_clients.client_id
AND c.digits GLOB '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]';
Another way
WITH stripped AS (
SELECT client_id,
replace(replace(replace(replace(replace(replace(phone, ' ', ''), '(', ''), ')', ''), '.', ''), '-', ''), '+', '') AS s
FROM shop_clients
),
cleaned AS (
SELECT client_id,
iif(length(s) = 11 AND substr(s, 1, 1) = '1', substr(s, 2), s) AS d
FROM stripped
)
UPDATE shop_clients
SET phone = (
SELECT substr(d, 1, 3) || '-' || substr(d, 4, 3) || '-' || substr(d, 7)
FROM cleaned
WHERE cleaned.client_id = shop_clients.client_id
)
WHERE client_id IN (
SELECT client_id
FROM cleaned
WHERE length(d) = 10
AND d NOT GLOB '*[^0-9]*'
);
Why common answers fail
Skips step 2, so numbers written with a leading country code 1 are left unformatted.
UPDATE shop_clients SET phone = substr(d, 1, 3) || '-' || substr(d, 4, 3) || '-' || substr(d, 7) FROM (SELECT client_id AS cid, replace(replace(replace(replace(replace(replace(phone, ' ', ''), '(', ''), ')', ''), '.', ''), '-', ''), '+', '') AS d FROM shop_clients) WHERE cid = shop_clients.client_id AND d GLOB '[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]';
Checks only the length, so 10 characters with letters, such as '555CALLNOW', get hyphens inserted.
UPDATE shop_clients SET phone = substr(d, 1, 3) || '-' || substr(d, 4, 3) || '-' || substr(d, 7) FROM (SELECT client_id AS cid, iif(length(s) = 11 AND s LIKE '1%', substr(s, 2), s) AS d FROM (SELECT client_id, replace(replace(replace(replace(replace(replace(phone, ' ', ''), '(', ''), ')', ''), '.', ''), '-', ''), '+', '') AS s FROM shop_clients)) WHERE cid = shop_clients.client_id AND length(d) = 10;
Formats every row without any checks, mangling short, long and non-numeric values that must stay untouched.
UPDATE shop_clients SET phone = substr(replace(replace(replace(replace(replace(replace(phone, ' ', ''), '(', ''), ')', ''), '.', ''), '-', ''), '+', ''), 1, 3) || '-' || substr(replace(replace(replace(replace(replace(replace(phone, ' ', ''), '(', ''), ')', ''), '.', ''), '-', ''), '+', ''), 4, 3) || '-' || substr(replace(replace(replace(replace(replace(replace(phone, ' ', ''), '(', ''), ')', ''), '.', ''), '-', ''), '+', ''), 7);
What the interviewer is checking
Tests careful string manipulation plus the discipline of only touching rows that pass validation in a data-fix UPDATE.
Mistakes to avoid
- Checking length without checking that every character is a digit.
- Putting the validation only inside SET (with a CASE) and forgetting to return the old value when it fails.
- Ignoring the country-code case, or dropping a leading 1 from numbers that are already 10 digits.
Follow-ups to expect
- List the rows that could not be normalised for manual review (SELECT the rows that fail the same test).
- Store numbers as E.164 instead, such as +14155550132 (prefix '+1' to the ten digits).
- Prevent bad formats in future (CHECK (phone GLOB '[0-9][0-9][0-9]-[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]')).
In real work
Normalising phone numbers, postcodes and IDs is a standard step before de-duplicating customers or matching records across systems.