Use a nested query:
SELECT abc, def
FROM (
SELECT abc, def, COUNT(*) as c
FROM xpto
GROUP BY abc, def
) alias
ORDER BY abc, c DESC
(from comment) The challenge is not order abc alphabetically but by number of entries with the same value. I edited the example in question to better understand the ultimate goal.
OK, then just change the order of columns in the ORDER BY clause:
SELECT abc, def, c
FROM (
SELECT abc, def, COUNT(*) as c
FROM xpto
GROUP BY abc, def
) alias
ORDER BY c DESC, abc;
The above query gives aggregate valuse (i.e. Unique rows - one row per each pair of values).
If you wish to list all rows from the table ordered by number of entries, try this query:
SELECT abc, def,
( SELECT COUNT(*) FROM xpto x1
WHERE ( x.abc, x.def ) = (x1.abc, x1.def)
) as c
FROM xpto x
ORDER by c desc, abc
;
This query displays columns abc, def + count. If you want to display only abc + def, without the value of count, then try this query:
SELECT abc, def
FROM xpto x
ORDER by ( SELECT COUNT(*) FROM xpto x1
WHERE x.abc = x1.abc AND x.def = x1.def
) desc,
abc
;
Look at sqlfiddle demo that demonstrates these 3 queries.
Pay attention to the condition used in the second query (in the dependent subquery):
WHERE ( x.abc, x.def ) = (x1.abc, x1.def)
This syntax is complaint with ANSI SQL, however may not work on some older version of MySQL. In that case, change this condition to its equivalent version:
WHERE x.abc = x1.abc AND x.def = x1.def