Find Duplicate Employee Records by Name and Email, Keeping the Smallest ID

Quick Overview

Write a SQL query that returns every duplicate employee row sharing a name and email with a row that has a smaller id. It tests grouping, filtering against an aggregate subquery, and careful handling of exact case-sensitive matches and NULL values.

Find Duplicate Employee Records by Name and Email, Keeping the Smallest ID

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Each row of the `Employees` table is one employee record. A row is a duplicate if another row has the same `name` and `email` but a smaller `id`. Within each (`name`, `email`) group, the row with the smallest `id` is kept. Return the `id`, `name` and `email` of every duplicate row, ordered by `id` ascending. ```hint Where to start Work out which `id` each (`name`, `email`) group keeps before you look for the rows to return. ``` ### Input Table(s) Table: `Employees` | Column | Type | Meaning | |---|---|---| | id | INT | Row identifier | | name | VARCHAR(100) | Employee name | | email | VARCHAR(100) | Employee email address | Primary key: `id`. There is no other key or unique constraint, so `name` and `email` may repeat across rows. ### Output Contract - Columns, in this order: `id`, `name`, `email`, copied unchanged from the duplicate row. - One output row per duplicate record. A row that is kept (the smallest `id` of its group) does not appear. - Order by `id` ascending. `id` is unique, so the order is fully determined. - No rounding applies. ### Example Input `Employees`: | id | name | email | |---|---|---| | 1 | Alice | alice@example.com | | 2 | Bob | bob@example.com | | 3 | Alice | alice@example.com | | 4 | Charlie | charlie@example.com | | 5 | Bob | bob@example.com | | 6 | alice | alice@example.com | Expected output: | id | name | email | |---|---|---| | 3 | Alice | alice@example.com | | 5 | Bob | bob@example.com | Rows 3 and 5 repeat the (`name`, `email`) pairs of rows 1 and 2, which have smaller ids and are kept. Row 4 is the only Charlie row. Row 6 writes the name in lowercase, so it forms its own group and is kept. ### Constraints and Clarifications - The table has 1 to 10,000 rows. - `id` is unique. `name` and `email` may repeat across rows. - Comparisons on `name` and `email` are exact and case-sensitive: `Alice` and `alice` are different names. No trimming or other normalization is applied. - A group with three or more rows returns every row except the one with the smallest `id`. - `name` and `email` are not declared NOT NULL. A NULL matches only another NULL in the same column, the way `GROUP BY` treats it, so two rows with a NULL `name` and the same `email` form one group. - If no row is a duplicate, the result is empty.

Overview: Write a SQL query that returns every duplicate employee row sharing a name and email with a row that has a smaller id. It tests grouping, filtering against an aggregate subquery, and careful handling of exact case-sensitive matches and NULL values.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Each row of the Employees table is one employee record. A row is a duplicate if another row has the same name and email but a smaller id. Within each (name, email) group, the row with the smallest id is kept. Return the id, name and email of every duplicate row, ordered by id ascending.

Input Table(s)

Table: Employees

ColumnTypeMeaning
idINTRow identifier
nameVARCHAR(100)Employee name
emailVARCHAR(100)Employee email address

Primary key: id. There is no other key or unique constraint, so name and email may repeat across rows.

Output Contract

  • Columns, in this order: id , name , email , copied unchanged from the duplicate row.
  • One output row per duplicate record. A row that is kept (the smallest id of its group) does not appear.
  • Order by id ascending. id is unique, so the order is fully determined.
  • No rounding applies.

Example

Input Employees:

idnameemail
1Alicealice@example.com
2Bobbob@example.com
3Alicealice@example.com
4Charliecharlie@example.com
5Bobbob@example.com
6alicealice@example.com

Expected output:

idnameemail
3Alicealice@example.com
5Bobbob@example.com

Rows 3 and 5 repeat the (name, email) pairs of rows 1 and 2, which have smaller ids and are kept. Row 4 is the only Charlie row. Row 6 writes the name in lowercase, so it forms its own group and is kept.

Constraints and Clarifications

  • The table has 1 to 10,000 rows.
  • id is unique. name and email may repeat across rows.
  • Comparisons on name and email are exact and case-sensitive: Alice and alice are different names. No trimming or other normalization is applied.
  • A group with three or more rows returns every row except the one with the smallest id .
  • name and email are not declared NOT NULL. A NULL matches only another NULL in the same column, the way GROUP BY treats it, so two rows with a NULL name and the same email form one group.
  • If no row is a duplicate, the result is empty.