# Duplicate Contact Emails

**Type:** SOQL · **Difficulty:** Hard · **Topics:** GROUP BY, HAVING, COUNT

Practice this problem interactively (write and run your SOQL against realistic data): https://www.forcechallenges.com/problems/duplicate-contact-emails

## Problem

**Scenario**
The data-quality team is cleaning up the CRM. Several Contacts share the same email address, which causes duplicate outreach and failed merges. Before merging anything they want a list of the email addresses that are used more than once, and how often.

**Task**
Write one SOQL query on `Contact` that:
1. Ignores Contacts that have no email (`Email` is null).
2. Groups the remaining Contacts by `Email`.
3. Keeps only emails that appear on **more than one** Contact.
4. Sorts by the number of Contacts, **highest first**. Break ties by `Email` ascending.

**Expected output**
One row per duplicated email with two columns: `Email` and `dupes` (the number of Contacts that use it).

**Example**
Contacts: three use `shared@example.com`, two use `pair@example.com`, one uses `solo@example.com`, and two have no email.

| Email | dupes |
|---|---:|
| shared@example.com | 3 |
| pair@example.com | 2 |

`solo@example.com` is dropped because it is used only once, and the Contacts with no email are ignored.

## Constraints

Use one aggregate query on Contact. Exclude null emails with WHERE, group by Email, and keep groups with more than one Contact using HAVING. Alias the count as `dupes`. Order by `dupes` descending, then Email ascending.

---
Source: ForceChallenges · last updated 2026-10-24
