Help with Group By And Order By

Question 10 https://www.codecademy.com/courses/learn-sql/projects/fakeapps

I thought it would be easiest to enter the rounding and avg valuation first (especially since I’m having trouble with rounding and average code formatting), so I entered

select round(avg(valuation),2), category from startups group by 3 order by 2 desc;

No result when I enter the above, but when I run the below, I get my answer. My question is why must it be in the below order for the function to run?

select category, round(avg(valuation),2) from startups group by 1 order by 2 desc;

Thanks!

You don’t have three columns in the SELECT statement, you have two: valuation and category.
That’s why your second query works.
It doesn’t matter (unless one is asked to specifically do so) what order the columns are in, in the SELECT, just have to make sure that you’re grouping and ordering using the correct index (or, use column name instead).

2 Likes