SQL 조인

5065 단어 databasetutorialsql

내부 조인





SQL 쿼리:

SELECT * FROM A INNER JOIN B ON A.KEY = B.KEY


정의 :

Select tables A & B and compare key values of table A and table B and return records which only have the identical key values in both tables of A & B. This is similar to the Set theory operation Intersection which is denoted by A ∩ B, the set containing all elements of A that also belong to B.



왼쪽 조인





SQL 쿼리:

SELECT * FROM A LEFT JOIN B ON A.KEY = B.KEY


정의 :

Select tables A & B and compare key values of table A and table B and return all records of A which is in the left side of the sql query but in case of B which is in right side of sql query, only returns records that have the same key values of table A.



오른쪽 조인





SQL 쿼리:

SELECT * FROM A RIGHT JOIN B ON A.KEY = B.KEY


정의 :

Select tables A & B and compare key values of table A and table B and return all records of B which is in the right side of the sql query but in case of A which is in left side of sql query, only returns records that have the same key values of table B.



메모 :
1. ON 절은 USING 절보다 더 일반적인 절입니다. 하나의 열, 열 집합 및 조건에 대한 테이블을 조인할 수 있습니다. 예를 들어:

SELECT * FROM city JOIN country ON city.country_code = country.country_code


2. USING는 두 테이블이 조인하는 동일한 이름의 열을 공유할 때 유용합니다. 예를 들어:

SELECT city.name, city_id FROM city JOIN country USING city_id


전체 외부 조인





SQL 쿼리:

SELECT * FROM A FULL OUTER JOIN B ON A.KEY = B.KEY


정의 :

Select tables A & B and compare key values of table A and table B and return all records of both A & B and fill in NULL for missing matches on either side.



왼쪽 외부 조인




SQL 쿼리:

SELECT * FROM A LEFT OUTER JOIN B ON A.KEY = B.KEY WHERE B.KEY IS NULL


정의 :

Left outer join returns set of records from A, with the matching records in B (which are available). If there is no match, the right side will have null.



오른쪽 외부 조인




SQL 쿼리:

SELECT * FROM A RIGHT OUTER JOIN B ON A.KEY = B.KEY WHERE A.KEY IS NULL


정의 :

Right outer join returns set of records from B, with the matching records in A (which are available). If there is no match, the left side will have null.



교차 없는 전체 외부 조인





SELECT * FROM A FULL OUTER JOIN B ON A.KEY = B.KEY WHERE A.KEY IS NULL OR B.KEY IS NULL


정의 :

Full outer join without intersection returns the set of records only in table A, but not in table B or the set of records only in table B, but not in table A. This clause returns all records that match the ON condition, excluding those are in common between two tables, or those records exist in both tables.



내 개인 블로그 : https://danyson.github.io/

좋은 웹페이지 즐겨찾기