Skip to content
Advertisement

Sort a table but keep groups of rows together

How do I sort a table by it’s minimum value per group but at the same time keep a group of rows together. Below a simple example of what i am trying to accomplish. The table is sorted by the lowest group value, but the group remains together. I am pretty sure this question has been asked already but i could not find an answer.

Desired output

Advertisement

Answer

If you are running MySQL 8.0, you can sort with window functions:

In earlier versions, one option is to join an aggregate subquery:

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement