Querydrill

Level 6: Work mode Exercise 130 of 161 Hard Clean messy data

Keep one account per person

An SQL exercise from Querydrill. Solve it with a SQL editor in your browser, checked on the rows it returns and again on changed data, and it is checked the moment you submit.

Try it yourself

From Sam in Support: "For everyone who signed up more than once with the same email typed differently, I only want their earliest signup. The signup dates come in mixed formats, and if two signups fall on the same day the lowest id wins. Send me one row per person." Return the id of the account to keep and the email in lowercase with no spaces around it.

signups_raw (first 12 of 14 rows)

idemailcountryplansigned_up
1anna@mail.comUSPro2025-01-04
2 Anna@Mail.comus pro2025-01-09 08:30:00
3bruno@mail.comBRFREE2025-01-15T10:02:00
4chloe@mail.com UsPro2025-02-02
5dev@mail.comINNULL2025-02-11 17:45:00
6elif@mail.comtrfree2025-02-20
7farah@mail.comUSTeam2025-03-01T09:00:00
8CHLOE@mail.com USpro2025-03-05
9gus@mail.comnlFree2025-03-14
10hana@mail.comNULLPro2025-03-22 12:00:00
11ivo@mail.comNL team2025-04-02
12jun@mail.comNULL2025-04-10T16:20:00

Free, no account needed. PostgreSQL runs in your browser and your answer is checked again on changed data.

What this exercise teaches

Deduplication with ROW_NUMBER is the standard pattern: partition by what makes two rows the same, order by which one should win, keep row 1. Taking MIN(id) per email only works while ids happen to follow the signup dates, which real data does not promise.

A hint if you get stuck

ROW_NUMBER() OVER (PARTITION BY the cleaned email ORDER BY substr(signed_up, 1, 10), id) numbers each person's accounts from oldest to newest. Keep number 1 in an outer query.

Background you need

Common mistakes on this exercise

  • Lowest id is not always the earliest signup.
  • Sorted on the raw text: a date with a time sorts after a plain date on the same day.

Each of these returns a result that looks plausible. The checker catches them because it runs your query on changed data as well.

Keep going on the practice page

Your first 8 exercises on Querydrill are free, with no account. The practice page has the full editor, timed mode and all the exercises in order. A free account shows 3 reference solutions a day; Pro unlocks every exercise, every solution, timed mode and the certificate.

Open this exercise

More practice on the same topic

More exercises in Work mode