Patients With a Condition - Problem

You're working as a data analyst for a hospital's electronic health records system. The hospital maintains a Patients table that stores patient information including their medical conditions.

Your task is to identify all patients who have been diagnosed with Type I Diabetes. In the hospital's coding system, Type I Diabetes conditions always start with the prefix DIAB1.

The conditions column contains medical condition codes separated by spaces. For example, a patient might have conditions like "SADIAB100 MYOP" or "DIAB1 MYOP SLEEP".

Goal: Write a SQL query to find the patient_id, patient_name, and conditions of patients with Type I Diabetes.

Key Challenge: The DIAB1 prefix must be at the beginning of a condition code, not embedded within another code. For instance, "SADIAB100" should NOT match, but "DIAB1" or "DIAB100" should match.

Input & Output

example_1.sql โ€” Basic Type I Diabetes Cases
$ Input: Patients: +------------+--------------+--------------+ | patient_id | patient_name | conditions | +------------+--------------+--------------+ | 1 | Daniel | DIAB1 MYOP | | 2 | Alice | HYPER LUPUS | | 3 | Bob | DIAB100 MYOP | +------------+--------------+--------------+
โ€บ Output: +------------+--------------+--------------+ | patient_id | patient_name | conditions | +------------+--------------+--------------+ | 1 | Daniel | DIAB1 MYOP | | 3 | Bob | DIAB100 MYOP | +------------+--------------+--------------+
๐Ÿ’ก Note: Daniel has 'DIAB1' at the start of conditions, and Bob has 'DIAB100' which also starts with 'DIAB1'. Alice has no DIAB1-related conditions.
example_2.sql โ€” Edge Case with Embedded DIAB1
$ Input: Patients: +------------+--------------+------------------+ | patient_id | patient_name | conditions | +------------+--------------+------------------+ | 4 | Charlie | SADIAB100 HYPER | | 5 | Eve | MYOP DIAB1 | | 6 | Frank | DIAB1 | +------------+--------------+------------------+
โ€บ Output: +------------+--------------+--------------+ | patient_id | patient_name | conditions | +------------+--------------+--------------+ | 5 | Eve | MYOP DIAB1 | | 6 | Frank | DIAB1 | +------------+--------------+--------------+
๐Ÿ’ก Note: Charlie's 'SADIAB100' contains 'DIAB1' but not at the start of a condition code, so it's excluded. Eve has 'DIAB1' after a space (valid), and Frank has 'DIAB1' as the only condition (valid).
example_3.sql โ€” Multiple DIAB1 Conditions
$ Input: Patients: +------------+--------------+------------------------+ | patient_id | patient_name | conditions | +------------+--------------+------------------------+ | 7 | Grace | DIAB1 HYPER DIAB199 | | 8 | Henry | LUPUS MYOP SLEEP | | 9 | Ivy | DIAB199 HYPER DIAB1 | +------------+--------------+------------------------+
โ€บ Output: +------------+--------------+------------------------+ | patient_id | patient_name | conditions | +------------+--------------+------------------------+ | 7 | Grace | DIAB1 HYPER DIAB199 | | 9 | Ivy | DIAB199 HYPER DIAB1 | +------------+--------------+------------------------+
๐Ÿ’ก Note: Grace has both 'DIAB1' and 'DIAB199' (both valid), Ivy has 'DIAB199' at start and 'DIAB1' after space (both valid). Henry has no DIAB1-related conditions.

Constraints

  • 1 โ‰ค patient_id โ‰ค 105
  • patient_name contains only alphabetic characters and spaces
  • conditions contains only uppercase letters, digits, and spaces
  • Each condition code is separated by exactly one space
  • Type I Diabetes conditions always start with DIAB1 prefix

Visualization

Tap to expand
Hospital Patient Database - Type I Diabetes SearchSQL Query Execution FlowWHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%'Patient 1DIAB1 MYOPโœ“ Match: DIAB1%Diabetes PatientPatient 2SADIAB100 HYPโœ— No MatchExcludedPatient 3MYOP DIAB1โœ“ Match: % DIAB1%Diabetes PatientPatient 4HYPER LUPUSโ—‹ No MatchRegular PatientResult SetPatients 1 and 3 identified with Type I Diabetes2 records returned out of 4 total patients๐ŸŽฏ Key: Word boundary matching prevents false positives like 'SADIAB100'
Understanding the Visualization
1
Database Scan
SQL engine scans through all patient records
2
Pattern Check
For each record, check if conditions match DIAB1 patterns
3
Word Boundary
Ensure DIAB1 appears at start or after space
4
Result Set
Return all matching patients
Key Takeaway
๐ŸŽฏ Key Insight: Word boundary pattern matching ensures medical condition codes are matched precisely, preventing dangerous false positives in healthcare data analysis.
Asked in
Amazon 45 Microsoft 32 Google 28 Meta 18
28.4K Views
Medium Frequency
~12 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen