Deduplicate User Login Sessions
SQL coding challenge · Difficulty: medium · Topic: Deduplication with ROW_NUMBER · +100 XP
In an online service, users may have multiple login sessions recorded due to technical issues. Given a 'logins' table, deduplicate login sessions for each user by keeping only the earliest session for any given day. Return user_id, login_date, and login_time for each unique user and day combination, ordered by user_id and login_date.
Example input
`
logins table:
login_id | user_id | login_date | login_time
1 | 201 | 2024-03-01 | 09:00:00
2 | 201 | 2024-03-01 | 12:30:00
3 | 201 | 2024-03-02 | 08:45:00
4 | 202 | 2024-03-01 | 10:15:00
5 | 202 | 2024-03-01 | 14:00:00
6 | 203 | 2024-03-03 | 11:00:00
7 | 203 | 2024-03-03 | 11:05:00
`
Expected output
`
user_id | login_date | login_time
201 | 2024-03-01 | 09:00:00
201 | 2024-03-02 | 08:45:00
202 | 2024-03-01 | 10:15:00
203 | 2024-03-03 | 11:00:00
`
What this SQL challenge teaches you
“Deduplicate User Login Sessions” is a medium-level SQL challenge focused on Deduplication with ROW_NUMBER. Working through it gives you hands-on practice with ROW_NUMBER, deduplication, date — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.
Concepts covered
- ROW_NUMBER
- deduplication
- date
How to approach it
If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:
- 1 ≤ number of rows ≤ 100,000
- Each user_id appears between 1 and 500 times
- login_date values are valid dates in YYYY-MM-DD format
- login_time values are valid times in HH:MM:SS format
- Return columns: user_id (INTEGER), login_date (DATE), login_time (TIME), ordered by user_id ASC, login_date ASC
How to practise it on PySpark.in
Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.
Related SQL challenges
- Remove Duplicate Customer Records
- Remove Duplicate Customer Records (PySpark)
- Top 3 Products per Category
- Running Total Revenue
- Find Duplicate Emails
- Median Salary per Department
Frequently asked questions
Do I need to install Spark or a database to solve this?
No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.
Is this challenge free?
Yes - the problem, the sample dataset, the hints and unlimited test runs are free.
What level is it?
It is rated medium and covers Deduplication with ROW_NUMBER.