Recap - Self Join
Coddy SQL 여정의 기초 섹션에 포함된 레슨. 72개 중 47번째.
챌린지
어려움사용 가능한 테이블 및 열:
<strong>friends</strong>:<strong>id</strong>,<strong>name</strong>,<strong>friend_id</strong>
friends 테이블의 각 인물에 대해 가지고 있는 모든 건너 건너 아는 친구(friend-of-friend) 관계를 찾으세요. 건너 건너 아는 친구 관계는 A가 B와 친구이고, B가 다시 C와 친구일 때 발생합니다. 이 관계를 세 개의 열로 나열하세요:
- friend1: 시작하는 사람
- friend2: 해당 사람의 직접적인 친구
- friend3: 친구의 친구
규칙:
- friend3가 friend1과 동일한 사람인 경우는 포함하지 마세요.
- 결과를 friend1의 이름을 기준으로 내림차순 정렬하세요.
예시: Alice가 Bob과 친구이고, Bob이 Carol과 친구라면, Alice-Bob-Carol은 유효한 건너 건너 아는 친구 관계가 됩니다.
직접 해보기
-- 테이블을 자신과 두 번 조인하여 한 행이 세 사람의 체인을 담도록 합니다
SELECT ____ AS friend1, ____ AS friend2, ____ AS friend3
FROM friends f1
JOIN friends f2 ON ____
JOIN friends f3 ON ____
-- 시작한 사람으로 되돌아가는 체인을 제거합니다
WHERE ____
ORDER BY ____ DESC;기초의 모든 레슨
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns직접 연습해 보세요: SQL 플레이그라운드