ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

Detect Duplicates Using GROUP BY and HAVING

SQL MySQL

Goal -- WPM

Ready
Exercise Algorithm Area
1SELECT
2email,
3phone_number,
4COUNT(*) AS duplicate_count
5FROM
6customers
7WHERE
8email IS NOT NULL AND phone_number IS NOT NULL -- Exclude rows where either is NULL for simplicity in this example
9GROUP BY
10email,
11phone_number
12HAVING
13COUNT(*) > 1;
14
15-- Explanation:
16-- This query identifies combinations of email and phone_number that appear more than once.
17-- It assumes that a duplicate is defined by having the same email AND the same phone number.
18
19-- To also find records where only email is duplicated, or only phone_number is duplicated:
20-- SELECT email, COUNT(*) FROM customers GROUP BY email HAVING COUNT(*) > 1;
21-- SELECT phone_number, COUNT(*) FROM customers GROUP BY phone_number HAVING COUNT(*) > 1;
22
23-- Handling NULLs:
24-- If you want to consider rows with NULL email and NULL phone_number as duplicates,
25-- you would need a more complex approach, possibly using UNION ALL or conditional aggregation.
26-- For instance, to count NULL emails as a group:
27-- SELECT COUNT(*) FROM customers WHERE email IS NULL;
28-- The current query explicitly excludes rows with NULL email or phone_number from the duplicate check.
29
30-- Example Scenario:
31-- If the customers table has:
32-- (1, 'Alice', 'alice@example.com', '111-1111')
33-- (2, 'Bob', 'bob@example.com', '222-2222')
34-- (3, 'Alice Duplicate', 'alice@example.com', '111-1111')
35-- (4, 'Charlie', 'charlie@example.com', '333-3333')
36-- (5, 'Bob Duplicate', 'bob@example.com', '222-2222')
37-- The query would return:
38-- ('alice@example.com', '111-1111', 2)
39-- ('bob@example.com', '222-2222', 2)
Algorithm description viewbox

Detect Duplicates Using GROUP BY and HAVING

Algorithm description:

This scenario focuses on identifying duplicate records within a MySQL table using standard SQL aggregation techniques. Specifically, it targets duplicates based on a composite key (email and phone number). This is a common data cleaning task essential for maintaining data integrity and accuracy in databases.

Algorithm explanation:

The query first filters out rows where `email` or `phone_number` is NULL, as `GROUP BY` treats NULLs as distinct values by default, and we're looking for exact matches. It then groups the remaining rows by the combination of `email` and `phone_number`. The `COUNT(*)` aggregate function counts the number of rows in each group. The `HAVING COUNT(*) > 1` clause filters these groups, returning only those combinations that appear more than once, indicating duplicates. The time complexity is typically O(N log N) due to the sorting involved in grouping, or O(N) if an appropriate index exists. Space complexity is O(K) where K is the number of distinct `(email, phone_number)` combinations.

Pseudocode:

1. Select `email`, `phone_number`, and the count of occurrences.
2. From the `customers` table.
3. Filter out rows where `email` or `phone_number` is NULL.
4. Group the results by `email` and `phone_number`.
5. Use `HAVING` to filter groups where the count is greater than 1.