<p>I know I need to have (although I don't know why) a <code>GROUP BY</code> clause on the end of a SQL query that uses any aggregate functions like <code>count</code>, <code>sum</code>, <code>avg</code>, etc:</p>

<pre><code>SELECT count(userID), userName
FROM users
GROUP BY userName
</code></pre>

<p>When else would <code>GROUP BY</code> be useful, and what are the performance ramifications?</p>

[{'answer_id': 2704, 'author': 'GateKiller', 'author_id': 383, 'author_profile': 'https://Stackoverflow.com/users/383', 'pm_score': 2, 'selected': False, 'text': '<p>Counting the number of times tags are used might be a google example:</p>\n\n<pre><code>SELECT TagName, Count(*)\nAS TimesUsed\nFROM Tags\nGROUP BY TagName ORDER TimesUsed\n</code></pre>\n\n<p>If you simply want a distinct value of tags, I would prefer to use the <code>DISTINCT</code> statement.</p>\n\n<pre><code>SELECT DISTINCT TagName\nFROM Tags\nORDER BY TagName ASC\n</code></pre>\n'}, {'answer_id': 2706, 'author': 'Dillie-O', 'author_id': 71, 'author_profile': 'https://Stackoverflow.com/users/71', 'pm_score': 0, 'selected': False, 'text': '<p>GROUP BY also helps when you want to generate a report that will average or sum a bunch of data. You can GROUP By the Department ID and the SUM all the sales revenue or AVG the count of sales for each month.</p>\n'}, {'answer_id': 2707, 'author': 'Stu', 'author_id': 414, 'author_profile': 'https://Stackoverflow.com/users/414', 'pm_score': 2, 'selected': False, 'text': '<p>Group By forces the entire set to be populated before records are returned (since it is an implicit sort).</p>\n\n<p>For that reason (and many others), never use a Group By in a subquery.</p>\n'}, {'answer_id': 2736, 'author': 'Chris Farmer', 'author_id': 404, 'author_profile': 'https://Stackoverflow.com/users/404', 'pm_score': 6, 'selected': True, 'text': '<p>To retrieve the number of widgets from each widget category that has more than 5 widgets, you could do this:</p>\n\n<pre><code>SELECT WidgetCategory, count(*)\nFROM Widgets\nGROUP BY WidgetCategory\nHAVING count(*) &gt; 5\n</code></pre>\n\n<p>The "having" clause is something people often forget about, instead opting to retrieve all their data to the client and iterating through it there.</p>\n'}, {'answer_id': 16212, 'author': 'Seibar', 'author_id': 357, 'author_profile': 'https://Stackoverflow.com/users/357', 'pm_score': 4, 'selected': False, 'text': '<p>GROUP BY is similar to DISTINCT in that it groups multiple records into one.</p>\n\n<p>This example, borrowed from <a href="http://www.devguru.com/technologies/t-sql/7080.asp" rel="noreferrer">http://www.devguru.com/technologies/t-sql/7080.asp</a>, lists distinct products in the Products table. </p>\n\n<pre><code>SELECT Product FROM Products GROUP BY Product\n\nProduct\n-------------\nDesktop\nLaptop\nMouse\nNetwork Card\nHard Drive\nSoftware\nBook\nAccessory\n</code></pre>\n\n<p>The advantage of GROUP BY over DISTINCT, is that it can give you granular control when used with a HAVING clause.</p>\n\n<pre><code>SELECT Product, count(Product) as ProdCnt\nFROM Products\nGROUP BY Product\nHAVING count(Product) &gt; 2\n\nProduct      ProdCnt\n--------------------\nDesktop          10\nLaptop            5\nMouse             3\nNetwork Card      9\nSoftware          6\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/2702', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/357/']