High Engagement Pages

A hard SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
hard
Seniority
L3
Asked in
Meta

Problem

We run a web analytics product where every page view logs how long it stayed on screen, in milliseconds, and which user viewed it. For each session a user starts, add up the on-screen time in seconds across all of that user's page views.

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 TEXT

page_views: 10 of 200 rows
view_idpage_urluser_idreferrerdur_msdeviceviewed_at
5053/products1555amazon.com637desktop2026-09-02 01:09:00
5106/checkout1652twitter.com774tablet2026-09-03 02:18:00
5159/profile1749github.com911Mobile2026-09-04 03:27:00
5212/search1846NULL1048DESKTOP2026-09-05 04:36:00
5265/about1943reddit.com1185mobile2026-09-06 05:45:00
5318/blog/intro1555NULLNULLdesktop2026-09-07 06:54:00
5371/pricing1652bing.com1459tablet2026-09-08 07:03:00
5424/docs/api1749news.ycombinator.com1596Mobile2026-09-09 08:12:00
5477/settings1846NULL1733DESKTOP2026-09-10 09:21:00
5530/Home1943bing.com1870mobile2026-09-11 10:30:00

user_sessions

Columns: session_id INTEGER, user_id INTEGER, device_id INTEGER, session_start TEXT, session_duration_sec INTEGER, pages_viewed INTEGER

user_sessions: 10 of 200 rows
session_iduser_iddevice_idsession_startsession_duration_secpages_viewed
70611972302026-02-02 01:00:00533
71222942592026-03-03 02:00:00766
71833912882026-04-04 03:00:00999
72444883172026-05-05 04:00:0012212
73055853462026-06-06 05:00:0014515
73666823752026-07-07 06:00:0016818
74277794042026-08-08 07:00:0019121
7488876NULL2026-09-09 08:00:0021424
75499734622026-01-10 09:00:0023727
761010704912026-02-11 10:00:0026030

Expected output

First 5 of 88 rows
session_idtotal_seconds
706141.456
712242.004
724443.1
730524.564
736644.196

Worked solution and explanation

What this problem is really asking

Beneath the analytics framing this is a cross-table sum: the value you group by, session_id, lives in user_sessions, while the thing you add up, dur_ms, lives in page_views, and the only column the two tables share is user_id. So the real question is how a page view maps to a session, and the honest answer is that it does not map to one directly. A page view is stamped with a user, not a session, so a session's viewing time is the total on-screen time of the user who owns that session, and every session that user started carries the same total.


The one thing to notice

There is no session_id on page_views. A candidate who goes hunting for one, or who tries to line a view up with a single session, stalls. The link is user_id: join user_sessions to page_views on it and every session a user started collects that user's whole set of views. That repetition is the intended grain here, not a bug to design around.


Building the query

Step 1: Join the two tables on the shared user

INNER JOIN page_views pv ON pv.user_id = us.user_id attaches every one of a user's views to each of that user's sessions. Because it is an inner join, a session whose user logged no views quietly drops out, which is exactly the membership you want: a session only counts once there is on-screen time behind it.

Step 2: Total the on-screen time per session in seconds

Group by us.session_id and compute SUM(pv.dur_ms) / 1000.0. Summing the raw millisecond durations and dividing once at the end gives seconds; the / 1000.0 keeps the result a real number instead of integer-truncating. The roughly 1% of dur_ms that are NULL are ignored by SUM, which is the behavior you want.


The solution

Join on the shared user, then total per session
SELECT us.session_id, SUM(pv.dur_ms) / 1000.0 AS total_seconds
FROM user_sessions us
INNER JOIN page_views pv ON pv.user_id = us.user_id
GROUP BY us.session_id

Common follow-up questions