Deduplicate Marketing Leads
Lead forms produced duplicates. Two leads are the same person when their emails match after trimming spaces and lowercasing. Leads with a NULL email are discarded.
Keep one lead per person: the earliest created_at, and if that ties, the smallest lead_id. Show the email in its normalised form (trimmed, lowercase).
Return columns lead_id, email, source. Row order does not matter.
Schema
CREATE TABLE leads ( lead_id INTEGER PRIMARY KEY, email TEXT, source TEXT NOT NULL, created_at TEXT NOT NULL );
Sample data
leads
| lead_id | source | created_at | |
|---|---|---|---|
| 1 | maya@corp.com | webinar | 2026-03-01 10:00:00 |
| 2 | Maya@Corp.com | google_ads | 2026-02-20 09:00:00 |
| 3 | liam@shop.ae | 2026-03-02 12:00:00 | |
| 4 | NULL | webinar | 2026-03-02 13:00:00 |
| 5 | noor@bank.ae | referral | 2026-03-03 08:30:00 |
| 6 | LIAM@shop.ae | webinar | 2026-03-05 16:00:00 |
Hint 1
Partition by the normalised email LOWER(TRIM(email)).
Hint 2
Filter out NULL emails before ranking, or every NULL lead lands in one group.
Good practice for: Data Engineer, Digital Marketing Specialist, Data Analyst, Backend Developer
SQL (SQLite)
Run checks your query on the sample data (Ctrl + '). Submit also checks hidden databases.