Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Aggregates in One Row

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

Your question is SQL Aggregates in One Row. 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

Exusia Care needs a compact patient activity summary for its reporting dashboard. Write a PostgreSQL query that returns the number of distinct male and female patients who had at least one completed encounter during 2025 in the same row.

Requirements

  1. Join patients to encounters using patient_id.
  2. Include only encounters with status completed and an encounter date from 2025-01-01 through 2025-12-31.
  3. Count each qualifying patient once, even if the patient had multiple completed encounters.
  4. Return one row with the columns male_patient_count and female_patient_count. Do not include other genders in either count.

Schema

patients
ColumnTypeDescription
patient_idPKINTEGERUnique patient identifier
patient_nameVARCHAR(100)Patient's name
genderVARCHAR(20)Recorded gender
encounters
ColumnTypeDescription
encounter_idPKINTEGERUnique encounter identifier
patient_idINTEGERPatient associated with the encounter
encounter_dateDATEDate of the encounter
statusVARCHAR(20)Encounter status
Tablespatientsencounters
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results