Flip OVER your results
In the last exercise, the rank generated in your query was organized from smallest to largest. By adding DESC to your window function, you can create a rank sorted from largest to smallest.
SELECT
id,
RANK() OVER(ORDER BY home_goal DESC) AS rank
FROM match;
Deze oefening maakt deel uit van de cursus
Data Manipulation in SQL
Oefeninstructies
- Select the league
nameand average total goals scored fromleagueandmatch. - Complete the window function to rank each league from highest to lowest average goals scored.
ORDERthe main queryBYthe rank you just created.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
SELECT
-- Select the league name and average goals scored
___ AS league,
___(___ + m.away_goal) AS avg_goals,
-- Rank leagues in descending order by average goals
___ ___(___ ___ ___(___ + m.away_goal) ___) AS league_rank
FROM league AS l
LEFT JOIN match AS m
ON l.id = m.country_id
WHERE m.season = '2011/2012'
GROUP BY l.name
-- Order the query by the rank you created
___ ___ ___;