[SQL문제풀기] 오랜 기간 보호한 동물(JOIN)

241209
이나겸's avatar
Dec 09, 2024
[SQL문제풀기] 오랜 기간 보호한 동물(JOIN)
Contents
Q.풀이

Q.

notion image
 
 

풀이

  • left join
    • left join 기준 왼쪽 테이블인 animal_ins 테이블의 모든 데이터 포함
    • 오른쪽 테이블인 animal_outs에 매칭되는 데이터가 없으면 null로 표시
  • where a.animal_id is null
    • animal_outs 테이블에 해당 animal_id가 없는 경우를 필터링, 아직 입양되지 않은 동물만 선택
  • order by
    • 정렬 (asc가 작성되어있지않아도 오름차순이 디폴트)
  • limit 3
    • 상위 3개의 데이터만 반환
select b.name, b.datetime from animal_ins as b left join animal_outs as a on b.animal_id = a.animal_id where a.animal_id is null order by b.datetime limit 3;
Share article

Nakyeom's Study