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;
This exercise is part of the course
Data Manipulation in SQL
Exercise instructions
- 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.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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
___ ___ ___;