Return Unique Name and Email Pairs Without Using DISTINCT
Company: Goldman Sachs
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Return each unique (`name`, `email`) pair from the `Contacts` table without using `DISTINCT`, for example with `GROUP BY` or `ROW_NUMBER()`. For each group of duplicate rows, keep the row with the smallest `id`, and order the results by that smallest `id` ascending.
```hint Ordering
The sort key is not one of the output columns. Ask what value each group carries that you could sort on.
```
### Input Table(s)
Table: `Contacts`
| Column | Type | Meaning |
|---|---|---|
| id | INT | Row identifier |
| name | VARCHAR(100) | Contact name |
| email | VARCHAR(100) | Contact 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: `name`, `email`.
- Exactly one row per distinct (`name`, `email`) pair in the table.
- Order by the smallest `id` of each pair, ascending. Each `id` belongs to exactly one pair, so no two output rows share a sort key and the order is fully determined. The `id` itself is not returned.
- No rounding applies.
### Example
Input `Contacts`:
| id | name | email |
|---|---|---|
| 1 | Alice | alice@example.com |
| 2 | Bob | bob@example.com |
| 3 | Alice | alice@example.com |
| 4 | Carol | carol@example.com |
| 5 | Bob | bob@example.com |
| 6 | alice | alice@example.com |
Expected output:
| name | email |
|---|---|
| Alice | alice@example.com |
| Bob | bob@example.com |
| Carol | carol@example.com |
| alice | alice@example.com |
(Alice, alice@example.com) appears at ids 1 and 3, so it is returned once with first id 1. Bob's pair appears at ids 2 and 5, so its first id is 2, and Carol appears once at id 4. Row 6 writes the name in lowercase, so (alice, alice@example.com) is a separate pair with first id 6 and comes last.
### Constraints and Clarifications
- The table has 1 to 10,000 rows.
- `id` is unique. `name` and `email` may repeat across rows.
- The query must not use `DISTINCT` anywhere, including `DISTINCT ON` and `COUNT(DISTINCT ...)`.
- Two rows form the same pair only when `name` and `email` both match exactly, the way `DISTINCT` compares them. Comparison is case-sensitive and nothing is trimmed, so `Alice` and `alice` are different names.
- A NULL matches only another NULL in the same column, again as `DISTINCT` treats it. Two rows with a NULL `name` and the same `email` form one pair, which is returned with a NULL `name`.
Overview: Write a SQL query that returns each unique name and email pair from a contacts table without the DISTINCT keyword, ordered by the first id where each pair appears. It tests deduplication with grouping or window functions, ordering by a value that is not selected, and exact matching rules for case and NULLs.