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 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 medals

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.

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
๐Ÿ† Olympic Ranking Rules Visualization1st Priority: ๐Ÿฅ‡ Gold Medals (Descending)Countries with more gold medals rank higherExample: 10 gold beats 9 gold (regardless of other medals)2nd Priority: ๐Ÿฅˆ Silver Medals (Descending)If gold medals are tied, more silver medals winsExample: (10g,8s) beats (10g,7s)3rd Priority: ๐Ÿฅ‰ Bronze Medals (Descending)If gold and silver tied, more bronze medals winsExample: (10g,8s,6b) beats (10g,8s,5b)4th Priority: ๐Ÿ“ Country Name (Ascending)If all medals identical, sort alphabeticallyExample: Austria comes before Belgium๐Ÿ’ก SQL: ORDER BY gold_medals DESC, silver_medals DESC, bronze_medals DESC, country ASC
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.
Asked in
Meta 15 Amazon 12 Microsoft 8 Google 6
28.4K Views
Medium Frequency
~8 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen