Guides › Find duplicate customers
Find duplicate customers in SQL Server
Someone typed the same person in twice. Reports count them as two customers. You want the keys that collide, then the rows.
Which emails appear more than once
SELECT LOWER(LTRIM(RTRIM(email))) AS email, COUNT(*) AS times
FROM Customers
GROUP BY LOWER(LTRIM(RTRIM(email)))
HAVING COUNT(*) > 1
ORDER BY times DESC;
Trim and lower first. Sam@Harbour.co and sam@harbour.co are one person in the real world.
Then the actual rows
SELECT c.customer_id, c.first_name, c.last_name, c.email
FROM Customers c
WHERE LOWER(LTRIM(RTRIM(c.email))) IN (
SELECT LOWER(LTRIM(RTRIM(email)))
FROM Customers
GROUP BY LOWER(LTRIM(RTRIM(email)))
HAVING COUNT(*) > 1
)
ORDER BY c.email, c.customer_id;
Matching on name only
Two John Smiths are not always a duplicate. Email, phone, or a customer code is safer. Name + postcode if that is all you have, and still check by eye before you merge.
Try it on Harbour & Co
Becky Ashworth (Support) needs this now: “I think some customers are on the system twice. Which emails belong to more than one record?” Write the query, run it, send the results. On the paid plan.
Take the ticket →Learn it on a real ticket
Monday is free on WorkSQL. A colleague’s ticket lands, you write the T-SQL, we mark it. No videos. Five minutes.
Clock in free See Monday’s tickets