Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Top Row Retrieval

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

Your question is SQL Top Row Retrieval. Start with the requirements and the one table 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

Write SQL to get the top row of a column.

Assume the top row is the complete row with the highest non-NULL visit_count. If multiple rows share the highest value, return the row with the smallest encounter_id.

Output

  1. Return at most one row with all columns from clinical_encounters.
  2. Exclude rows where visit_count is NULL.
  3. Order the result by highest visit_count, then lowest encounter_id.

Schema

clinical_encounters
ColumnTypeDescription
encounter_idPKINTUnique identifier for the encounter
patient_idINTIdentifier of the patient
provider_nameVARCHAR(100)Name of the assigned provider
encounter_typeVARCHAR(50)Type of clinical encounter
visit_countINTNumber of visits represented by the encounter record
Tablesclinical_encounters
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results