Your question is SQL for Patient Data Extraction. 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.
Grady Health System needs a patient-level summary of active patients who had multiple completed visits during calendar year 2025. Write a PostgreSQL query using the provided tables.
Active and whose visits were completed between January 1 and December 31, 2025.Use a CTE with ROW_NUMBER() to identify each patient's latest qualifying visit. A patient whose latest visit has no department should still appear with a NULL department.
| Column | Type | Description |
|---|---|---|
| patient_idPK | INT | Unique patient identifier |
| mrn | VARCHAR(20) | Medical record number |
| first_name | VARCHAR(50) | Patient first name |
| last_name | VARCHAR(50) | Patient last name |
| date_of_birth | DATE | Patient date of birth |
| sex | VARCHAR(20) | Recorded sex |
| status | VARCHAR(20) | Patient status |
| Column | Type | Description |
|---|---|---|
| visit_idPK | INT | Unique visit identifier |
| patient_id | INT | Referenced patient |
| department_id | INT | Referenced department |
| visit_date | DATE | Date of service |
| visit_type | VARCHAR(30) | Visit type |
| visit_status | VARCHAR(20) | Visit completion status |
| charge | NUMERIC(10,2) | Billed charge |
| Column | Type | Description |
|---|---|---|
| department_idPK | INT | Unique department identifier |
| department_name | VARCHAR(100) | Grady Health System department name |