--- id: sql-group-by title: "SQL Group By" category: "Database" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["SQL GROUP BY Statement", "GROUP BY clause", "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", "groupby", "aggregate"] raw_sources: ["https://www.w3schools.com/sql/sql_groupby.asp"] applied_in: [] github_commit: "" --- # [[SQL Group By]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) GROUP BY collapses rows sharing the same value into summary rows, and is almost always paired with an aggregate function like COUNT/SUM/AVG to compute a value per group. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **GROUP BY statement** β€” groups rows with the same values into summary rows, e.g. "find the number of customers in each country". [S1] - **Pairs with aggregate functions** β€” COUNT(), MAX(), MIN(), SUM(), AVG() compute a single value per group. [S1] - **Syntax** β€” `SELECT column1, aggregate_function(column2), column3, ... FROM table_name WHERE condition GROUP BY column1, column3 ORDER BY column_name;`. [S1] - **GROUP BY with JOIN** β€” grouping works across joined tables, e.g. counting orders per shipper after a LEFT JOIN. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Group-then-sort-by-aggregate** β€” combining `GROUP BY` with `ORDER BY COUNT(...) DESC` ranks groups by their aggregate value, a very common "top categories" reporting pattern. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Count customers per country: `SELECT Country, COUNT(CustomerID) AS [Number of Customers] FROM Customers GROUP BY Country;`. [S1] - Same, sorted by count descending: `... GROUP BY Country ORDER BY COUNT(CustomerID) DESC;`. [S1] - GROUP BY with a JOIN: `SELECT Shippers.ShipperName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders LEFT JOIN Shippers ON Orders.ShipperID = Shippers.ShipperID GROUP BY ShipperName;`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) μ†ŒμŠ€μ—μ„œ λͺ¨μˆœλ˜λŠ” μ •λ³΄λŠ” λ°œκ²¬λ˜μ§€ μ•ŠμŒ. ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” λ‹€μŒ 챕터인 HAVING이 GROUP BY κ²°κ³Ό 자체λ₯Ό ν•„ν„°λ§ν•˜λŠ” 방법을 ν™•μž₯ν•œλ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Count rows per group, sorted by count (SQL): ```sql SELECT Country, COUNT(CustomerID) AS [Number of Customers] FROM Customers GROUP BY Country ORDER BY COUNT(CustomerID) DESC; ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[SQL Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[SQL Having]], [[SQL Aggregate Functions]], [[SQL Count]], [[SQL Order By]] - **μ°Έμ‘° λ§₯락:** 집계 리포트(μΉ΄ν…Œκ³ λ¦¬λ³„ 집계, λž­ν‚Ή) μž‘μ„±μ˜ 핡심 문법 β€” HAVINGκ³Ό ν•¨κ»˜ 이해해야 μ™„μ „ν•΄μ§„λ‹€. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” SQL GROUP BY Statement β€” https://www.w3schools.com/sql/sql_groupby.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "SQL GROUP BY Statement" page (Astra wiki-curation, P-Reinforce v3.1 format).