Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
Window Functions Data Retrieval
00:00
5 left

Window Functions Data Retrieval

MediumSQL · PostgreSQL

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
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.