Delete Repeated Email Rows While Keeping the Smallest ID

Quick Overview

Delete repeated email rows from the Person table while retaining the original row with the smallest ID for each email.

Delete Repeated Email Rows While Keeping the Smallest ID

Company: Google

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem Delete duplicate-email rows from Person itself, leaving the row with the smallest id for each email. SQL must use a DELETE statement, not a SELECT projection. The observed answer is the remaining Person table, whose row order is immaterial. Emails contain no uppercase letters. ### Tables Table: `Person` ```text +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id is the primary key (column with unique values) for this table. Each row of this table contains an email. The emails will not contain uppercase letters. ``` ### Constraints - In this version, every `email` value is non-NULL. ### Example ```text Person before (id,email): (8,a@example.net), (3,a@example.net), (6,b@example.net), (9,b@example.net) Person after (id,email): (3,a@example.net), (6,b@example.net) ``` ### What a Strong Answer Covers - Modify the Person table and keep exactly the smallest-ID row for each email. - Leave unique emails and their original IDs unchanged. ### Follow-up Questions - How would you preview the rows to delete before applying the change? - How would you prevent future duplicate emails after cleaning the table?

Overview: Delete repeated email rows from the Person table while retaining the original row with the smallest ID for each email.

Google logo
Google
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

Delete duplicate-email rows from Person itself, leaving the row with the smallest id for each email. SQL must use a DELETE statement, not a SELECT projection. The observed answer is the remaining Person table, whose row order is immaterial. Emails contain no uppercase letters.

Tables

Table: Person

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| email       | varchar |
+-------------+---------+
id is the primary key (column with unique values) for this table.
Each row of this table contains an email. The emails will not contain uppercase letters.

Constraints

  • In this version, every email value is non-NULL.

Example

Person before (id,email): (8,a@example.net), (3,a@example.net), (6,b@example.net), (9,b@example.net)
Person after (id,email): (3,a@example.net), (6,b@example.net)

What a Strong Answer Covers Guidance

  • Modify the Person table and keep exactly the smallest-ID row for each email.
  • Leave unique emails and their original IDs unchanged.

Follow-up Questions Guidance

  • How would you preview the rows to delete before applying the change?
  • How would you prevent future duplicate emails after cleaning the table?