Data/PostgreSQL
PostgreSQL outer join
바리새인
2025. 1. 29. 10:41
Left outer join: a 기준으로 다 나옴
SELECT
a.id AS table_a_id,
a.name AS table_a_name,
b.id AS table_b_id,
b.description AS table_b_description
FROM table_a a
LEFT OUTER JOIN table_b b ON a.id = b.a_id;
table_a_id | table_a_name | table_b_id | table_b_description
-----------+-------------+------------+----------------------
1 | Apple | 1 | Fruit
2 | Carrot | NULL | NULL
3 | Orange | 3 | Citrus
Right outer join: b 기준으로 다 나옴
SELECT
a.id AS table_a_id,
a.name AS table_a_name,
b.id AS table_b_id,
b.description AS table_b_description
FROM table_a a
RIGHT OUTER JOIN table_b b ON a.id = b.a_id;
table_a_id | table_a_name | table_b_id | table_b_description
-----------+-------------+------------+----------------------
1 | Apple | 1 | Fruit
NULL | NULL | 2 | Vegetable
3 | Orange | 3 | Citrus
Full outer join: 양쪽 다 나옴
SELECT
a.id AS table_a_id,
a.name AS table_a_name,
b.id AS table_b_id,
b.description AS table_b_description
FROM table_a a
FULL OUTER JOIN table_b b ON a.id = b.a_id;
table_a_id | table_a_name | table_b_id | table_b_description
-----------+-------------+------------+----------------------
1 | Apple | 1 | Fruit
2 | Carrot | NULL | NULL
3 | Orange | 3 | Citrus
NULL | NULL | 2 | Vegetable