Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL for User Navigation Path

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL for User Navigation Path. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Discover's analytics team wants to review how a user navigated the Discover.com account experience. Write a PostgreSQL query that reconstructs the selected user's interaction sequence from raw page events.

Requirements

  1. Join the interaction records to the user's Discover profile and filter for user_id = 101.
  2. Order events chronologically, using interaction_id as a tie breaker, and assign each event a navigation step number.
  3. Show the previous action and the elapsed seconds since the previous interaction using a window function.
  4. Preserve events where action is NULL, since an event without a captured action is still part of the navigation path.

Schema

users
ColumnTypeDescription
user_idPKINTEGERDiscover user identifier
user_nameVARCHAR(100)User display name
account_statusVARCHAR(20)Current account status
page_interactions
ColumnTypeDescription
interaction_idPKINTEGERUnique interaction identifier
user_idINTEGERReferences users.user_id
event_timeTIMESTAMPTZTimestamp of the page interaction
page_nameVARCHAR(80)Discover.com page or surface
actionVARCHAR(80)Captured action, which may be NULL
Tablesuserspage_interactions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results