1
question category 
A           X
B           Y
C           X
D           X
E           Y

I want to make the output from the most frequent category

question category
A             X
C             X
D             X
B             Y
E             Y
GMB
  • 216,147
  • 25
  • 84
  • 135
Dev
  • 11
  • 1
  • 1
    Possible duplicate of [MySQL order by before group by](https://stackoverflow.com/questions/14770671/mysql-order-by-before-group-by) – SherylHohman Nov 28 '19 at 19:58

3 Answers3

3

You could try with join on the count for category

select m.question, m.category  
from my_table  m
inner join  (
  select category, count(*) num
  from my_table  
   group by category 
) t on t.category = m.category
order by t.num desc, m.category, m.question
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
2

If you are running MySQL 8.0, you can use window functions for this. This avoids the need for a join.

select *
from mytable
order by 
    count(*) over(partition by category) desc,
    question 
GMB
  • 216,147
  • 25
  • 84
  • 135
-4

Use order by category.

select * from [table] order by category

Amjad Paracha
  • 11
  • 1
  • 3