Task: find duplicated emails.
Duplicate emails are found by grouping on a normalised value: GROUP BY LOWER(TRIM(email)) with HAVING COUNT(*) > 1. Normalisation is needed because addresses differing in case or whitespace are different for grouping. NULLs are excluded, since grouping collects them into one group and reports a false duplicate. The records themselves come from a window COUNT. Prevention is a unique index on the normalised expression, because UNIQUE(email) does not normalise case.
SQL · Tasks
The backend promises a unique email, yet the database holds several "identical" addresses. How to find them?
Email uniqueness is usually stated in the requirements, and this is where backends often slip: they compare strings literally or check "does it exist?" and only then insert. The interview task is simple: find the repeated emails. On real data, though, the simple query misses part of the duplicates and adds a spurious row.
| id | |
|---|---|
| 1 | anna@test.com |
| 2 | Anna@test.com |
| 3 | 'anna@test.com ' (trailing space) |
| 4 | oleh@test.com |
| 5 | oleh@test.com |
| 6 | maria@test.com |
| 7 | NULL |
| 8 | NULL |
How it works and why. GROUP BY email collects rows with the same value into one group, and HAVING COUNT(*) > 1 keeps the groups with more than one row. A condition on a group cannot go in WHERE: that works on single rows before grouping. The key word is "same": for grouping, rows with a different case or whitespace are different, while all NULLs land in one group (for grouping NULLs count as equal, although NULL = NULL gives UNKNOWN).
Example: from the literal query to the normalised one.
SELECT email, COUNT(*) AS cnt
FROM users
GROUP BY email
HAVING COUNT(*) > 1;
-- oleh@test.com | 2
-- NULL | 2
Diagnosis: anna@test.com, Anna@test.com and the record with a trailing space are three different values for grouping, so none reached the "more than one" threshold. Meanwhile two empty emails formed a NULL group, and it looks like a duplicate. For a required field that is a separate defect; for an optional field it is normal.
SELECT LOWER(TRIM(email)) AS email_norm, COUNT(*) AS cnt
FROM users
WHERE email IS NOT NULL
GROUP BY LOWER(TRIM(email))
HAVING COUNT(*) > 1;
-- anna@test.com | 3
-- oleh@test.com | 2
What each of the two queries sees
| cnt | |
|---|---|
| oleh@test.com | 2 |
| NULL | 2 |
| cnt | |
|---|---|
| anna@test.com | 3 |
| oleh@test.com | 2 |
To see the records themselves, not just the values, use a window count: it adds the count to every row, and an outer query filters the groups.
SELECT id, email
FROM (
SELECT id, email,
COUNT(*) OVER (PARTITION BY LOWER(TRIM(email))) AS n
FROM users
WHERE email IS NOT NULL
) x
WHERE n > 1
ORDER BY LOWER(TRIM(email)), id;
-- ids 1, 2, 3 (anna) and 4, 5 (oleh)
Limits and alternatives. In PostgreSQL TRIM removes only spaces by default, not tabs and non-breaking spaces: catch those separately with a regular expression or REGEXP_REPLACE. What counts as a duplicate is a product rule: whether addresses that differ in the case of the local part are the same, and whether a +tag makes another address. Found duplicates are a symptom; prevention lives in the database: a unique index over the normalised expression (CREATE UNIQUE INDEX … ON users (LOWER(email))). A plain UNIQUE(email) does not normalise the case and allows several NULLs.
Common mistakes.
- Grouping by the raw email: duplicates that differ in case or whitespace stay unnoticed.
- Without
IS NOT NULL: empty emails form a NULL "duplicate". WHERE COUNT(*) > 1instead ofHAVING: the query fails with an error.- Counting duplicates as the
COUNT(*)of groups: a group of three records has two extra, not three.
Check yourself
Why does GROUP BY email HAVING COUNT(*) > 1 return a row where the email is NULL, and is it always a bug?
Answer
For grouping all NULLs fall into one group, so two empty emails give a "duplicate". It is a bug only if the field is required. If an empty email is allowed, exclude NULL with WHERE email IS NOT NULL.
The table has UNIQUE(email). Can it hold both Anna@test.com and anna@test.com?
Answer
Yes: in PostgreSQL string comparison is case-sensitive, so these are different values. Case-insensitive uniqueness needs a unique index on LOWER(email) or the citext type. A registration test with a different case exposes this defect.