C.W.K.
Stream
Lesson 03 of 10 · published

INNER JOIN — 기본

~12 min · joins, inner-join, querying

Level 0Scout
0 XP0/80 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

두 테이블 모두 매치되는 row

INNER JOIN 은 join predicate 가 true 인 두 테이블 row pair 마다 한 row 반환. 어느 쪽에서든 매치 안 되는 row 는 결과에서 drop.

SELECT cols FROM A INNER JOIN B ON A.x = B.x;

JOIN 만 쓰면 INNER JOIN — 키워드 optional 이지만 명시적으로 쓰면 다음 reader 한테 의도 명확.

Tip: 테이블 항상 alias (FROM authors a INNER JOIN posts p) 하고 컬럼 qualify. id 라는 컬럼 두 개인 unaliased query 는 못 읽고 silently 모호함.

Code

Author 정보와 함께 post·sql
SELECT p.id, p.title, a.name AS author, a.email
FROM   posts p
INNER  JOIN authors a ON a.id = p.author_id
ORDER  BY p.created_at DESC
LIMIT  20;
3 테이블 inner join·sql
-- conversations + messages + brain stat
SELECT c.id, c.title, count(m.id) AS msg_count, c.brain
FROM   conversations c
INNER  JOIN messages m ON m.conversation_id = c.id
GROUP  BY c.id
HAVING count(m.id) > 5;

External links

Exercise

authors + posts + comments 빌드. 샘플 데이터: post 없는 author 한 명, comment 없는 post 한 개 포함. INNER JOIN query 3 개: (1) post + author, (2) post + comment, (3) 셋 다 join. 각각 no-match 케이스가 row 몇 개 잃었는지 수동 COUNT 로 확인.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.