Sort the Olympic Table - Problem

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

Olympic
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
Primary Key: country
Note: Each row represents a country's medal count in the Olympics

Input & Output

Example 1 — Standard Olympic Ranking
Input Table:
country gold_medals silver_medals bronze_medals
China 10 10 20
South Sudan 0 0 1
USA 10 10 20
Russia 10 10 20
Output:
country gold_medals silver_medals bronze_medals
China 10 10 20
Russia 10 10 20
USA 10 10 20
South Sudan 0 0 1
💡 Note:

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.

Example 2 — Different Medal Priorities
Input Table:
country gold_medals silver_medals bronze_medals
Norway 2 1 3
Canada 2 3 1
Output:
country gold_medals silver_medals bronze_medals
Canada 2 3 1
Norway 2 1 3
💡 Note:

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.

Example 3 — Bronze Medal Tiebreaker
Input Table:
country gold_medals silver_medals bronze_medals
France 1 2 1
Italy 1 2 3
Output:
country gold_medals silver_medals bronze_medals
Italy 1 2 3
France 1 2 1
💡 Note:

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
  • country names are unique and contain only letters and spaces
  • country is the primary key

Visualization

Tap to expand
Olympic Table Sorting ProblemInput: Unsorted CountriescountrygoldsilverbronzeUSA101020China101020Russia101020ORDER BYOlympic RulesOutput: Olympic RankingcountrygoldsilverbronzeChina101020Russia101020USA101020Olympic Ranking Rules Applied:🥇 1st Priority: Gold medals (DESC) - All tied with 10🥈 2nd Priority: Silver medals (DESC) - All tied with 10🥉 3rd Priority: Bronze medals (DESC) - All tied with 20🔤 4th Priority: Country name (ASC) - China, Russia, USA
Understanding the Visualization
1
Input Table
Countries with medal counts in random order
2
ORDER BY Multiple Criteria
Apply 4-level sorting: gold DESC, silver DESC, bronze DESC, country ASC
3
Ranked Output
Countries sorted according to Olympic rules
Key Takeaway
🎯 Key Insight: Multi-level ORDER BY perfectly implements Olympic ranking by evaluating criteria sequentially
Asked in
Amazon 23 Microsoft 18 Google 15
28.4K Views
Medium Frequency
~8 min Avg. Time
892 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