sqlcmd.net validated sql reference
intermediate data-quality MySQL MariaDB SQL Server PostgreSQL SQLite

Find Duplicate Values With GROUP BY And HAVING

Group rows by the candidate key and keep only values that appear more than once.

Docker-validated Not currently validation-green

List email addresses that appear more than once

[email protected] appears only once, so it is filtered out by the HAVING clause. Ada and Bob each appear twice, so they are the values flagged as duplicates.

Rows loaded before the example query runs.
Setup
CREATE TABLE users (id INT, email VARCHAR(100));

INSERT INTO
  users (id, email)
VALUES
  (1, '[email protected]'),
  (2, '[email protected]'),
  (3, '[email protected]'),
  (4, '[email protected]'),
  (5, '[email protected]');
Shared across supported engines.
SQL
SELECT
  email,
  COUNT(*) AS duplicate_count
FROM
  users
GROUP BY
  email
HAVING
  COUNT(*) > 1
ORDER BY
  email;
Returned rows for the shared example.

All engines use the same `GROUP BY` plus `HAVING COUNT(*) > 1` duplicate check here.

Where this command helps.

  • checking whether imported identifiers were loaded twice
  • finding repeated emails, usernames, or lookup values before cleanup

What the command is doing.

Duplicate detection usually starts with a GROUP BY on the value that should be unique, followed by HAVING COUNT(*) > 1. That pattern shows which values are duplicated and how many times they appear. It is the fastest way to audit imports, email addresses, external IDs, or any field that should not repeat.