Patients With a Condition - Problem

You are given a table Patients containing information about hospital patients. Each patient has a unique patient_id, a patient_name, and a conditions column that contains medical condition codes separated by spaces.

Your task: Find all patients who have Type I Diabetes. Type I Diabetes codes always start with the prefix DIAB1.

Return the patient_id, patient_name, and conditions for these patients in any order.

Table Schema

Patients
Column Name Type Description
patient_id PK int Primary key, unique identifier for each patient
patient_name varchar Full name of the patient
conditions varchar Medical condition codes separated by spaces (can be empty)
Primary Key: patient_id
Note: The conditions column contains 0 or more condition codes separated by spaces

Input & Output

Example 1 — Mixed Conditions
Input Table:
patient_id patient_name conditions
1 Daniel YFEV COUGH
2 Alice
3 Bob DIAB100 MYOP
4 George ACNE DIAB100
5 Alain DIAB201
Output:
patient_id patient_name conditions
3 Bob DIAB100 MYOP
4 George ACNE DIAB100
💡 Note:

Patients Bob, George, and Alain have conditions starting with DIAB1. Bob has DIAB100 at the start, George has DIAB100 after a space, and Alain has DIAB201. Daniel and Alice don't have any Type I Diabetes conditions.

Example 2 — Edge Cases
Input Table:
patient_id patient_name conditions
1 John DIAB1
2 Jane XDIAB100
3 Mike
Output:
patient_id patient_name conditions
1 John DIAB1
💡 Note:

Only John matches because his condition starts with exactly DIAB1. Jane's XDIAB100 doesn't match because DIAB1 is not at the beginning of a word. Mike has no conditions.

Constraints

  • 1 ≤ patient_id ≤ 100000
  • patient_name consists of uppercase and lowercase letters and spaces
  • conditions consists of uppercase letters and spaces, separated by single spaces
  • Type I Diabetes conditions always start with DIAB1 prefix

Visualization

Tap to expand
Patients With a Condition Find patients with Type I Diabetes (DIAB1 prefix) INPUT id name conditions 1 Daniel DIAB1 (Type I) 2 Alice DIAB100 (Type I) 3 Bob ACNE (Not DIAB1) 4 George ACNE DIAB100 (Has DIAB1) 5 Alain MYDIAB1 (Not prefix) Patients Table 5 records to filter ALGORITHM STEPS 1 Use REGEXP Pattern Match 'DIAB1' as word start 2 Word Boundary Check Pattern: '(^| )DIAB1' (^| ) = Start or space DIAB1 = Exact prefix match 3 Filter Rows WHERE conditions matches 4 Return Results Select matching patients SELECT * FROM Patients WHERE conditions REGEXP '(^| )DIAB1' FINAL RESULT id name conditions 1 Daniel DIAB1 2 Alice DIAB100 4 George ACNE DIAB100 3 Patients Found Excluded: Bob (ACNE only) Alain (MYDIAB1) OK - Type I patients found Key Insight: The REGEXP pattern '(^| )DIAB1' ensures DIAB1 appears at the START of conditions or after a SPACE. This correctly excludes cases like 'MYDIAB1' where DIAB1 is a suffix, not a condition code prefix. TutorialsPoint - Patients With a Condition | Optimal Solution
Asked in
Amazon 15 Google 8 Microsoft 12
28.5K Views
Medium Frequency
~8 min Avg. Time
890 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