INNER JOIN
Joining two tables
To use data from both tables at once, you join them. An INNER JOIN pairs up rows that match on a key — here, the order's customer_id with the customer's id:
When a column name could come from either table, write table.column so it is clear.
두 테이블 연결하기
두 테이블의 데이터를 동시에 사용하려면 연결해야 합니다. INNER JOIN은 키에 맞춰 행을 짝지어 맞춥니다 — 여기서는 주문의 customer_id과 고객의 id를 연결합니다:
SELECT customer.name, orders.total
FROM customer
INNER JOIN orders ON customer.id = orders.customer_id;
열 이름이 어느 테이블에서 온지 모호할 경우, table.column을 작성하여 명확하게 하세요.
Only matching rows survive
An INNER JOIN keeps a row only when the ON condition finds a match in the other table.
In our shop, Ben and Dan have no orders, so they do not appear in the join — there is nothing to pair them with. Ada and Cara, who do have orders, appear once for each of their orders.
매칭되는 행만 남음
INNER JOIN는 다른 테이블에서 ON 조건에 해당하는 결과가 있을 때만 한 행을 유지합니다.
우리 상점에서 Ben과 Dan은 주문이 없으므로 연결 결과에 나타나지 않습니다—매칭할 것이 없기 때문입니다. 주문이 있는 Ada와 Cara는 각 주문당 한 번씩 나타납니다.
Common mistakes
- Match on the key with
ON; a missingONpairs every row with every row. - INNER JOIN drops rows with no match on the other side.
흔한 실수
ON키에 매칭; 누락된ON은 모든 행을 다른 모든 행과 연결합니다.- INNER JOIN은 한쪽에서 매칭이 없는 행을 제거합니다.
INNER JOIN matches keys · INNER JOIN은 키를 매칭시킵니다
Rows from two tables combine where the key matches. · 두 테이블의 행들이 키가 일치하는 지점에서 결합됩니다.
List each customer's name next to the total of each of their orders. Join customer to orders on the key, ordered by orders.id. · 각 고객의 name과 해당 주문들의 total 나열을 표시하십시오. 키를 기준으로 customer를 orders에 조인하고, orders.id으로 정렬하십시오.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Show the customer name and order total for orders of 25 or more, largest total first. · 주문 총액이 25 이상인 경우의 고객 name과 주문 total을 표시하되, 가장 큰 총액부터 정렬하십시오.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.