| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
Tags
- mysql
- 코딩테스트
- python
- LeetCode
- Level2
- lv4
- coding test
- level4
- 프로그래머스
- time complexity
- sql
- import re
- tableau
- join
- 조합
- 파이썬
- AARRR
- 코테
- 완전탐색
- programmers
- itertools
- 코딩
- Stack
- cte
- collections
- coding
- Growth hacking
- Queue
- counter
- lambda
Archives
- Today
- Total
ror_coding
[Programmers Lv.4] 특정 세대의 대장균 찾기 - 301650 본문
728x90
세대 찾기(LEVEL)을 간단하게 JOIN을 이용하여 풀기 !
Question
3세대의 대장균의 ID(ID) 를 출력하는 SQL 문을 작성해주세요. 이때 결과는 대장균의 ID 에 대해 오름차순 정렬해주세요.
Point
- Code 1 : JOIN의 코드 이해를 위한 시각화 자료.
- 2세대 -> JOIN 1번, 3세대 -> JOIN 2번.

Code (After 2509)
with recursive cte as(
select parent_id, id, 1 as lv
from ECOLI_DATA
where parent_id is null
union all
select c.parent_id, c.id, lv + 1
from cte p
join ECOLI_DATA c on p.id = c.parent_id
where lv < 3
)
select id
from cte
where lv = 3
order by 1
;
Code 1 : JOIN
SELECT A.ID
FROM ECOLI_DATA AS A
JOIN ECOLI_DATA AS B ON A.PARENT_ID = B.ID
JOIN ECOLI_DATA AS C ON B.PARENT_ID = C.ID
WHERE C.PARENT_ID IS NULL
ORDER BY 1;
Code 2 : My code (CTE)
WITH RECURSIVE GENERATION AS(
SELECT ID, PARENT_ID, 1 AS LV
FROM ECOLI_DATA
WHERE PARENT_ID IS NULL
UNION ALL
SELECT E.ID, E.PARENT_ID, LV + 1
FROM ECOLI_DATA E
JOIN GENERATION G ON E.PARENT_ID = G.ID
)
SELECT ID
FROM GENERATION
WHERE LV = 3
ORDER BY ID

now me
On my github
728x90
'Coding Test > SQL' 카테고리의 다른 글
| [Programmers Lv.4] FrontEnd 개발자 찾기 - 276035 (1) | 2025.01.12 |
|---|---|
| [Programmers Lv.4] 자동차 대여 기록 별 대여 금액 구하기 - 151141 (0) | 2025.01.05 |
| [Programmers Lv.4] 입양 시각 구하기(2) - 59413 (1) | 2025.01.04 |
| [Programmers Lv.4] 오프라인/온라인 판매 데이터 통합하기 - 131537 (0) | 2024.10.25 |
| [Programmers Lv.4] 연간 평가점수에 해당하는 평가 등급 및 성과금 조회하기 - 284528 (0) | 2024.10.24 |