Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Window Functions Data Retrieval

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

Your question is Window Functions Data Retrieval. 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

Ancestry wants to understand which paid members had the most productive sessions in Ancestry Record Search during January 2025. Write a PostgreSQL query using a window function to return each paid member's best qualifying session, including paid members with no qualifying session.

Requirements

  1. Consider only members with a Plus or Pro subscription.
  2. Include only completed sessions on the Record Search surface from 2025-01-01 inclusive through 2025-02-01 exclusive.
  3. Rank each member's sessions by result_count descending, then by started_at descending to break ties, and return rank 1 for each member.
  4. Preserve paid members without a qualifying session, showing NULL session fields. Order the final results by member_id.

Schema

members
ColumnTypeDescription
member_idPKINTEGERUnique Ancestry member identifier
display_nameVARCHAR(100)Member display name
subscription_tierVARCHAR(20)Current membership tier
country_codeVARCHAR(2)Two-letter member country code
search_sessions
ColumnTypeDescription
session_idPKINTEGERUnique search session identifier
member_idINTEGERMember associated with the session
started_atTIMESTAMPTimestamp when the session began
search_surfaceVARCHAR(40)Ancestry product surface used
result_countINTEGERNumber of results returned by the session
statusVARCHAR(20)Session completion state
Tablesmemberssearch_sessions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results