--- id: sql-union title: "SQL Union" category: "Database" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["SQL UNION Operator", "UNION operator", "SQL ν•©μ§‘ν•©"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["sql", "database", "w3schools", "union"] raw_sources: ["https://www.w3schools.com/sql/sql_union.asp"] applied_in: [] github_commit: "" --- # [[SQL Union]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) UNION combines the result-sets of two or more SELECT statements into one, automatically removing duplicate rows, provided every SELECT has the same number/type/order of columns. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **UNION operator** β€” combines the result-set of two or more SELECT statements, removing duplicates automatically. [S1] - **Requirements** β€” every SELECT must have the same number of columns, similar data types, and columns in the same order. [S1] - **Syntax** β€” `SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;`. [S1] - **Result column naming** β€” result-set column names default to the first SELECT statement's column names. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Literal tag column** β€” a literal string like `'Customer' AS Type` in one SELECT and `'Supplier'` in the other lets a combined UNION query tag which source table each row came from. [S1] - **WHERE-filtered UNION** β€” each SELECT in a UNION can carry its own WHERE clause before being combined. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Combine and dedupe countries from two tables: `SELECT Country FROM Customers UNION SELECT Country FROM Suppliers ORDER BY Country;`. [S1] - Filtered UNION: `SELECT City, Country FROM Customers WHERE Country='Germany' UNION SELECT City, Country FROM Suppliers WHERE Country='Germany' ORDER BY City;`. [S1] - Tagged UNION distinguishing source: `SELECT 'Customer' AS Type, ContactName, City, Country FROM Customers UNION SELECT 'Supplier', ContactName, City, Country FROM Suppliers;`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) μ†ŒμŠ€μ—μ„œ λͺ¨μˆœλ˜λŠ” μ •λ³΄λŠ” λ°œκ²¬λ˜μ§€ μ•ŠμŒ. 쀑볡 제거λ₯Ό μ›μΉ˜ μ•ŠμœΌλ©΄ UNION ALL을 쓰라고 λͺ…μ‹œμ μœΌλ‘œ μ•ˆλ‚΄ν•¨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” 이후 UNION ALL μ±•ν„°μ—μ„œ 쀑볡 제거 μ—¬λΆ€λ§Œ λ‹€λ₯Έ λŒ€μ‘ μ—°μ‚°μžλ‘œ ν™•μž₯λœλ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Combine and dedupe two result-sets (SQL): ```sql SELECT Country FROM Customers UNION SELECT Country FROM Suppliers ORDER BY Country; ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[SQL Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[SQL Union All]], [[SQL Select Distinct]], [[SQL Order By]] - **μ°Έμ‘° λ§₯락:** μ—¬λŸ¬ SELECT κ²°κ³Όλ₯Ό ν•˜λ‚˜λ‘œ 합쳐야 ν•  λ•Œμ˜ κΈ°λ³Έ 도ꡬ β€” 쀑볡 μ œκ±°κ°€ ν•„μš” μ—†λ‹€λ©΄ UNION ALL을 λŒ€μ‹  μ‚¬μš©. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” SQL UNION Operator β€” https://www.w3schools.com/sql/sql_union.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "SQL UNION Operator" page (Astra wiki-curation, P-Reinforce v3.1 format).