Table: Olympic
+---------------+---------+ | Column Name | Type | +---------------+---------+ | country | varchar | | gold_medals | int | | silver_medals | int | | bronze_medals | int | +---------------+---------+ In SQL, country is the primary key for this table. Each row in this table shows a country name and the number of gold, silver, and bronze medals it won in the Olympic games.
The Olympic table is sorted according to the following rules:
Write a solution to sort the Olympic table.
The result format is shown in the following example.
Example 1:
Input: Olympic 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 | +-------------+-------------+---------------+---------------+ 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 | +-------------+-------------+---------------+---------------+ Explanation: The tie between China and USA is broken by their lexicographical names. Since "China" is lexicographically smaller than "USA", it comes first. Israel comes before Egypt because it has more bronze medals.
Problem Overview: You are given an Olympic medal table containing each team's gold, silver, and bronze medal counts. The task is to return the teams ranked by medal priority: highest gold first, then silver, then bronze. If two teams have identical medal counts, sort them alphabetically by team name.
Approach 1: Multi-Column ORDER BY Sorting (O(n log n) time, O(1) space)
This problem is a straightforward SQL sorting task. You retrieve all rows from the table and apply an ORDER BY clause with multiple columns to enforce Olympic ranking rules. SQL evaluates sorting conditions sequentially: first by gold_medals DESC, then silver_medals DESC, then bronze_medals DESC. If multiple teams still tie after these comparisons, the final tie-breaker sorts alphabetically by team ASC.
The key insight is that SQL already supports hierarchical sorting. Each column in the ORDER BY list acts as a tie-breaker for the previous one. This avoids any need for manual ranking logic, temporary tables, or window functions. The database engine internally performs a sort operation based on the specified column precedence.
From a complexity standpoint, database sorting typically runs in O(n log n) time where n is the number of teams, since the rows must be ordered. The space complexity is O(1) from the query perspective because you are not creating additional structures—only instructing the database to sort the result set.
This pattern appears frequently in database and SQL interview questions where ranking or leaderboard-style ordering is required. Understanding how ORDER BY evaluates multiple columns is essential for solving leaderboard, scoring, or priority-based ranking problems. The same concept is widely used in analytics dashboards, competition rankings, and sports statistics systems.
In practice, the query simply selects the relevant columns and applies the correct priority ordering. The database engine handles the rest efficiently. Because the problem requires no aggregation, filtering, or joins, the solution remains concise and performs well even with large tables.
Recommended for interviews: Interviewers expect the multi-column ORDER BY approach. It demonstrates understanding of SQL sorting precedence and mirrors how real production leaderboards are implemented. More complex constructs like window functions are unnecessary here and would add overhead without benefit.
MySQL
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Multi-Column ORDER BY | O(n log n) | O(1) | Standard SQL ranking problems where rows must be sorted by multiple priority columns |
| Application-Level Sorting | O(n log n) | O(n) | When raw data is fetched first and sorting must be performed in application code instead of SQL |
LeetCode 2377 "Sort the Olympic Table" Interview SQL Question with Detailed Explanation • Everyday Data Science • 1,347 views views
Watch 1 more video solutions →Practice Sort the Olympic Table with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor