As of MySQL 8.0.12,  ORDER BY  and  ROLLUP  can be used together, which enables the use of  ORDER BY and  GROUPING()  to achieve a specific sort order of grouped results. For example: GROUP BY Modifiers mysql>  SELECT year, SUM(profit) AS profit FROM sales GROUP BY year WITH ROLLUP ORDER BY GROUPING(year) DESC; +------+--------+ | year | profit | +------+--------+ | NULL | 7535 | | 2000 | 4525 | | 2001 | 3010 | +------+--------+ In both cases, the super-aggregate summary rows sort with the rows from which they are calculated, and their placement depends on sort order (at the end for ascending sort, at the beginning for descending sort). LIMIT  can be used to restrict the number of rows returned to the client.  LIMIT  is applied after  ROLLUP , so the limit applies against the extra rows added by  ROLLUP . For example: +------+---------+------------+--------+ | year | country | product | profit | +------+---------+------------+--------+ | 2000 | Finland | Computer | 1500 | | 2000 | Finland | Phone | 100 | | 2000 | Finland | NULL | 1600 | | 2000 | India | Calculator | 150 | | 2000 | India | Computer | 1200 | +------+---------+------------+--------+ mysql>  SELECT year, country, product, SUM(profit) AS profit FROM sales GROUP BY year, country, product WITH ROLLUP LIMIT 5; Using  LIMIT  with  ROLLUP  may produce results that are more difficult to interpret, because there is less context for understanding the super-aggregate rows. A MySQL extension permits a column that does not appear in the  GROUP BY  list to be named in the select list. (For information about nonaggregated columns and  GROUP BY , see Section 12.20.3, “MySQL Handling of GROUP BY” .) In this case, the server is free to choose any value from this nonaggregated column in summary rows, and this includes the extra rows added by  WITH ROLLUP . For example, in the following query,  country  is a nonaggregated column that does not appear in the  GROUP BY  list and values chosen for this column are nondeterministic: mysql>  SELECT year, country, SUM(profit) AS profit FROM sales GROUP BY year WITH ROLLUP; +------+---------+--------+ | year | country | profit | +------+---------+--------+ | 2000 | India | 4525 | | 2001 | USA | 3010 | | NULL | USA | 7535 | +------+---------+--------+ This behavior is permitted when the  ONLY_FULL_GROUP_BY  SQL mode is not enabled. If that mode is enabled, the server rejects the query as illegal because  country  is not listed in the  GROUP BY  clause. With  ONLY_FULL_GROUP_BY  enabled, you can still execute the query by using the  ANY_VALUE()  function for nondeterministic-value columns: mysql>  SELECT year, ANY_VALUE(country) AS country, SUM(profit) AS profit FROM sales 2410