--- id: sql-sum title: "SQL Sum" category: "Database" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["SQL SUM() Function", "SUM function", "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", "sum", "aggregate"] raw_sources: ["https://www.w3schools.com/sql/sql_sum.asp"] applied_in: [] github_commit: "" --- # [[SQL Sum]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) SUM() totals a numeric column, ignores NULLs, and can take an expression (not just a bare column) β€” including a value computed by joining in another table's price. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **SUM() function** β€” calculates the total sum of values within a numeric column; ignores NULL values. [S1] - **Syntax** β€” `SELECT SUM(column_name) FROM table_name WHERE condition;`. [S1] - **SUM() with an expression** β€” the argument can be an expression like `Quantity * 10`, not just a column reference. [S1] - **SUM() with GROUP BY** β€” returns a sum per group. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Join-then-sum for real totals** β€” instead of assuming a flat unit price, joining the detail table to the actual `Products` table and summing `Price * Quantity` gives the real total, illustrating that SUM() arguments can encode business logic via JOIN + expression. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic sum: `SELECT SUM(Quantity) FROM OrderDetails;`. [S1] - With WHERE: `SELECT SUM(Quantity) FROM OrderDetails WHERE ProductId = 11;`. [S1] - With alias: `SELECT SUM(Quantity) AS total FROM OrderDetails;`. [S1] - With GROUP BY: `SELECT OrderID, SUM(Quantity) AS [Total Quantity] FROM OrderDetails GROUP BY OrderID;`. [S1] - With an expression: `SELECT SUM(Quantity * 10) FROM OrderDetails;`. [S1] - With a JOIN for real prices: `SELECT SUM(Price * Quantity) FROM OrderDetails LEFT JOIN Products ON OrderDetails.ProductID = Products.ProductID;`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) μ†ŒμŠ€μ—μ„œ λͺ¨μˆœλ˜λŠ” μ •λ³΄λŠ” λ°œκ²¬λ˜μ§€ μ•ŠμŒ. ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” 맀좜/μˆ˜λŸ‰ 합계 리포트의 κΈ°λ³Έ ν•¨μˆ˜λ‘œ, JOINκ³Ό κ²°ν•©ν•œ μ‹€μ œ 맀좜 계산 μ˜ˆμ‹œκ°€ μ†ŒμŠ€μ— 직접 λ“±μž₯ν•œλ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) SUM with expression and JOIN (SQL): ```sql SELECT SUM(Price * Quantity) FROM OrderDetails LEFT JOIN Products ON OrderDetails.ProductID = Products.ProductID; ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[SQL Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[SQL Aggregate Functions]], [[SQL Group By]], [[SQL Left Join]] - **μ°Έμ‘° λ§₯락:** 합계 리포트의 κΈ°λ³Έ ν•¨μˆ˜ β€” ν‘œν˜„μ‹Β·JOINκ³Ό κ²°ν•©ν•΄ μ‹€μ œ 맀좜/μˆ˜λŸ‰ 계산에 쓰인닀. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” SQL SUM() Function β€” https://www.w3schools.com/sql/sql_sum.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "SQL SUM() Function" page (Astra wiki-curation, P-Reinforce v3.1 format).