데이터베이스/SQL

1527. Patients With a Condition

칼퇴시켜주세요 2022. 12. 7. 15:55
728x90

Patients 테이블에서 conditions 가 'DIAB1'로 시작하는 모든 레코드를 반환하도록 SQL을 작성하는 문제이다.

문제를 보는 순간 LIKE 연산자를 사용하면 쉽게 해결할 수 있을것 같다고 생각했다.

#wrong answer -> 틀린이유는 SDIAB100도 레코드로 반환이 된다.
select *
from Patients
where conditions like '%DIAB1%'

#correct answer -> DIAB1로 시작하거나 (space)DIAB1 인 결과만 반환하도록 수정한다.
select *
from Patients
where conditions like 'DIAB1%' or conditions like '% DIAB1%'

#other solution
SELECT * FROM Patients
WHERE conditions REGEXP '^DIAB1| DIAB1';
반응형

'데이터베이스 > SQL' 카테고리의 다른 글

178. Rank Scores  (0) 2023.01.05
181. Employees Earning More Than Their Managers  (0) 2023.01.05
1484. Group Sold Products By The Date  (1) 2022.12.06
1667. Fix Names in a Table  (0) 2022.12.05
196. Delete Duplicate Emails  (0) 2022.12.05