๐Ÿ“š 100 DAYS SQL

CODING CHALLENGE

DAY 14

Master SQL One Problem at a Time

โœจ Daily SQL Problems
โœจ Real-World Scenarios
โœจ Complete Solutions
โœจ Interview Ready

๐Ÿฅ Hospital Patient Management System

Database Schema - RAW DATA (Messy!)

Table 1: patient_records - Sample data (25 rows total)

patient_idfull_namedate_of_birthadmission_datedischarge_datediagnosis
  P001  john smith15/05/19852024-01-10 08:30:002024-01-15Hypertension
p002SARAH JONES1990-08-2210/01/2024NULL  Diabetes  
P003  Mike Wilson  1978-03-102024/01/12 14:452024-01-20flu
P004anna garcia22-07-19952024-01-15NULLPNEUMONIA

...and 21 more messy rows! Download the full dataset above.

Table 2: medications - Drug reference data

med_idmedication_namecategoryrisk_level
MED001AspirinPain ReliefLOW
MED004WarfarinBlood ThinnerHIGH
MED005MorphinePain ReliefHIGH

Table 3: prescriptions - Messy prescription data

prescription_idpatient_iddoctor_idmed_iddosagestatus
RX001P001  D101  MED00310mgACTIVE
RX002p002d102med002500 mgactive
RX003P003D101MED001NULLCompleted
RX004P004D103med999250mgACTIVE

Table 4: doctors - Doctor information

doctor_iddoctor_namespecializationlicense_status
D101dr. emily chenCardiologyACTIVE
D104james wilsonOrthopedicsSUSPENDED

๐Ÿ“‹ Problem Requirements

Build a hospital management system to track patient medications, validate prescriptions, detect risks, and generate compliance reports.

๐Ÿงน Data Cleaning Tasks:
  • Standardize all IDs: uppercase, trim spaces (patient_id, doctor_id, med_id)
  • Clean names: proper case (Title Case), trim spaces
  • Standardize dates: convert all to YYYY-MM-DD format
  • Clean diagnosis: proper case, trim spaces
  • Standardize dosage: consistent format with units
  • Standardize status: lowercase
  • Handle NULL discharge dates (patient still admitted)
๐Ÿ”— JOIN & Enrichment:
  • JOIN prescriptions with patient_records to get patient details
  • JOIN prescriptions with medications to get drug info & pricing
  • JOIN prescriptions with doctors to validate prescriber credentials
  • LEFT JOIN to detect orphan prescriptions (invalid patient/med/doctor)
  • Calculate patient age from date_of_birth using TIMESTAMPDIFF
  • Calculate length of hospital stay (admission to discharge)
โš ๏ธ Risk Detection & Validation:
  • Flag HIGH risk medications (Warfarin, Morphine)
  • Flag prescriptions with missing dosage information
  • Flag prescriptions from SUSPENDED doctors (credential validation)
  • Flag orphan prescriptions (non-existent med_id in catalog)
  • Calculate prescription risk score: 0-100 (sum of all risk factors)
  • Categorize prescriptions: Low Risk (0-30), Medium (31-60), High (61-100)

๐Ÿ’ก SQL Skills Required: TRIM, UPPER, INITCAP, STR_TO_DATE, CASE, CAST, JOINs, TIMESTAMPDIFF, DATEDIFF, COALESCE, CTEs!

This mirrors real-world hospital systems used to track patient medications and ensure prescription safety!

๐ŸŽ‰ Day 14 Complete!

Congratulations!

You've learned hospital data management using complex JOINs, date calculations, and multi-factor risk scoring! This is exactly what healthcare data engineers build - prescription tracking systems that ensure patient safety and regulatory compliance.

๐Ÿ”— Connect & Follow

Join me on the 100 Days SQL journey!

LinkedIn: linkedin.com/in/rahulkanche
GitHub: github.com/rahulkanche/100-days-sql-data

๐Ÿ’ช Keep Practicing!

โœ“ Master healthcare data pipelines
โœ“ Build prescription tracking systems
โœ“ Validate medical records with SQL
โœ“ Prepare for Healthcare/FinTech interviews

Follow the 100 Days SQL Challenge
Day 15 tomorrow - Master SQL one problem at a time!

Rahul Kanche โ€” exploring data, decoding clarity!