Given an Olympic table containing country names and their medal counts, you need to sort the table according to Olympic ranking rules.
Sorting Rules:
- Countries with more gold medals come first
- If tied on gold, countries with more silver medals come first
- If tied on silver, countries with more bronze medals come first
- If tied on all medals, sort countries alphabetically (A-Z)
Write a SQL query to return all countries sorted according to these Olympic ranking rules.
Table Schema
| Column Name | Type | Description |
|---|---|---|
country
PK
|
varchar | Country name (primary key) |
gold_medals
|
int | Number of gold medals won |
silver_medals
|
int | Number of silver medals won |
bronze_medals
|
int | Number of bronze medals won |
Input & Output
| country | gold_medals | silver_medals | bronze_medals |
|---|---|---|---|
| China | 10 | 10 | 20 |
| South Sudan | 0 | 0 | 1 |
| USA | 10 | 10 | 20 |
| Russia | 10 | 10 | 20 |
| country | gold_medals | silver_medals | bronze_medals |
|---|---|---|---|
| China | 10 | 10 | 20 |
| Russia | 10 | 10 | 20 |
| USA | 10 | 10 | 20 |
| South Sudan | 0 | 0 | 1 |
Countries are sorted by Olympic rules: China, Russia, and USA all have 10 gold medals (tied), 10 silver medals (tied), and 20 bronze medals (tied). Since all medal counts are identical, they are sorted alphabetically: China comes first, then Russia, then USA. South Sudan has fewer gold medals so ranks last.
| country | gold_medals | silver_medals | bronze_medals |
|---|---|---|---|
| Norway | 2 | 1 | 3 |
| Canada | 2 | 3 | 1 |
| country | gold_medals | silver_medals | bronze_medals |
|---|---|---|---|
| Canada | 2 | 3 | 1 |
| Norway | 2 | 1 | 3 |
Both countries have 2 gold medals (tied), so we compare silver medals. Canada has 3 silver medals while Norway has 1 silver medal, so Canada ranks higher despite having fewer bronze medals. Gold medals are prioritized first, then silver breaks the tie.
| country | gold_medals | silver_medals | bronze_medals |
|---|---|---|---|
| France | 1 | 2 | 1 |
| Italy | 1 | 2 | 3 |
| country | gold_medals | silver_medals | bronze_medals |
|---|---|---|---|
| Italy | 1 | 2 | 3 |
| France | 1 | 2 | 1 |
Both countries have 1 gold medal (tied) and 2 silver medals (tied), so we compare bronze medals. Italy has 3 bronze medals while France has 1 bronze medal, so Italy ranks higher. This demonstrates the bronze medal tiebreaker rule.
Constraints
-
1 ≤ number of countries ≤ 1000 -
0 ≤ gold_medals, silver_medals, bronze_medals ≤ 100 -
countrynames are unique and contain only letters and spaces -
countryis the primary key