
Meta's business analytics team is reviewing lead data captured from Meta Lead Ads. Some leads were submitted multiple times for the same ad, and you need to identify those duplicate records and show how you would remove them while keeping one record per duplicate group.
Write a SQL query to find duplicate lead submissions, where a duplicate is defined as the same ad, email, and submission date. Keep the earliest lead_id as the canonical record.
lead_submissions.ad_campaigns.ROW_NUMBER().campaign_name, submission_date, email, and lead_id.lead_submissions| column | type | description |
|---|---|---|
| lead_id | INT | Unique lead submission ID |
| ad_id | INT | Ad that generated the lead |
| VARCHAR(255) | Submitted email address | |
| full_name | VARCHAR(255) | Lead name |
| submission_date | DATE | Date of submission |
| source_surface | VARCHAR(100) | Meta surface where the lead was captured |
ad_campaigns| column | type | description |
|---|---|---|
| ad_id | INT | Unique ad ID |
| campaign_name | VARCHAR(255) | Campaign tied to the ad |
| objective | VARCHAR(100) | Campaign objective |
lead_submissions| lead_id | ad_id | full_name | submission_date | source_surface | |
|---|---|---|---|---|---|
| 104 | 202 | liam@example.com | Liam Chen | 2024-05-03 | |
| 101 | 201 | ava@example.com | Ava Patel | 2024-05-01 | |
| 109 | 205 | mia@example.com | Mia Gomez | 2024-05-05 | Messenger |
| 103 | 201 | ava@example.com | Ava Patel | 2024-05-01 | |
| 106 | 203 | noah@example.com | Noah Kim | 2024-05-02 | |
| 110 | 205 | mia@example.com | Mia Gomez | 2024-05-05 | Messenger |
| 102 | 201 | ava@example.com | Ava Patel | 2024-05-01 | |
| 108 | 204 | null | Emma Reed | 2024-05-04 | |
| 105 | 202 | liam@example.com | Liam Chen | 2024-05-03 | |
| 107 | 204 | emma@example.com | Emma Reed | 2024-05-04 |
ad_campaigns| ad_id | campaign_name | objective |
|---|---|---|
| 205 | Messenger Lead Gen | Leads |
| 201 | SMB Signup Q2 | Leads |
| 202 | Reels Creator Outreach | Leads |
| 203 | Shops Seller Onboarding | Conversions |
| 204 | WhatsApp Business Trial | Leads |
| 206 | Advantage+ Retail Push | Sales |
| 207 | Creator Marketplace Growth | Leads |
| 208 | Threads Brand Awareness | Reach |
| campaign_name | lead_id | ad_id | submission_date | row_num | duplicate_count | action | |
|---|---|---|---|---|---|---|---|
| Messenger Lead Gen | 109 | 205 | mia@example.com | 2024-05-05 | 1 | 2 | keep |
| Messenger Lead Gen | 110 | 205 | mia@example.com | 2024-05-05 | 2 | 2 | remove |
| Reels Creator Outreach | 104 | 202 | liam@example.com | 2024-05-03 | 1 | 2 | keep |
| Reels Creator Outreach | 105 | 202 | liam@example.com | 2024-05-03 | 2 | 2 | remove |
| SMB Signup Q2 | 101 | 201 | ava@example.com | 2024-05-01 | 1 | 3 | keep |
| SMB Signup Q2 | 102 | 201 | ava@example.com | 2024-05-01 | 2 | 3 | remove |
| SMB Signup Q2 | 103 | 201 | ava@example.com | 2024-05-01 | 3 | 3 | remove |
| Column | Type | Description |
|---|---|---|
| lead_idPK | INT | Unique lead submission identifier |
| ad_id | INT | Ad identifier associated with the lead |
| VARCHAR(255) | Lead email address | |
| full_name | VARCHAR(255) | Lead full name |
| submission_date | DATE | Date the lead was submitted |
| source_surface | VARCHAR(100) | Meta surface where the lead was captured |
| Column | Type | Description |
|---|---|---|
| ad_idPK | INT | Unique ad identifier |
| campaign_name | VARCHAR(255) | Campaign name for the ad |
| objective | VARCHAR(100) | Campaign objective |
| ad_id | campaign_name | objective |
|---|---|---|
| 205 | Messenger Lead Gen | Leads |
| 201 | SMB Signup Q2 | Leads |
| 202 | Reels Creator Outreach | Leads |
| 203 | Shops Seller Onboarding | Conversions |
| 204 | WhatsApp Business Trial | Leads |
| 206 | Advantage+ Retail Push | Sales |
| 207 | Creator Marketplace Growth | Leads |
| 208 | Threads Brand Awareness | Reach |
| 1 | Restaurant Orders Launch | Shops |
| 2 | Restaurant Orders Launch | Catalog Sales |
| 3 | Meta Verified Trial | Demand Generation |
| 4 | Advantage+ Retail Push | null |
| 5 | Enterprise Webinar Signup | Commerce |
| 6 | Nonprofit Donor Campaign | Prospecting |
| 7 | Cross-Border Sellers Push | null |
| 8 | SMB Signup Q2 | Conversions |
| 9 | Enterprise Webinar Signup | Retention |
| 10 | Global SMB Acquisition | Retargeting |
| 11 | Global SMB Acquisition | Sales |
| 12 | WhatsApp Business Trial | Store Traffic |
| 13 | SMB Signup Q2 | Video Views |
| 14 | Click to Message Boost | Messenger |
| 15 | Travel Leads Summer | Messages |
| 16 | SMB Signup Q2 | Messenger |
| 17 | Messenger Lead Gen | Acquisition |
| 18 | Meta Verified Trial | |
| 19 | Nonprofit Donor Campaign | Brand Awareness |
| lead_id | ad_id | full_name | submission_date | source_surface | |
|---|---|---|---|---|---|
| 104 | 202 | liam@example.com | Liam Chen | 2024-05-03 | |
| 101 | 201 | ava@example.com | Ava Patel | 2024-05-01 | |
| 109 | 205 | mia@example.com | Mia Gomez | 2024-05-05 | Messenger |
| 103 | 201 | ava@example.com | Ava Patel | 2024-05-01 | |
| 106 | 203 | noah@example.com | Noah Kim | 2024-05-02 | |
| 110 | 205 | mia@example.com | Mia Gomez | 2024-05-05 | Messenger |
| 102 | 201 | ava@example.com | Ava Patel | 2024-05-01 | |
| 108 | 204 | Emma Reed | 2024-05-04 | ||
| 105 | 202 | liam@example.com | Liam Chen | 2024-05-03 | |
| 107 | 204 | emma@example.com | Emma Reed | 2024-05-04 | |
| 1 | 59 | ava@example.com | Logan Hall | 2024-05-05 | Instagram Profile |
| 2 | 23 | abigail@example.com | James Wright | 2024-04-30 | Facebook Feed |
| 3 | 96 | chloe@example.com | Evelyn King | 2024-05-07 | Facebook Groups |
| 4 | 57 | null | null | 2024-05-01 | null |
| 5 | 53 | lucas@example.com | Noah Kim | 2024-04-28 | Instagram Reels |
| 6 | 71 | evelyn@example.com | Elijah Allen | 2024-04-28 | Instagram Shop |
| 7 | 13 | amelia@example.com | null | 2024-05-04 | Shops |
| 8 | 50 | mia@example.com | Daniel Perez | 2024-05-02 | Facebook Search |
| 9 | 14 | grace@example.com | Daniel Perez | 2024-05-08 | Creator Marketplace |
| 10 | 3 | ella@example.com | Grace Torres | 2024-05-01 | null |
| 11 | 58 | ava@example.com | Grace Torres | 2024-05-02 | |
| 12 | 71 | nora@example.com | Grace Torres | 2024-05-04 | Messenger Inbox |
| 13 | 20 | null | Jackson Flores | 2024-05-06 | Threads Feed |
| 14 | 17 | emma@example.com | Elijah Allen | 2024-04-28 | Instagram Stories |
| 15 | 87 | amelia@example.com | Grace Torres | 2024-05-08 | Shops |
| 16 | 54 | henry@example.com | Evelyn King | 2024-04-28 | Instagram Explore |
| 17 | 75 | noah@example.com | Logan Hall | 2024-04-30 | Audience Network |
| campaign_name | lead_id | ad_id | submission_date | row_num | duplicate_count | action | |
|---|---|---|---|---|---|---|---|
| Messenger Lead Gen | 109 | 205 | mia@example.com | 2024-05-05 | 1 | 2 | keep |
| Messenger Lead Gen | 110 | 205 | mia@example.com | 2024-05-05 | 2 | 2 | remove |
| Reels Creator Outreach | 104 | 202 | liam@example.com | 2024-05-03 | 1 | 2 | keep |
| Reels Creator Outreach | 105 | 202 | liam@example.com | 2024-05-03 | 2 | 2 | remove |
| SMB Signup Q2 | 101 | 201 | ava@example.com | 2024-05-01 | 1 | 3 | keep |
| SMB Signup Q2 | 102 | 201 | ava@example.com | 2024-05-01 | 2 | 3 | remove |
| SMB Signup Q2 | 103 | 201 | ava@example.com | 2024-05-01 | 3 | 3 | remove |