Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Order By Count

If I have a table and data like this:

ID |  Name  |  Group     1    Apple     A      2    Boy       A  3    Cat       B  4    Dog       C  5    Elep      C  6    Fish      C 

and I wish to order it according to the total of Group from smallest to largest value, such as : A - 2 records , B - 1 record , C - 3 records , so it will become:

3    Cat       B  1    Apple     A      2    Boy       A  4    Dog       C  5    Elep      C  6    Fish      C 

I tried

    $sql = "SELECT ID,Name FROM table ORDER BY COUNT(Group)"; 

but it just returns one result for me.

Are there any hints? Thank you.

like image 644
Irene Ling Avatar asked Mar 03 '12 11:03

Irene Ling


People also ask

Can I ORDER BY count in SQL?

ORDER BY COUNT clause in standard query language(SQL) is used to sort the result set produced by a SELECT query in an ascending or descending order based on values obtained from a COUNT function. For uninitiated, a COUNT() function is used to find the total number of records in the result set.

How do I ORDER BY Count desc?

Use ORDER BY with DESC to order in descending order. For counting the values, use the COUNT(). For example, if the name “John” appears thrice in the column, then a separate column will display the count 3 and in this way all the count values will be arranged in descending order using the ORDER BY DESC.

How do I arrange a count in descending order in SQL?

The ORDER BY command is used to sort the result set in ascending or descending order. The ORDER BY command sorts the result set in ascending order by default. To sort the records in descending order, use the DESC keyword.

How do I count total orders in SQL?

The first step is to use the GROUP BY clause to create the groups (in our example, we group by the country column). Then, in the ORDER BY clause, you use the aggregate function COUNT, which counts the number of values in the column of your choice; in our example, we count distinct IDs with COUNT(id) .


2 Answers

You need to aggregate the data first, this can be done using the GROUP BY clause:

SELECT Group, COUNT(*) FROM table GROUP BY Group ORDER BY COUNT(*) DESC 

The DESC keyword allows you to show the highest count first, ORDER BY by default orders in ascending order which would show the lowest count first.

like image 142
Vince Pergolizzi Avatar answered Sep 20 '22 20:09

Vince Pergolizzi


...none of the other answers seem to do what the asker asked.

For table named 'things' with column 'group':

SELECT   things.*, counter.count FROM   things LEFT JOIN (   SELECT     things.group, count(things.group) as count   FROM     things   GROUP BY     things.group ) counter ON counter.group = things.group ORDER BY   counter.count ASC; 

which gives:

id | name  | group | count  --------------------------- 3  | Cat   | B     | 1 1  | Apple | A     | 2 2  | Boy   | A     | 2 4  | Dog   | C     | 3 5  | Elep  | C     | 3 6  | Fish  | C     | 3 
like image 42
xxjjnn Avatar answered Sep 18 '22 20:09

xxjjnn