chiggy_viggy
chiggy_viggy

Reputation: 1

I'm trying to find the Top 5 results of SQL Query but getting an error

SELECT Top 5* 
FROM (
select
    SUM(CASE WHEN Medal = 'Gold' THEN 1 ELSE 0 END) AS 'Gold_Medals',
    Country_Code
from olympic_medals
GROUP BY Country_Code
ORDER BY Gold_Medals DESC)

ERROR:

The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

Upvotes: 0

Views: 34

Answers (1)

juergen d
juergen d

Reputation: 204766

No need for a subquery

select top 5 SUM(CASE WHEN Medal = 'Gold' THEN 1 ELSE 0 END) AS 'Gold_Medals',
       Country_Code
from olympic_medals
GROUP BY Country_Code
ORDER BY Gold_Medals DESC

Upvotes: 1

Related Questions