SQL Guide · Prompt: “Find duplicate email addresses in the users table and show how many times each appears”
How to Find Duplicate Rows in SQL Using GROUP BY
Verified by LiveFormulaLab Engineering Team • Updated November 2026
Find duplicate emails, IDs, or records in any SQL table using GROUP BY and HAVING COUNT(*) > 1. Test live in the interactive SQL sandbox.
Verified SQL Syntax
SELECT email, COUNT(*) AS occurrences
FROM users
GROUP BY email
ORDER BY occurrences DESC;How This Formula Works (Step-by-Step)
To detect duplicate records in PostgreSQL, MySQL, or BigQuery, group rows by the column that should be unique and count occurrences.
Step 1: GROUP BY email collapses identical email addresses into a single summary row.
Step 2: Filter with HAVING COUNT(*) > 1 to surface only duplicated entries.
Formula Syntax & Arguments Breakdown
Groups rows by the target natural key (such as email or external_id), counts occurrences per group, and ranks or filters groups with COUNT(*) > 1.
| Argument / Clause | Requirement | Data Type | Role & Behavior |
|---|---|---|---|
SELECT email, COUNT(*) AS occurrences | Required | Projection | Outputs each unique key alongside the exact number of matching rows in the table. |
FROM users | Required | Table Source | Target table audited for duplicate records. |
GROUP BY email | Required | Grouping Key | Collapses identical keys together so aggregate counts apply per unique value. |
HAVING COUNT(*) > 1 / ORDER BY | Optional | Post-Aggregate Filter | Filters out unique singletons so only duplicated keys appear (ordered highest frequency first). |
3 Real-World Use Cases
- Auditing user accounts for duplicate signups before enforcing a UNIQUE constraint.
- Catching double-processed webhook transactions in billing tables.
- Identifying repeated product SKUs in warehouse catalog imports.
Cross-Platform Equivalents
- Excel
=IF(COUNTIF($B$2:$B$6, B2)>1, "Duplicate", "Unique")- SQL
SELECT email, COUNT(*) AS occurrences FROM users GROUP BY email HAVING COUNT(*) > 1;- Python Pandas
df[df.duplicated('email', keep=False)].groupby('email').size()
Common Mistakes & Pro Tips
Pitfalls & How to Fix Them
Mistake: Putting COUNT(*) > 1 inside the WHERE clause throws a SQL syntax error because aggregates run after WHERE.
Fix: Always filter aggregate counts in a HAVING COUNT(*) > 1 clause placed after GROUP BY.
Mistake: Case differences ("Alex@acme.com" vs "alex@acme.com") escape exact GROUP BY duplicate checks.
Fix: Normalize keys inside GROUP BY LOWER(TRIM(email)) to catch casing and whitespace duplicates.
Mistake: Forgetting NULL handling groups all missing NULL emails into one giant duplicate bucket.
Fix: Add WHERE email IS NOT NULL before GROUP BY when auditing optional fields.
Engineering Pro Tips
- Add STRING_AGG(id::text, ', ') (Postgres) or GROUP_CONCAT(id) (MySQL) to inspect the exact row IDs of every duplicate cluster.
- Once duplicates are cleaned, enforce prevention at the database layer with CREATE UNIQUE INDEX idx_users_email ON users (LOWER(email)).
- To check composite uniqueness (e.g. customer + invoice_no), simply list both columns in SELECT and GROUP BY.
Frequently Asked Questions
- Why use HAVING instead of WHERE to filter COUNT(*)?
- WHERE filters individual rows before grouping, whereas HAVING filters grouped aggregate results after COUNT(*) runs.
- How do I check duplicates across two columns together?
- List both columns in GROUP BY, e.g., GROUP BY first_name, email.
- How do I inspect the full duplicate rows instead of just the count?
- Use a window function: COUNT(*) OVER (PARTITION BY email) > 1 inside a CTE.
Related Formulas & Guides
- Calculate a Running Total in SQL Using Window Functions — Calculate a cumulative running total in PostgreSQL, Snowflake, or BigQuery with SUM() OVER (ORDER BY). Test and convert to Excel/Pandas.
- Select Top N Rows Per Group in SQL with ROW_NUMBER() — Retrieve the top 3 highest deals or latest orders per category in SQL using ROW_NUMBER() OVER (PARTITION BY). Full guide and examples.
- Calculate Month-over-Month (MoM) Growth in SQL with LAG() — Compute MoM percentage growth in PostgreSQL, BigQuery, and Snowflake using the LAG() window function and NULLIF to prevent zero-division.
- Delete Duplicate Rows in SQL While Keeping One Record — Safely deduplicate SQL tables using a CTE and ROW_NUMBER() window function while preserving the newest or lowest ID record cleanly.