109. UNION - llighter/database GitHub Wiki
The UNION [ALL] Oracle Help Centor
SQL UNION Operator W3School
UNION, INTERSECT λ° EXCEPT AWS μ€λͺ μ >> λ°μ΄ν°λ² μ΄μ€ κ°λ°μ μλ΄μ
UNION
Operator
The SQL The UNION
operator is used to combine the result-set of two or more SELECT
statements.
- Each
SELECT
statement withinUNION
must have the same number of columns - The columns must also have similar data types
- The columns in each
SELECT
statement must also be in the same order
UNION
Syntax
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
UNION ALL
Syntax
The UNION
operator selects only distinct values by default. To allow duplicate values, use UNION ALL
:
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
μ€μ μ°μ°μμ λν νκ° μμ
UNION
λ°EXCEPT
μ€μ μ°μ°μλ μ’μ°μ κ²°ν© μ°μ°μμ λλ€.- μ°μ μμμ μν₯μ μ£ΌκΈ° μν΄ κ΄νΈκ° μ§μ λμ΄ μμ§ μμ κ²½μ° μ΄λ¬ν μ€μ μ°μ°μμ μ‘°ν©μ μΌμͺ½μμ μ€λ₯Έμͺ½μΌλ‘ κ³μ°λ©λλ€.
UNION
Example 1 Oracle Help Center
The following statement combines the results of two queries with the UNION
operator, which eliminates duplicate selected rows. This statement shows that you must match datatype (using the TO_CHAR
function) when columns do not exist in one or the other table:
SELECT location_id, department_name "Department",
TO_CHAR(NULL) "Warehouse" FROM departments
UNION
SELECT location_id, TO_CHAR(NULL) "Department", warehouse_name
FROM warehouses;
LOCATION_ID Department Warehouse
----------- --------------------- --------------------------
1400 IT
1400 Southlake, Texas
1500 Shipping
1500 San Francisco
1600 New Jersey
1700 Accounting
1700 Administration
1700 Benefits
1700 Construction
...
UNION
Example 2 AWS λ°μ΄ν°λ² μ΄μ€ κ°λ°μ μλ΄μ
λ€μ UNION
쿼리μμ SALES
ν
μ΄λΈμ νμ LISTING
ν
μ΄λΈμ νκ³Ό λ³ν©λ©λλ€. κ°κ°μ ν
μ΄λΈμμ νΈνλλ 3κ°μ μ΄μ΄ μ νλλ©°, μ΄ κ²½μ°μλ ν΄λΉνλ μ΄λ€μ μ΄λ¦κ³Ό λ°μ΄ν° νμμ΄ λμΌν©λλ€.
μ΅μ’
κ²°κ³Ό μ§ν©μ LISTING
ν
μ΄λΈμ 첫 λ²μ§Έ μ΄μ κΈ°μ€μΌλ‘ μ λ ¬λκ³ LISTID
κ°μ΄ κ°μ₯ λμ 5κ°μ νμΌλ‘ μ νλ©λλ€.
select listid, sellerid, eventid from listing
union
select listid, sellerid, eventid from sales
order by listid, sellerid, eventid desc limit 5;
listid | sellerid | eventid
--------+----------+---------
1 | 36861 | 7872
2 | 16002 | 4806
3 | 21461 | 4256
4 | 8117 | 4337
5 | 1616 | 8647
(5 rows)
λ€μ μμ λ κ²°κ³Ό μ§ν©μμ μ΄λ€ 쿼리 ννμμ΄ κ°κ°μ νμ μμ±νλμ§ λ³Ό μ μλλ‘ UNION
쿼리μ μΆλ ₯μ 리ν°λ΄ κ°μ μΆκ°ν μ μλ λ°©λ²μ 보μ¬μ€λλ€. μ΄ μΏΌλ¦¬λ 첫 λ²μ§Έ 쿼리 ννμμ νμ "B"(buyer)λ‘ μλ³νκ³ λ λ²μ§Έ 쿼리 ννμμ νμ "S"(seller)λ‘ μλ³ν©λλ€.
μ΄ μΏΌλ¦¬λ $10,000 μ΄μμ ν°μΌ κ±°λμ λν΄ κ΅¬λ§€μμ νλ§€μλ₯Ό μλ³ν©λλ€. UNION
μ°μ°μμ μ΄λ νμͺ½μμ λ 쿼리 ννμμ μ μΌν μ°¨μ΄μ μ SALES
ν
μ΄λΈμ λν μ‘°μΈ μ΄μ
λλ€.
select listid, lastname, firstname, username,
pricepaid as price, 'S' as buyorsell
from sales, users
where sales.sellerid=users.userid
and pricepaid >=10000
union
select listid, lastname, firstname, username, pricepaid,
'B' as buyorsell
from sales, users
where sales.buyerid=users.userid
and pricepaid >=10000
order by 1, 2, 3, 4, 5;
listid | lastname | firstname | username | price | buyorsell
--------+----------+-----------+----------+-----------+-----------
209658 | Lamb | Colette | VOR15LYI | 10000.00 | B
209658 | West | Kato | ELU81XAA | 10000.00 | S
212395 | Greer | Harlan | GXO71KOC | 12624.00 | S
212395 | Perry | Cora | YWR73YNZ | 12624.00 | B
215156 | Banks | Patrick | ZNQ69CLT | 10000.00 | S
215156 | Hayden | Malachi | BBG56AKU | 10000.00 | B
(6 rows)
μ€λ³΅λ νμ΄ λ°κ²¬λλ κ²½μ° κ²°κ³Όμ μ΄λ° νμ μ μ§ν΄μΌ νλ―λ‘, λ€μ μμ μμλ UNION ALL
μ°μ°μλ₯Ό μ¬μ©ν©λλ€. μ΄λ²€νΈ IDμ νΉμ μ리μ¦μ λν΄, 쿼리λ κ° μ΄λ²€νΈμ κ΄λ ¨λ κ°κ°μ νλ§€μ λν΄ 0κ° μ΄μμ νμ λ°ννκ³ κ·Έ μ΄λ²€νΈμ κ° λͺ©λ‘μ λν΄ 0κ° λλ 1κ°μ νμ λ°νν©λλ€. μ΄λ²€νΈ IDλ LISTING
λ° EVENT
ν
μ΄λΈμμ κ°κ°μ νμ κ³ μ νμ§λ§, SALES
ν
μ΄λΈμμ μ΄λ²€νΈ λ° λͺ©λ‘ IDμ λμΌν μ‘°ν©μ λν΄ μ¬λ¬ κ°μ νλ§€ κ±΄μ΄ μμ μ μμ΅λλ€.
κ²°κ³Ό μ§ν©μ μΈ λ²μ§Έ μ΄μ νμ μλ³Έμ μλ³ν©λλ€. νμ μΆμ²κ° SALES
ν
μ΄λΈμΈ κ²½μ° SALESROW
μ΄μ "YES"λ‘ νμλ©λλ€. (SALESROW
λ SALES.LISTID
μ λ³μΉμ
λλ€.) νμ μΆμ²κ° LISTING
ν
μ΄λΈμΈ κ²½μ° SALESROW
μ΄μ "No"λ‘ νμλ©λλ€.
μ΄ κ²½μ°, κ²°κ³Ό μ§ν©μ λͺ©λ‘ 500, μ΄λ²€νΈ 7787μ λν΄ 3κ°μ νλ§€ νμΌλ‘ ꡬμ±λ©λλ€. μ¦, μ΄ λͺ©λ‘ λ° μ΄λ²€νΈ μ‘°ν©μ λν΄ 3κ°μ§ λ€λ₯Έ νΈλμμ
μ΄ λ°μνμ΅λλ€. λ€λ₯Έ λ λͺ©λ‘ 501 λ° 502μμλ μ΄λ€ νλ§€λ μμ±λμ§ μμμΌλ―λ‘, μΏΌλ¦¬κ° μ΄λ€ λͺ©λ‘ IDμ λν΄ μμ±νλ μ μΌν νμ μΆμ²λ LISTING
ν
μ΄λΈμ
λλ€(SALESROW
= 'No').
select eventid, listid, 'Yes' as salesrow
from sales
where listid in(500,501,502)
union all
select eventid, listid, 'No'
from listing
where listid in(500,501,502)
order by listid asc;
eventid | listid | salesrow
---------+--------+----------
7787 | 500 | No
7787 | 500 | Yes
7787 | 500 | Yes
7787 | 500 | Yes
6473 | 501 | No
5108 | 502 | No
(6 rows)
ALL
ν€μλ μμ΄ κ°μ 쿼리λ₯Ό μ€ννλ κ²½μ° κ²°κ³Όμλ νλ§€ κ±°λ μ€ νλλ§ μ μ§λ©λλ€.
select eventid, listid, 'Yes' as salesrow
from sales
where listid in(500,501,502)
union
select eventid, listid, 'No'
from listing
where listid in(500,501,502)
order by listid asc;
eventid | listid | salesrow
---------+--------+----------
7787 | 500 | No
7787 | 500 | Yes
6473 | 501 | No
5108 | 502 | No
(4 rows)