Longest Session Duration per User

SQL coding challenge · Difficulty: easy · Topic: Session Analysis · +50 XP

A mobile app records every user session with a start and an end timestamp. For each user, find the length of their longest session in minutes.

Return user_id and longest_session_minutes, one row per user, ordered by user_id ascending. Session lengths are whole minutes (every timestamp in this table falls on a whole minute). A session can run past midnight -- its length is still the time between start and end.

Example Input

`

sessions table:

session_id | user_id | start_time | end_time

1 | 1 | 2024-03-01 09:00:00 | 2024-03-01 09:45:00

2 | 1 | 2024-03-01 13:00:00 | 2024-03-01 14:45:00

3 | 1 | 2024-03-02 08:00:00 | 2024-03-02 08:30:00

4 | 2 | 2024-03-01 10:00:00 | 2024-03-01 11:30:00

5 | 2 | 2024-03-03 22:30:00 | 2024-03-03 23:10:00

6 | 3 | 2024-03-02 23:30:00 | 2024-03-03 00:45:00

7 | 4 | 2024-03-04 12:00:00 | 2024-03-04 12:20:00

`

Expected Output

`

user_id | longest_session_minutes

1 | 105

2 | 90

3 | 75

4 | 20

`

User 3's only session crosses midnight and lasts 75 minutes.

What this SQL challenge teaches you

“Longest Session Duration per User” is a easy-level SQL challenge focused on Session Analysis. Working through it gives you hands-on practice with TIMESTAMPDIFF, GROUP BY, MAX, datetime — 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

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. Work out the length of ONE session first: TIMESTAMPDIFF(MINUTE, start_time, end_time).
  2. Do not subtract HOUR() or MINUTE() parts -- that breaks for sessions that cross midnight.
  3. You want the largest length per user, so aggregate with MAX and GROUP BY user_id.
  4. Return exactly the two columns user_id and longest_session_minutes, ordered by user_id.

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

Helpful resources

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 easy and covers Session Analysis.

Solve this challenge free on PySpark.in