<p>I have a table of tags and want to get the highest count tags from the list.</p>

<p>Sample data looks like this</p>

<pre><code>id (1) tag ('night')
id (2) tag ('awesome')
id (3) tag ('night')
</code></pre>

<p>using</p>

<pre><code>SELECT COUNT(*), `Tag` from `images-tags`
GROUP BY `Tag`
</code></pre>

<p>gets me back the data I'm looking for perfectly. However, I would like to organize it, so that the highest tag counts are first, and limit it to only send me the first 20 or so.</p>

<p>I tried this...</p>

<pre><code>SELECT COUNT(id), `Tag` from `images-tags`
GROUP BY `Tag`
ORDER BY COUNT(id) DESC
LIMIT 20
</code></pre>

<p>and I keep getting an "Invalid use of group function - ErrNr 1111"</p>

<p>What am I doing wrong?</p>

<p>I'm using MySQL 4.1.25-Debian</p>

MySQL prior to version 5 did not allow aggregate functions in ORDER BY clauses.
You can get around this limit with the deprecated syntax:
```
```
SELECT COUNT(id), `Tag` from `images-tags`
GROUP BY `Tag`
ORDER BY 1 DESC
LIMIT 20
```
```
1, since it's the first column you want to group on.