[SQL 문제 풀이] Find COVID Recovery Patients (COVID 회복 환자 찾기)

Stupefyee's avatar
Jun 17, 2025
[SQL 문제 풀이] Find COVID Recovery Patients (COVID 회복 환자 찾기)
Find COVID Recovery Patients - LeetCode
Can you solve this real interview question? Find COVID Recovery Patients - Table: patients +-------------+---------+ | Column Name | Type | +-------------+---------+ | patient_id | int | | patient_name| varchar | | age | int | +-------------+---------+ patient_id is the unique identifier for this table. Each row contains information about a patient. Table: covid_tests +-------------+---------+ | Column Name | Type | +-------------+---------+ | test_id | int | | patient_id | int | | test_date | date | | result | varchar | +-------------+---------+ test_id is the unique identifier for this table. Each row represents a COVID test result. The result can be Positive, Negative, or Inconclusive. Write a solution to find patients who have recovered from COVID - patients who tested positive but later tested negative. * A patient is considered recovered if they have at least one Positive test followed by at least one Negative test on a later date * Calculate the recovery time in days as the difference between the first positive test and the first negative test after that positive test * Only include patients who have both positive and negative test results Return the result table ordered by recovery_time in ascending order, then by patient_name in ascending order. The result format is in the following example.   Example: Input: patients table: +------------+--------------+-----+ | patient_id | patient_name | age | +------------+--------------+-----+ | 1 | Alice Smith | 28 | | 2 | Bob Johnson | 35 | | 3 | Carol Davis | 42 | | 4 | David Wilson | 31 | | 5 | Emma Brown | 29 | +------------+--------------+-----+ covid_tests table: +---------+------------+------------+--------------+ | test_id | patient_id | test_date | result | +---------+------------+------------+--------------+ | 1 | 1 | 2023-01-15 | Positive | | 2 | 1 | 2023-01-25 | Negative | | 3 | 2 | 2023-02-01 | Positive | | 4 | 2 | 2023-02-05 | Inconclusive | | 5 | 2 | 2023-02-12 | Negative | | 6 | 3 | 2023-01-20 | Negative | | 7 | 3 | 2023-02-10 | Positive | | 8 | 3 | 2023-02-20 | Negative | | 9 | 4 | 2023-01-10 | Positive | | 10 | 4 | 2023-01-18 | Positive | | 11 | 5 | 2023-02-15 | Negative | | 12 | 5 | 2023-02-20 | Negative | +---------+------------+------------+--------------+ Output: +------------+--------------+-----+---------------+ | patient_id | patient_name | age | recovery_time | +------------+--------------+-----+---------------+ | 1 | Alice Smith | 28 | 10 | | 3 | Carol Davis | 42 | 10 | | 2 | Bob Johnson | 35 | 11 | +------------+--------------+-----+---------------+ Explanation: * Alice Smith (patient_id = 1): * First positive test: 2023-01-15 * First negative test after positive: 2023-01-25 * Recovery time: 25 - 15 = 10 days * Bob Johnson (patient_id = 2): * First positive test: 2023-02-01 * Inconclusive test on 2023-02-05 (ignored for recovery calculation) * First negative test after positive: 2023-02-12 * Recovery time: 12 - 1 = 11 days * Carol Davis (patient_id = 3): * Had negative test on 2023-01-20 (before positive test) * First positive test: 2023-02-10 * First negative test after positive: 2023-02-20 * Recovery time: 20 - 10 = 10 days * Patients not included: * David Wilson (patient_id = 4): Only has positive tests, no negative test after positive * Emma Brown (patient_id = 5): Only has negative tests, never tested positive Output table is ordered by recovery_time in ascending order, and then by patient_name in ascending order.
Find COVID Recovery Patients - LeetCode
notion image
코로나19에서 회복된 환자, 양성 판정을 받았지만 나중에 음성 판정을 받은 환자를 찾기 위한 솔루션을 작성하세요. * 환자가 한 번 이상의 양성 판정을 받은 후 나중에 한 번 이상의 음성 판정을 받은 경우 회복된 것으로 간주됩니다 * 첫 번째 양성 테스트와 양성 테스트 후 첫 번째 음성 테스트의 차이로 회복 시간(일)을 계산합니다 * 양성 및 음성 검사 결과를 모두 받은 환자만 포함하십시오 결과 테이블을 회복_시간 순서대로 오름차순으로, 환자_이름 순서대로 반환합니다.
 

내가 작성한 쿼리

Oracle

SELECT p.patient_id, p.patient_name, p.age, -- 최초 음성 날짜 - 최초 양성 날짜 → 회복 기간 계산 MIN(ct2.test_date) - MIN(ct1.test_date) AS recovery_time FROM patients p JOIN covid_tests ct1 ON p.patient_id = ct1.patient_id -- 양성 검사와 환자 테이블을 환자 ID 기준으로 조인 AND ct1.result = 'Positive' -- 양성 검사만 필터링 JOIN covid_tests ct2 ON p.patient_id = ct2.patient_id -- 음성 검사와 환자 테이블을 환자 ID 기준으로 조인 AND ct2.result = 'Negative' -- 음성 검사만 필터링 AND ct2.test_date > ct1.test_date -- 양성 검사보다 이후에 시행된 음성 검사만 포함 GROUP BY p.patient_id, p.patient_name, p.age ORDER BY recovery_time ASC, p.patient_name ASC;

MySQL

SELECT p.patient_id, p.patient_name, p.age, -- 최초 음성 날짜 - 최초 양성 날짜 → 회복 기간 계산 DATEDIFF(MIN(ct2.test_date), MIN(ct1.test_date)) AS recovery_time FROM patients p JOIN covid_tests ct1 ON p.patient_id = ct1.patient_id -- 양성 검사와 환자 테이블을 환자 ID 기준으로 조인 AND ct1.result = 'Positive' -- 양성 검사만 필터링 JOIN covid_tests ct2 ON p.patient_id = ct2.patient_id -- 음성 검사와 환자 테이블을 환자 ID 기준으로 조인 AND ct2.result = 'Negative' -- 음성 검사만 필터링 AND ct2.test_date > ct1.test_date -- 양성 검사보다 이후에 시행된 음성 검사만 포함 GROUP BY p.patient_id, p.patient_name ORDER BY recovery_time ASC, p.patient_name ASC;

차이점

  • 날짜 계산 방식의 차이
  • 그룹화 조건 차
Share article

stupefyee