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

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 TIMESTAMP, 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 17 rows
user_idsession_secondsviewing_seconds
1002184040.908
197486541.456
294498042.004
488266643.1
585532524.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

Join first, then sum: inflated on both sides
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_id

Each 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

Total each side per user, then join
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_id

Common follow-up questions