Aggregate Functions

I did the exercise for GROUP BY, and wanted to add ORDER BY to see the results in descending order, so I typed this:

It returned an empty screen. What am I doing wrong? Thanks for any help on this.

Hey there and welcome to the forums!

When you’re using ORDER BY you need to specify which column you want to order. So you can do this in three ways, either you can give the sum column an alias or just reference it directly or use numeric referencing, like below:

-- This is one way of ensuring it works correctly
SELECT category, SUM(downloads)
FROM fake_apps
GROUP BY category
ORDER BY SUM(downloads) DESC;

-- This is another way
SELECT category, SUM(downloads) AS Total
FROM fake_apps
GROUP BY category
ORDER BY Total DESC;

-- This is a third
SELECT category, SUM(downloads)
FROM fake_apps
GROUP BY category
ORDER BY 2 DESC;

I don’t remember the exact ordering of the course so you may not have encountered some of these yet, but the first is the most basic and by the end of the course if not already the second and third will make more sense.

3 Likes

This is a big help. Thank you!