Zomato: Sessionize App Events and Flag Sessions That Led to an Order
SQL coding challenge · Difficulty: medium · Topic: Sessionization · +100 XP
Problem
*Zomato-style interview practice.*
The Zomato app logs every tap in app_events, but it never records sessions. Product analysts need them: how long people browse, how many taps a visit takes, and which visits end in an order. Build the sessions from the event times.
- Work per user, in time order: by
event_time, then byevent_idfor events in the same second. - A user's first event starts session 1. A new session starts whenever the gap since that user's previous event is more than 30 minutes. A gap of exactly 30 minutes stays in the same session; 30 minutes and 1 second starts a new one.
- Number each user's sessions 1, 2, 3, ... in time order (
session_no). - For every session return
session_start(time of its first event),session_end(time of its last event),event_count(every event, including one whoseevent_typeis NULL) andordered: 1 if the session contains at least oneplace_orderevent, otherwise 0. - Another user's events never split or join a session. Sessions can cross midnight.
Return user_id, session_no, session_start, session_end, event_count and ordered (the grader checks the rows, not their order).
Schema
Table: app_events
| Column | Type | Meaning | | --- | --- | --- | | event_id | INT | Unique id of the event | | user_id | VARCHAR | The user who tapped | | event_time | DATETIME | When it happened (to the second) | | event_type | VARCHAR | `open`, `search`, `view_menu`, `add_to_cart`, `place_order`, or NULL when the app did not send one |
Tables
Table: app_events
| event_id | user_id | event_time | event_type | | --- | --- | --- | --- | | 1 | U1 | 2025-03-01 09:00:00 | open | | 8 | U2 | 2025-03-01 09:10:00 | open | | 2 | U1 | 2025-03-01 09:05:00 | search | | 3 | U1 | 2025-03-01 09:20:00 | view_menu | | 9 | U2 | 2025-03-01 09:30:00 | view_menu | | 4 | U1 | 2025-03-01 09:50:00 | add_to_cart | | 5 | U1 | 2025-03-01 09:52:00 | place_order | | 6 | U1 | 2025-03-01 10:22:01 | open | | 7 | U1 | 2025-03-01 10:25:00 | search | | 10 | U2 | 2025-03-01 11:00:00 | open | | 11 | U2 | 2025-03-01 11:10:00 | place_order | | 12 | U2 | 2025-03-01 11:15:00 | place_order | | 13 | U3 | 2025-03-01 23:50:00 | open | | 15 | U3 | 2025-03-02 00:15:00 | NULL | | 14 | U3 | 2025-03-02 00:10:00 | view_menu |
Expected Output
| user_id | session_no | session_start | session_end | event_count | ordered | | --- | --- | --- | --- | --- | --- | | U1 | 1 | 2025-03-01 09:00:00 | 2025-03-01 09:52:00 | 5 | 1 | | U1 | 2 | 2025-03-01 10:22:01 | 2025-03-01 10:25:00 | 2 | 0 | | U2 | 1 | 2025-03-01 09:10:00 | 2025-03-01 09:30:00 | 2 | 0 | | U2 | 2 | 2025-03-01 11:00:00 | 2025-03-01 11:15:00 | 3 | 1 | | U3 | 1 | 2025-03-01 23:50:00 | 2025-03-02 00:15:00 | 3 | 0 |
Explanation
- U1: 09:20 → 09:50 is exactly 30 minutes, so the add-to-cart stays in session 1, which ends with the order at 09:52. The next event, 10:22:01, is 30 minutes and 1 second later: session 2. (Measuring the gap in whole minutes would round it down to 30 and miss this split.)
- U2: its events sit between U1's but are a separate timeline. 09:30 → 11:00 is 90 minutes, so 11:00 starts session 2, which places two orders:
orderedis still 1. - U3: 23:50 → 00:10 → 00:15 is one session across midnight. The 00:15 event has no type, but it is still an event: 3 events.
Notes
- Column names must be exactly
user_id,session_no,session_start,session_end,event_count,ordered. - The query runs on MySQL 8, which supports window functions.
What this SQL challenge teaches you
“Zomato: Sessionize App Events and Flag Sessions That Led to an Order” is a medium-level SQL challenge focused on Sessionization. Working through it gives you hands-on practice with Sessionization, LAG, Running SUM, Gaps and Islands, TIMESTAMPDIFF, Conditional aggregation — 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
- Sessionization
- LAG
- Running SUM
- Gaps and Islands
- TIMESTAMPDIFF
- Conditional aggregation
- Food Delivery
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:
- LAG(event_time) OVER (PARTITION BY user_id ORDER BY event_time, event_id) gives each event the same user's previous event time.
- Flag a new session when there is no previous event or TIMESTAMPDIFF(SECOND, prev_time, event_time) > 1800. Minutes would round 30:01 down to 30.
- A running SUM of the flags (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) numbers the sessions 1, 2, 3 per user.
- GROUP BY user_id, session_no; use COUNT(*) so NULL event types count, and MAX(CASE ...) so two orders still give 1.
Where this comes up
Variations of this problem have been reported in interviews at Zomato. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.
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
- Zomato: Sessionize App Events and Flag Sessions That Led to an Order (PySpark)
- Top 3 Products per Category
- Running Total Revenue
- Find Duplicate Emails
- Median Salary per Department
- Second Highest Salary
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 medium and covers Sessionization.