Sort the Olympic Table - Problem
Sort the Olympic Medal Table
You're given a database table containing Olympic medal counts for different countries. Your task is to sort the Olympic table according to official ranking rules.
The
โข
โข
โข
โข
Official Olympic Ranking Rules:
1. ๐ฅ Countries with more gold medals rank higher
2. ๐ฅ If tied on gold, countries with more silver medals rank higher
3. ๐ฅ If tied on silver, countries with more bronze medals rank higher
4. ๐ If tied on all medals, sort alphabetically by country name
Write a SQL query to return all countries sorted by these Olympic ranking rules.
You're given a database table containing Olympic medal counts for different countries. Your task is to sort the Olympic table according to official ranking rules.
The
Olympic table contains:โข
country (varchar): Country name (primary key)โข
gold_medals (int): Number of gold medalsโข
silver_medals (int): Number of silver medalsโข
bronze_medals (int): Number of bronze medalsOfficial Olympic Ranking Rules:
1. ๐ฅ Countries with more gold medals rank higher
2. ๐ฅ If tied on gold, countries with more silver medals rank higher
3. ๐ฅ If tied on silver, countries with more bronze medals rank higher
4. ๐ If tied on all medals, sort alphabetically by country name
Write a SQL query to return all countries sorted by these Olympic ranking rules.
Input & Output
example_1.sql โ Basic Olympic Ranking
$
Input:
Olympic table:
+----------+-------------+---------------+---------------+
| country | gold_medals | silver_medals | bronze_medals |
+----------+-------------+---------------+---------------+
| USA | 10 | 10 | 10 |
| China | 10 | 9 | 8 |
| Japan | 5 | 5 | 5 |
| Australia| 10 | 10 | 11 |
+----------+-------------+---------------+---------------+
โบ
Output:
+----------+-------------+---------------+---------------+
| country | gold_medals | silver_medals | bronze_medals |
+----------+-------------+---------------+---------------+
| Australia| 10 | 10 | 11 |
| USA | 10 | 10 | 10 |
| China | 10 | 9 | 8 |
| Japan | 5 | 5 | 5 |
+----------+-------------+---------------+---------------+
๐ก Note:
All countries with 10 gold medals come first. Among them, Australia and USA both have 10 silver medals (more than China's 9), so they rank higher. Between Australia and USA (tied on gold and silver), Australia wins due to more bronze medals (11 vs 10). China follows with fewer silver medals, and Japan is last with fewer gold medals.
example_2.sql โ Alphabetical Tie-Breaking
$
Input:
Olympic table:
+----------+-------------+---------------+---------------+
| country | gold_medals | silver_medals | bronze_medals |
+----------+-------------+---------------+---------------+
| Germany | 5 | 3 | 2 |
| France | 5 | 3 | 2 |
| Brazil | 5 | 3 | 2 |
+----------+-------------+---------------+---------------+
โบ
Output:
+----------+-------------+---------------+---------------+
| country | gold_medals | silver_medals | bronze_medals |
+----------+-------------+---------------+---------------+
| Brazil | 5 | 3 | 2 |
| France | 5 | 3 | 2 |
| Germany | 5 | 3 | 2 |
+----------+-------------+---------------+---------------+
๐ก Note:
All three countries have identical medal counts (5 gold, 3 silver, 2 bronze), so the tie-breaking rule applies: sort alphabetically by country name. Brazil comes first, followed by France, then Germany.
example_3.sql โ Single Country
$
Input:
Olympic table:
+----------+-------------+---------------+---------------+
| country | gold_medals | silver_medals | bronze_medals |
+----------+-------------+---------------+---------------+
| Norway | 3 | 2 | 1 |
+----------+-------------+---------------+---------------+
โบ
Output:
+----------+-------------+---------------+---------------+
| country | gold_medals | silver_medals | bronze_medals |
+----------+-------------+---------------+---------------+
| Norway | 3 | 2 | 1 |
+----------+-------------+---------------+---------------+
๐ก Note:
With only one country, the result is simply that country. No sorting is needed, but the query should still work correctly.
Constraints
- 1 โค number of countries โค 200
- 0 โค gold_medals, silver_medals, bronze_medals โค 100
- country names contain only letters and spaces
- All country names are unique (primary key constraint)
Visualization
Tap to expand
Understanding the Visualization
1
Group by Gold
Countries with more gold medals automatically rank higher
2
Break Gold Ties
Among countries with same gold, those with more silver rank higher
3
Break Silver Ties
Among countries tied on gold and silver, more bronze medals win
4
Alphabetical Final Tie-Break
If all medals are identical, sort alphabetically by country name
Key Takeaway
๐ฏ Key Insight: SQL's ORDER BY clause naturally handles multiple sorting criteria in priority order, making Olympic table ranking a simple one-line sort specification.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code