# Building a Leaderboard

A leaderboard converts stored results into a ranked, understandable output.

~~~sql
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

- [ ] Ranking rule is stated.
- [ ] SQL aggregation matches the rule.
- [ ] Ties are handled predictably.
- [ ] Headers and a visible caption explain the table.
- [ ] Manual expected results match actual output.