LeetCode 2377 - Sort the Olympic Table
This problem asks us to return the rows of the Olympic table in a very specific sorted order. Each row represents a country and the number of gold, silver, and bronze medals that country won in the Olympic games.
Difficulty: 🟢 Easy
Topics: Database
Solution
Problem Understanding
This problem asks us to return the rows of the Olympic table in a very specific sorted order. Each row represents a country and the number of gold, silver, and bronze medals that country won in the Olympic games.
The table contains four columns:
country, the country namegold_medals, the number of gold medalssilver_medals, the number of silver medalsbronze_medals, the number of bronze medals
The sorting rules are hierarchical. That means we compare countries using one field at a time in priority order.
The required ordering is:
- Countries with more gold medals should appear first.
- If two countries have the same number of gold medals, compare silver medals.
- If gold and silver medals are tied, compare bronze medals.
- If all medal counts are tied, sort alphabetically by country name in ascending lexicographical order.
The expected output is simply the same table rows reordered according to these rules.
The important observation is that this is purely a sorting problem. We are not calculating aggregates, filtering rows, or joining tables. We only need to apply a multi-column ordering strategy.
The constraints are small because this is an Easy SQL problem, but even without explicit limits, sorting database rows is efficient and well supported by SQL engines. The database guarantees that country is the primary key, so country names are unique. This means the final lexicographical tie breaker is always deterministic.
Several edge cases are important:
- Multiple countries may have identical medal counts.
- Some countries may have zero medals.
- Countries may tie in gold and silver but differ in bronze.
- Countries may tie in all medal counts, requiring alphabetical ordering.
- A naive implementation that only sorts by gold medals would produce incorrect ordering for ties.
Approaches
Brute Force Approach
A brute force approach would manually compare every pair of countries and repeatedly reorder them according to the medal rules. Conceptually, this resembles implementing a custom sorting algorithm from scratch.
For each comparison:
- Compare gold medals first.
- If tied, compare silver medals.
- If still tied, compare bronze medals.
- If still tied, compare country names alphabetically.
This approach works because every pair of rows can be consistently ordered according to the problem rules. However, implementing manual sorting logic is unnecessary because SQL databases already provide optimized sorting functionality through the ORDER BY clause.
A naive manual comparison solution would also be more verbose and harder to maintain.
Optimal Approach
The key insight is that SQL already supports multi-column sorting directly.
We can use ORDER BY with multiple fields in priority order:
gold_medals DESCsilver_medals DESCbronze_medals DESCcountry ASC
Descending order is used for medal counts because larger values should appear first. Ascending order is used for country names because lexicographically smaller names should come first.
This produces exactly the ordering required by the problem statement in a concise and efficient way.
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Brute Force | O(n log n) | O(1) or O(n) | Manual custom sorting implementation |
| Optimal | O(n log n) | O(1) | Uses SQL multi-column ORDER BY |
Algorithm Walkthrough
- Start with all rows from the
Olympictable. - Sort the rows by
gold_medalsin descending order because countries with more gold medals must appear first. - For countries tied in gold medals, sort by
silver_medalsin descending order. This ensures the second ranking rule is enforced correctly. - For countries still tied after gold and silver comparisons, sort by
bronze_medalsin descending order. - If all medal counts are equal, sort by
countryin ascending lexicographical order to break the final tie deterministically. - Return the sorted rows.
Why it works
SQL applies ORDER BY conditions sequentially. The database first sorts by the highest priority column. Whenever rows tie on that column, the next column is used to break the tie. Because the ordering conditions exactly match the rules described in the problem statement, the final output is guaranteed to be correct.
Python Solution
# Write your MySQL query statement below
SELECT
country,
gold_medals,
silver_medals,
bronze_medals
FROM Olympic
ORDER BY
gold_medals DESC,
silver_medals DESC,
bronze_medals DESC,
country ASC;
This solution directly mirrors the problem requirements.
The SELECT statement retrieves all required columns from the Olympic table.
The ORDER BY clause implements the ranking logic:
gold_medals DESCprioritizes countries with more gold medals.silver_medals DESCbreaks ties among equal gold medal counts.bronze_medals DESCresolves remaining ties.country ASCprovides lexicographical ordering when medal counts are identical.
Because SQL sorting is stable and hierarchical, this ordering exactly matches the Olympic ranking rules.
Go Solution
// There is no Go solution for this problem because LeetCode 2377
// is a Database problem that requires writing an SQL query only.
Unlike algorithmic LeetCode problems, Database problems are solved entirely using SQL queries. Therefore, no executable Go implementation is required or expected.
Worked Examples
Example 1
Input table:
| country | gold_medals | silver_medals | bronze_medals |
|---|---|---|---|
| China | 10 | 10 | 20 |
| South Sudan | 0 | 0 | 1 |
| USA | 10 | 10 | 20 |
| Israel | 2 | 2 | 3 |
| Egypt | 2 | 2 | 2 |
Step 1: Sort by Gold Medals Descending
| country | gold |
|---|---|
| China | 10 |
| USA | 10 |
| Israel | 2 |
| Egypt | 2 |
| South Sudan | 0 |
China and USA remain tied.
Israel and Egypt remain tied.
Step 2: Sort Ties by Silver Medals Descending
China and USA both have 10 silver medals.
Israel and Egypt both have 2 silver medals.
No ordering changes occur.
Step 3: Sort Remaining Ties by Bronze Medals Descending
| country | bronze |
|---|---|
| China | 20 |
| USA | 20 |
| Israel | 3 |
| Egypt | 2 |
| South Sudan | 1 |
Israel now correctly comes before Egypt because it has more bronze medals.
Step 4: Sort Final Ties Lexicographically
China and USA are still tied on all medal counts.
Alphabetically:
"China"comes before"USA"
Final output:
| country | gold_medals | silver_medals | bronze_medals |
|---|---|---|---|
| China | 10 | 10 | 20 |
| USA | 10 | 10 | 20 |
| Israel | 2 | 2 | 3 |
| Egypt | 2 | 2 | 2 |
| South Sudan | 0 | 0 | 1 |
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(n log n) | Sorting the rows dominates the runtime |
| Space | O(1) | SQL sorting uses database-managed internal memory |
The primary cost comes from sorting the rows. Comparison-based sorting requires O(n log n) time in general. The query itself is concise because SQL databases internally optimize sorting operations very efficiently.
Test Cases
# These are conceptual SQL test scenarios.
# Test 1: Provided example
# Validates all ordering rules together.
# Input:
# China 10 10 20
# USA 10 10 20
# Israel 2 2 3
# Egypt 2 2 2
# South Sudan 0 0 1
# Expected:
# China
# USA
# Israel
# Egypt
# South Sudan
# Test 2: Gold medal ordering
# Country with more gold medals should come first.
# Input:
# A 5 0 0
# B 3 100 100
# Expected:
# A
# B
# Test 3: Silver medal tie breaker
# Same gold medals, higher silver medals wins.
# Input:
# A 5 10 0
# B 5 8 100
# Expected:
# A
# B
# Test 4: Bronze medal tie breaker
# Same gold and silver, higher bronze wins.
# Input:
# A 5 5 7
# B 5 5 6
# Expected:
# A
# B
# Test 5: Lexicographical ordering
# Same medal counts, alphabetical order decides.
# Input:
# Canada 1 1 1
# Brazil 1 1 1
# Expected:
# Brazil
# Canada
# Test 6: Zero medal countries
# Ensures zeros are handled correctly.
# Input:
# A 0 0 0
# B 0 0 1
# Expected:
# B
# A
| Test | Why |
|---|---|
| Provided example | Validates the full ranking logic |
| Different gold medals | Ensures primary ordering works |
| Silver medal tie breaker | Verifies secondary sorting |
| Bronze medal tie breaker | Verifies tertiary sorting |
| Lexicographical tie | Confirms alphabetical ordering |
| Zero medal countries | Ensures small values are handled correctly |
Edge Cases
One important edge case occurs when two countries have exactly the same medal counts. A buggy solution might leave their ordering undefined or rely on database insertion order. The problem explicitly requires lexicographical ordering by country name in this situation. The implementation handles this using country ASC as the final sorting condition.
Another important case involves countries with zero medals. Some incorrect solutions may accidentally exclude or mishandle rows with zeros. Since the query simply sorts all rows numerically, zero values are processed naturally and correctly appear below countries with higher medal counts.
A third edge case appears when countries tie on gold medals but differ on silver or bronze medals. A naive solution that only sorts by gold medals would fail here. The multi-column ORDER BY clause correctly processes ties step by step, ensuring that silver and bronze medals are used exactly as required.