Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rank Records with Window Functions

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

Your question is Rank Records with Window Functions. Start with the requirements and the three 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

Bristol Myers Squibb clinical operations teams need to compare site enrollment performance across active studies for products such as Opdivo and Eliquis. Write a PostgreSQL query that ranks each participating site within its study.

Requirements

  1. Join the study, site, and enrollment data.
  2. Include only active studies where the site enrollment count is known.
  3. Use RANK() to rank sites separately within each study, with the highest enrollment receiving rank 1. Preserve ties.
  4. Return the results ordered by study and rank, with site ID as a deterministic tie-breaker.

Schema

clinical_trials
ColumnTypeDescription
trial_idPKINTUnique clinical trial identifier
product_nameVARCHAR(100)Bristol Myers Squibb product under study
therapeutic_areaVARCHAR(80)Therapeutic area for the study
statusVARCHAR(20)Current study status
trial_sites
ColumnTypeDescription
site_idPKINTUnique clinical trial site identifier
site_nameVARCHAR(120)Site or institution name
countryVARCHAR(60)Country where the site operates
site_enrollment
ColumnTypeDescription
enrollment_idPKINTUnique enrollment record identifier
trial_idINTReferenced clinical trial
site_idINTReferenced trial site
enrolled_patientsINTNumber of enrolled patients
Tablesclinical_trialstrial_sitessite_enrollment
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results