High Engagement Pages
A medium SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.
- Domain
- SQL
- Difficulty
- medium
- Seniority
- L3
- Asked in
- Meta
Problem
We run a web analytics product where every session records its length in seconds and every page view logs, in milliseconds, how long it stayed on screen and which user viewed it. For every user with both sessions and page views, put their total session time next to their total on-screen time, both in seconds.
Some pages hold attention longer than others.
Tables
page_views
Columns: view_id INTEGER, page_url TEXT, user_id INTEGER, referrer TEXT, dur_ms INTEGER, device TEXT, viewed_at TIMESTAMP
| view_id | page_url | user_id | referrer | dur_ms | device | viewed_at |
|---|---|---|---|---|---|---|
| 5053 | /products | 1555 | amazon.com | 637 | desktop | 2026-09-02 01:09:00 |
| 5106 | /checkout | 1652 | twitter.com | 774 | tablet | 2026-09-03 02:18:00 |
| 5159 | /profile | 1749 | github.com | 911 | Mobile | 2026-09-04 03:27:00 |
| 5212 | /search | 1846 | NULL | 1048 | DESKTOP | 2026-09-05 04:36:00 |
| 5265 | /about | 1943 | reddit.com | 1185 | mobile | 2026-09-06 05:45:00 |
| 5318 | /blog/intro | 1555 | NULL | NULL | desktop | 2026-09-07 06:54:00 |
| 5371 | /pricing | 1652 | bing.com | 1459 | tablet | 2026-09-08 07:03:00 |
| 5424 | /docs/api | 1749 | news.ycombinator.com | 1596 | Mobile | 2026-09-09 08:12:00 |
| 5477 | /settings | 1846 | NULL | 1733 | DESKTOP | 2026-09-10 09:21:00 |
| 5530 | /Home | 1943 | bing.com | 1870 | mobile | 2026-09-11 10:30:00 |
user_sessions
Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TIMESTAMP, session_duration_sec INTEGER, pages_viewed INTEGER
| session_id | user_id | device_id | session_start | session_duration_sec | pages_viewed |
|---|---|---|---|---|---|
| 7061 | 197 | 230 | 2026-02-02 01:00:00 | 53 | 3 |
| 7122 | 294 | 259 | 2026-03-03 02:00:00 | 76 | 6 |
| 7183 | 391 | 288 | 2026-04-04 03:00:00 | 99 | 9 |
| 7244 | 488 | 317 | 2026-05-05 04:00:00 | 122 | 12 |
| 7305 | 585 | 346 | 2026-06-06 05:00:00 | 145 | 15 |
| 7366 | 682 | 375 | 2026-07-07 06:00:00 | 168 | 18 |
| 7427 | 779 | 404 | 2026-08-08 07:00:00 | 191 | 21 |
| 7488 | 876 | NULL | 2026-09-09 08:00:00 | 214 | 24 |
| 7549 | 973 | 462 | 2026-01-10 09:00:00 | 237 | 27 |
| 7610 | 1070 | 491 | 2026-02-11 10:00:00 | 260 | 30 |
Expected output
| user_id | session_seconds | viewing_seconds |
|---|---|---|
| 100 | 21840 | 40.908 |
| 197 | 4865 | 41.456 |
| 294 | 4980 | 42.004 |
| 488 | 2666 | 43.1 |
| 585 | 5325 | 24.564 |
Worked solution and explanation
What this problem is really asking
This is a fan-out problem dressed up as an engagement report. Session length sits one row per session in user_sessions; on-screen time sits one row per view in page_views; the only shared key is user_id. Join the raw tables and every session pairs with every one of the user's views, so 5 sessions and 10 views become 50 rows. Sum after that and the session total is 10 times too big, the viewing total 5 times too big. Anyone can write the join. The skill is totaling each measure on the side that holds it before the two meet.
The trap
SELECT us.user_id,
SUM(us.session_duration_sec) AS session_seconds,
SUM(pv.dur_ms) / 1000.0 AS viewing_seconds
FROM user_sessions us
INNER JOIN page_views pv ON pv.user_id = us.user_id
GROUP BY us.user_idEach session row is repeated once per view and each view once per session before SUM runs.
Building the query
Step 1: Total session time per user
Group user_sessions by user_id and take SUM(session_duration_sec) as session_seconds. One row per user, each session counted once. SUM skips the 17 sessions with no recorded length.
Step 2: Total on-screen time per user in seconds
Group page_views by user_id and take SUM(dur_ms) / 1000.0 as viewing_seconds, dividing once at the end. The 26 views with a NULL user_id form their own NULL group, which the join in step 3 can never match.
Step 3: Join the two one-row-per-user results
Both sides are now one row per user_id, so the INNER JOIN is one-to-one and nothing multiplies. Being an inner join, it keeps only users who have both sessions and page views, which is the population the comparison asks for.
The solution
SELECT session_totals.user_id,
session_totals.session_seconds,
viewing_totals.viewing_seconds
FROM (
SELECT user_id, SUM(session_duration_sec) AS session_seconds
FROM user_sessions
GROUP BY user_id
) AS session_totals
INNER JOIN (
SELECT user_id, SUM(dur_ms) / 1000.0 AS viewing_seconds
FROM page_views
GROUP BY user_id
) AS viewing_totals
ON viewing_totals.user_id = session_totals.user_idCommon follow-up questions
- How would you add each user's session count without breaking the totals? (Tests whether the candidate keeps the count on the session side rather than counting joined rows.)
- What changes if you switch to a `FULL OUTER JOIN` so users missing either side still appear? (Tests outer join semantics and how missing totals should read.)
- If one user owns millions of views, where does the work skew and how would you handle it? (Tests awareness of skew on `user_id` in the aggregation and join.)