razorcode7 razorcode7 - 6 months ago 27
SQL Question

Descending order in sql

Can't seem to get my column to show in Descending order.

SELECT ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM Employee),2)||" %" AS "% of Employee", R.Region AS Region
FROM Employee R
GROUP BY R.Region
ORDER DESC "% of Employee" DESC


It prints out the order of Region in DESC instead of % of employee

% of Employee | Region
-----------------------
3.08% | Darwin
20.64% | Copley
2.44% | Brisbane
2.44% | Albany

Answer

That's because you're ordering on VARCHAR. The Regions being displayed in descending order is just a coincidence. You need to order by just the COUNT(*) instead:

SELECT 
    ROUND(COUNT(*) * 100.0 / (SELECT COUNT(*) FROM Employee),2)||" %" AS "% of Employee", R.Region 
    AS Region
FROM Employee R
GROUP BY R.Region
ORDER BY COUNT(*) DESC