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
Sort the Olympic Table INPUT Country G S B Total USA 10 15 12 37 China 10 8 5 23 Japan 8 6 10 24 Germany 10 8 9 27 Australia 8 6 7 21 Unsorted Table Gold Silver Bronze ALGORITHM STEPS 1 Compare Gold Sort by gold count DESC 2 Compare Silver If gold tied, sort silver DESC 3 Compare Bronze If silver tied, sort bronze DESC 4 Alphabetical If all tied, sort by name A-Z Custom Comparator compare(a, b): if a.gold != b.gold: return b.gold - a.gold if a.silver != b.silver: return b.silver - a.silver if a.bronze != b.bronze: return b.bronze - a.bronze FINAL RESULT Rank Country G S B 1 USA 10 15 12 2 Germany 10 8 9 3 China 10 8 5 4 Australia 8 6 7 5 Japan 8 6 10 Sorted - OK Ranking Explained: USA: Most silver (15) among 10-gold countries Germany vs China: Same G,S Germany wins on bronze (9>5) Key Insight: Olympic ranking uses a lexicographic-style comparison with medals as tuple (Gold, Silver, Bronze). The custom comparator applies cascading tiebreakers: Gold --> Silver --> Bronze --> Name. Time Complexity: O(n log n) for sorting with O(1) per comparison. Space: O(n) for stable sort. TutorialsPoint - Sort the Olympic Table | Optimal Solution
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