Computer Science/Database

[SQL] JOIN을 다 해보자

해피단무지 2020. 5. 29. 16:08

쿼리 

select * from hong_a;
 select * from hong_b;
 
-- inner join
 SELECT a.emp_id, b.emp_id
      FROM hong_a a inner join hong_b b
      on a.emp_id = b.emp_id;
-- natural join      
 SELECT emp_id -- * 도 동일한 결과 
      FROM hong_a a natural join hong_b b ;
-- right outer join
 SELECT a.emp_id, b.emp_id
      FROM hong_a a right outer join hong_b b
      on a.emp_id = b.emp_id;
-- left outer join
 SELECT *
      FROM hong_a a left outer join hong_b b
      on a.emp_id = b.emp_id;
-- full outer join
 SELECT *
      FROM hong_a a full outer join hong_b b
      on a.emp_id = b.emp_id;
-- cross join
SELECT *
      FROM hong_a a cross join hong_b b;
     -- on a.emp_id = b.emp_id;
--SELECT a.emp_id, b.emp_id
--FROM hong_a a, hong_b b; -- cross join과 동일한 효과

-- union -- full outer join과 동일한 효과
   SELECT a.emp_id, b.emp_id 
      FROM hong_a a right outer join hong_b b
      on a.emp_id = b.emp_id
  UNION 
  SELECT a.emp_id, b.emp_id 
      FROM hong_a a left outer join hong_b b
      on a.emp_id = b.emp_id;
-- self join
 SELECT *
      FROM hong_a a join hong_a b
      on a.emp_id = b.emp_id;

결과

Result Set 274

EMP_ID

10
20
40
10

Download CSV
4 rows selected.

Result Set 275

EMP_ID

10
20
30

Download CSV
3 rows selected.

Result Set 276

EMP_IDEMP_ID

10 10
10 10
20 20

Download CSV
3 rows selected.

Result Set 277

EMP_ID

10
10
20

Download CSV
3 rows selected.

Result Set 278

EMP_IDEMP_ID

10 10
20 20
10 10
- 30

Download CSV
4 rows selected.

Result Set 279

EMP_IDEMP_ID

10 10
10 10
20 20
40 -

Download CSV
4 rows selected.

Result Set 280

EMP_IDEMP_ID

10 10
10 10
20 20
- 30
40 -

Download CSV
5 rows selected.

Result Set 281

EMP_IDEMP_ID

10 10
10 20
10 30
20 10
20 20
20 30
40 10
40 20
40 30
10 10
10 20
10 30

Download CSV
12 rows selected.

Result Set 282

EMP_IDEMP_ID

10 10
20 20
40 -
- 30

Download CSV
4 rows selected.

Result Set 283

EMP_IDEMP_ID

10 10
10 10
20 20
40 40
10 10
10 10

Download CSV
6 rows selected.