Find Duplicate Rows in MariaDB with GROUP BY and HAVING

To find duplicates in MariaDB, group by the columns that define a duplicate and use HAVING COUNT(*) > 1 to keep groups with at least two rows. Join those groups back to the source to list individual rows and IDs. Decide which columns must match and whether missing values count before running the queries.

Choose the duplicate key and prepare test data

A repeated email is different from a repeated email and country combination. The primary key id is unique, so grouping by it would hide these duplicates. Choose the columns based on your business rule. Run the setup in a learning test database with CREATE, INSERT, and SELECT privileges. If the table already exists, use another practice environment.

USE learning;
CREATE TABLE duplicate_member_demo (
    id INT PRIMARY KEY,
    email VARCHAR(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL,
    country CHAR(2) NULL,
    display_name VARCHAR(30) NOT NULL
) ENGINE=InnoDB;
INSERT INTO duplicate_member_demo VALUES
    (1,'a@example.test','KR','A first'),
    (2,'a@example.test','KR','A second'),
    (3,'a@example.test','US','A abroad'),
    (4,'b@example.test','KR','B single'),
    (5,NULL,'KR','Unknown first'),
    (6,NULL,'KR','Unknown second'),
    (7,'c@example.test','KR','C first'),
    (8,'c@example.test','KR','C second'),
    (9,'d@example.test',NULL,'D first'),
    (10,'d@example.test',NULL,'D second');
SELECT id, email, country, display_name
FROM duplicate_member_demo ORDER BY id;

There are ten rows and the addresses are fictional. a@example.test appears three times, but adding country splits it into two KR rows and one US row. NULL represents missing or unknown data and differs from an empty string. A missing email does not establish that two rows represent the same person.

Find duplicates in one column and compare counts

The first two queries count duplicates across all emails and then among rows with a known email. WHERE filters rows before aggregation, while HAVING filters the resulting groups.

SELECT email, COUNT(*) AS row_count
FROM duplicate_member_demo
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY email;

SELECT email, COUNT(*) AS row_count
FROM duplicate_member_demo
WHERE email IS NOT NULL
GROUP BY email
HAVING COUNT(*) > 1
ORDER BY email;
SELECT email, COUNT(*) AS row_count, COUNT(email) AS known_email_count
FROM duplicate_member_demo
GROUP BY email
ORDER BY email;

The first query finds four groups: NULL with two rows, address a with three, address c with two, and address d with two. The second leaves three groups by excluding NULL. GROUP BY puts the missing emails in one group. In the third query, that group has COUNT(*) of 2 and COUNT(email) of 0 because the latter counts known values. MariaDB COUNT syntax MariaDB NULL grouping rules

Find repeated combinations of two columns

List both email and country in GROUP BY when both must match. Concatenating the values can mix different keys when separators occur in the data or a value is NULL.

SELECT email, country, COUNT(*) AS row_count
FROM duplicate_member_demo
GROUP BY email, country
HAVING COUNT(*) > 1
ORDER BY email, country;

The results are (NULL, KR, 2), (a@example.test, KR, 2), (c@example.test, KR, 2), and (d@example.test, NULL, 2). The US row for address a stands alone. Selecting ungrouped IDs or names with SELECT * fails under ONLY_FULL_GROUP_BY. With that mode off, the row supplying such a value is undefined. MariaDB GROUP BY syntax

Retrieve the source rows within the same scope

The first query joins the aggregate results to the original rows. The null-safe equality operator <=> is true when both inputs are NULL and false when only one is. MariaDB null-safe equality

SELECT m.id, m.email, m.country, m.display_name, d.row_count
FROM duplicate_member_demo AS m
JOIN (
    SELECT email, country, COUNT(*) AS row_count
    FROM duplicate_member_demo
    GROUP BY email, country
    HAVING COUNT(*) > 1
) AS d
  ON m.email <=> d.email
 AND m.country <=> d.country
ORDER BY m.id;

The query returns IDs 1, 2, 5, 6, 7, 8, 9, and 10, each with row_count of 2. Using = in the join would lose IDs 5 and 6 and IDs 9 and 10. If your business rule excludes missing values, apply it consistently to both the grouping and the source rows.

WITH duplicate_scope AS (
    SELECT id, email
    FROM duplicate_member_demo
    WHERE id >= 2 AND email IS NOT NULL
),
duplicate_keys AS (
    SELECT email, COUNT(*) AS row_count
    FROM duplicate_scope
    GROUP BY email
    HAVING COUNT(*) > 1
)
SELECT s.id, s.email, d.row_count
FROM duplicate_scope AS s
JOIN duplicate_keys AS d ON s.email = d.email
ORDER BY s.id;

The second query puts known emails with IDs of 2 or greater into a common table expression (CTE). It uses that same scope for counting and returning rows, so it returns IDs 2, 3, 7, 8, 9, and 10. Joining to the full table would also pull in ID 1, even though it was outside the scope. Share date and status filters the same way.

Distinguish duplicate groups, matching rows, and extra rows

The number of duplicate keys, the number of rows in their groups, and the number of rows beyond one per key answer different questions.

SELECT COUNT(*) AS duplicate_groups,
       COALESCE(SUM(row_count), 0) AS rows_in_duplicate_groups,
       COALESCE(SUM(row_count - 1), 0) AS extra_rows
FROM (
    SELECT COUNT(*) AS row_count
    FROM duplicate_member_demo
    GROUP BY email, country
    HAVING COUNT(*) > 1
) AS d;

The query returns 4, 8, 4. Four duplicate keys account for eight rows. Keeping one row per group would leave four extra rows. With no duplicate group, COUNT(*) returns 0 and SUM() returns NULL, so COALESCE displays zero for both sums.

Preview extra rows under a specific keep rule

This example treats the lowest ID in each email and country group as the row to keep. The ROW_NUMBER() window function numbers the rows by ID. The outer query filters for positions after the first. MariaDB ROW_NUMBER syntax

WITH ranked_members AS (
    SELECT id, email, country,
           ROW_NUMBER() OVER (
               PARTITION BY email, country
               ORDER BY id
           ) AS duplicate_position
    FROM duplicate_member_demo
)
SELECT id, email, country, duplicate_position
FROM ranked_members
WHERE duplicate_position > 1
ORDER BY id;

It returns IDs 2, 6, 8, and 10, each with duplicate_position of 2. If your real rule uses the latest update date or a verified status, order by that condition first and use a unique ID to break ties. The output is a review list. Before any delete or merge, confirm record validity, references, and recovery options.

Check case, trailing spaces, and uniqueness

A collation defines how text is compared and sorted. This query switches comparison rules without changing the stored data, once to ignore case and once to distinguish it. MariaDB character sets and collations

SELECT
    (_utf8mb4'Case@example.test' COLLATE utf8mb4_general_ci =
     _utf8mb4'case@example.test' COLLATE utf8mb4_general_ci) AS case_insensitive,
    (_utf8mb4'Case@example.test' COLLATE utf8mb4_bin =
     _utf8mb4'case@example.test' COLLATE utf8mb4_bin) AS case_sensitive;

The result is 1, 0. Treatment of trailing spaces depends on the collation and data type. Decide whether to use LOWER or TRIM based on the business key. DISTINCT shows a value once in the result. If a join creates extra rows when the source has no duplicates, inspect the join keys for many-to-many matches. A UNIQUE constraint can enforce a business rule, but MariaDB allows multiple NULL values in a unique index. For large tables, inspect the execution plan and indexes. When comparing several reports, consider whether they read data from the same point in time. MariaDB unique indexes guide