How to find duplicates in a CSV file with SQL
Find repeated customer IDs in a CSV export using GROUP BY and HAVING. Start with sample data, then apply the query to your own file.
1. Load the CSV
Download contacts.csv and use Import Wizard to name the table contacts. You can also open the ready-made example below.
| customer_id | |
|---|---|
| 1 | ada@example.com |
| 2 | linus@example.com |
| 1 | ada@example.com |
| 3 | grace@example.com |
| 2 | other@example.com |
2. Count repeated IDs
SELECT customer_id, COUNT(*) AS occurrences
FROM contacts
GROUP BY customer_id
HAVING COUNT(*) > 1
ORDER BY customer_id;Open this example in SQLSheet3. Compare the expected result
| customer_id | occurrences |
|---|---|
| 1 | 2 |
| 2 | 2 |
GROUP BY creates one group per customer ID. COUNT(*) counts rows, and HAVING keeps groups containing more than one row. This checks duplicate IDs, not whether entire rows are identical. Blank or NULL IDs also need separate review.
4. Inspect the original rows
SELECT * FROM contacts
WHERE customer_id IN (
SELECT customer_id FROM contacts
GROUP BY customer_id HAVING COUNT(*) > 1
)
ORDER BY customer_id, email;Review all four matching rows before removing anything: customer 2 has two different email addresses. For repeated pairs, group by customer_id, email instead. For complete-row duplicates, include every relevant column.
5. Export the rows for review
Download query results as CSV. For your own file, use the exact table and column names from Database Structure. Keep IDs as text if leading zeroes matter.