1527. Patients With a Condition
SQL 50 · Advanced String Functions, Regex, Clause (day 7). Decode it with the hints, solve it on LeetCode, then compare with the reference solution in MySQLPostgreSQL.
Your work
Hints
Open one at a time. Try to finish with as few as you can.
1. What does one output row represent?
One patient.
2. Approach
conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%'.
3. The trap
'%DIAB1%' would wrongly match ‘ADIAB100’. The code must start a word.
Reference solution
Tested on MySQL 8.0 and PostgreSQL 16. Switch dialect at the top of the page.
Show the MySQLPostgreSQL solution
SELECT patient_id, patient_name, conditions
FROM Patients
WHERE conditions LIKE 'DIAB1%'
OR conditions LIKE '% DIAB1%';Note: LIKE is case-sensitive in PostgreSQL (ILIKE is not). MySQL’s default collation is case-insensitive. The codes here are uppercase, so both work.