r/learnSQL • u/Aggravating-Street51 • Jun 16 '26
Can this be better optimized ?
SELECT
-- Select the league name and average goals scored
name AS league,
AVG(m.home_goal + m.away_goal) AS avg_goals,
-- Rank each league over the average goals
RANK () OVER(ORDER BY AVG(m.home_goal + 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
ORDER BY league_rank;I am doing a course, and this is technically what they want from me. I just don't see a point in the last line when it is ordered already in line 6. If i run with or without it gives me the same output