--- id: sql-self-join title: "SQL Self Join" category: "Database" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Self Join", "SQL 자기 쑰인"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.88 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["sql", "database", "w3schools", "join", "self-join"] raw_sources: ["https://www.w3schools.com/sql/sql_join_self.asp"] applied_in: [] github_commit: "" --- # [[SQL Self Join]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) A self join is a regular join where a table is joined with itself, made possible by giving the same table two different aliases so rows can be compared against other rows in the same table. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Self join** β€” a regular join where the table is joined with itself. [S1] - **Syntax** β€” `SELECT column_name(s) FROM table1 T1, table1 T2 WHERE condition;` where T1 and T2 are different aliases for the same table. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Exclude self-match** β€” a self join typically needs a condition like `A.CustomerID <> B.CustomerID` to avoid matching each row with itself. [S1] - **Dual-alias comparison** β€” the same table appears twice with different aliases (A, B) purely so SQL can treat the two references as independent row sets for comparison. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Match customers that share the same city (excluding matching a customer with itself): [S1] ```sql SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City FROM Customers A, Customers B WHERE A.CustomerID <> B.CustomerID AND A.City = B.City ORDER BY A.City; ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) μ†ŒμŠ€μ—μ„œ λͺ¨μˆœλ˜λŠ” μ •λ³΄λŠ” λ°œκ²¬λ˜μ§€ μ•ŠμŒ. ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” 쑰직도(상사-λΆ€ν•˜ 관계), 동일 λ„μ‹œ 고객 μ°ΎκΈ° λ“± "같은 ν…Œμ΄λΈ” λ‚΄ ν–‰ κ°„ 관계"λ₯Ό ν‘œν˜„ν•  λ•Œ μ“°μ΄λŠ” μ „ν˜•μ  νŒ¨ν„΄μ΄λ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Self join with alias and self-match exclusion (SQL): ```sql SELECT A.CustomerName AS CustomerName1, B.CustomerName AS CustomerName2, A.City FROM Customers A, Customers B WHERE A.CustomerID <> B.CustomerID AND A.City = B.City ORDER BY A.City; ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.88 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[SQL Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[SQL Joins]], [[SQL Aliases]], [[SQL Inner Join]] - **μ°Έμ‘° λ§₯락:** 같은 ν…Œμ΄λΈ” λ‚΄ ν–‰ κ°„ 관계λ₯Ό ν‘œν˜„ν•΄μ•Ό ν•  λ•Œ(계측 ꡬ쑰, 동일 κ·Έλ£Ή μ°ΎκΈ°) μž¬μ‚¬μš©. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” SQL Self Join β€” https://www.w3schools.com/sql/sql_join_self.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "SQL Self Join" page (Astra wiki-curation, P-Reinforce v3.1 format).