Building a Leaderboard

A leaderboard converts stored results into a ranked, understandable output.

SELECT team_name,
       SUM(wins) AS wins,
       SUM(losses) AS losses,
       SUM(wins) * 3 AS points
FROM team_results
GROUP BY team_name
ORDER BY points DESC, wins DESC, team_name ASC;

The points rule is an example only. Use the rule justified by the chosen context.

Display the ranking

Iterate through the query result and create a table with rank, team, wins, losses and points. Increment rank inside the loop. Escape text and cast numbers.

Explain tie behaviour. A deterministic secondary sort prevents ranks from changing unpredictably.

Test

Use a small dataset where you can calculate the result manually. Include a tie, zero values and an empty dataset.

Check


Revision #1
Created 2026-08-18 23:36:28 UTC by Mr Napper
Updated 2026-08-18 23:36:32 UTC by Mr Napper