277

I am trying to find a MySQL query that will find DISTINCT values in a particular field, count the number of occurrences of that value and then order the results by the count.

example db

id         name
-----      ------
1          Mark
2          Mike
3          Paul
4          Mike
5          Mike
6          John
7          Mark

expected result

name       count
-----      -----
Mike       3
Mark       2
Paul       1
John       1

4 Answers 4

486
SELECT name,COUNT(*) as count 
FROM tablename 
GROUP BY name 
ORDER BY count DESC;
11
  • 2
    What exactly is the group by doing here? It is not clear what is purpose is? It seems it should work with out it if you where just reading it plainly. Sep 19, 2011 at 15:46
  • 22
    While Amber's query is the correct answer for the question, I would like to make a correction on her comment to avoid leading new people astray. If you leave off the "group by" in a MySQL query, you don't get [Mike, 1], [Mike, 1], you get a single result which will be the name on the FIRST row returned, and a count of number of rows in the table, so in this case [Mark, 7]. count(), as an aggregate function works on the entire dataset, suming, counting, or concating the specified field down to one row. Group by subdivides the dataset into chunks based on unique combos of the specified fields Aug 24, 2012 at 16:37
  • @Avatar_Squadron Quite right; I've removed the previous comment - it was off the top of my head, and I don't tend to actually observe the results of count without group by much. :)
    – Amber
    Aug 25, 2012 at 2:59
  • 3
    Something I struggled with was eliminating results with no duplicates. You can't throw a count(*) > 1 into a where clause because it's an aggregate functions. You also get a very unhelpful message: "Invalid use of group function." The right way is to alias the count name,COUNT(*) as cnt and add a having clause like so: HAVING count > 1.
    – Patrick M
    May 14, 2013 at 0:25
  • 4
    @PatrickM Yes, HAVING is for conditions that should be applied after aggregation, whereas WHERE is for conditions that should be applied before it. (Another way of thinking of this is that WHERE applies to the original row data; HAVING applies to the output row data.)
    – Amber
    May 17, 2013 at 23:22
20

What about something like this:

SELECT
  name,
  count(*) AS num
FROM
  your_table
GROUP BY
  name
ORDER BY
  count(*)
  DESC

You are selecting the name and the number of times it appears, but grouping by name so each name is selected only once.

Finally, you order by the number of times in DESCending order, to have the most frequently appearing users come first.

3
  • Your query helped me. It returns a few rows as a result. I also wanted to know how to find the count of this result. Tried a few queries but it doesn't seem to be able to do a count on an aggregate. Could you help with that?
    – Nav
    Jun 2, 2016 at 5:41
  • @Nav - a count of what? The number of rows returned? That's SELECT COUNT(DISTINCT name) as count FROM your_table For a count of the total rows of the table, do Pascal's query without the group by statement. Dec 8, 2016 at 21:15
  • @AutumnLeonard thanx man. I got the answer from your comment then the answer.. :) Sep 23, 2021 at 5:21
9

Just changed Amber's COUNT(*) to COUNT(1) for the better performance.

SELECT name, COUNT(1) as count 
FROM tablename 
GROUP BY name 
ORDER BY count DESC;
1
  • 1
    Actually what you said is partial here
    – James
    Nov 10, 2019 at 10:51
0
$sql ="SELECT DISTINCT column_name FROM table_NAME";

$res = mysqli_query($connection_variable,$sql);

while($row = mysqli_fetch_assoc($res))
{
$sqlgetnum = "select count(*) as count from table_NAME where column_name= '$row[column_name]'";
}

WORKED PROPERLY FOR ME

GROUP BY DOES GIVE ALL DISTINCT VALUES

Not the answer you're looking for? Browse other questions tagged or ask your own question.