Skip to main content

Sort the Olympic Table - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase4 min read
Practice this problem

Problem Statement

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:

  • The country with more gold medals comes first.
  • If there is a tie in the gold medals, the country with more silver medals comes first.
  • If there is a tie in the silver medals, the country with more bronze medals comes first.
  • If there is a tie in the bronze medals, the countries with the tie are sorted in ascending order lexicographically.

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.

Approach Overview

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.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Multi-Column ORDER BYO(n log n)O(1)Standard SQL ranking problems where rows must be sorted by multiple priority columns
Application-Level SortingO(n log n)O(n)When raw data is fetched first and sorting must be performed in application code instead of SQL

Video Solution

LeetCode 2377 "Sort the Olympic Table" Interview SQL Question with Detailed Explanation • Everyday Data Science • 1,401 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Sort the Olympic Table easy or hard?
Sort the Olympic Table is classified as an Easy database problem. It mainly tests understanding of SQL ORDER BY with multiple columns and tie-breaking conditions rather than complex joins, aggregations, or window functions.
Sort the Olympic Table Python/Java solution
On platforms like LeetCode, this problem is solved using SQL rather than Python or Java. The query selects all teams and sorts them using ORDER BY gold_medals DESC, silver_medals DESC, bronze_medals DESC, and team ASC.
How to solve Sort the Olympic Table in O(n)?
Achieving true O(n) sorting is generally not possible because ranking requires ordering the rows. SQL engines typically use comparison-based sorting which runs in O(n log n). The optimal solution is a multi-column ORDER BY clause that leverages the database's built-in sorting mechanism.
What is the best approach for Sort the Olympic Table?
The best approach uses a multi-column SQL ORDER BY clause. Sort by gold medals descending, then silver descending, then bronze descending, and finally by team name ascending to break ties. This directly matches Olympic ranking rules and runs in O(n log n) time due to sorting.
Is Sort the Olympic Table asked at Google/Amazon/Meta?
Database ranking and leaderboard-style SQL problems are commonly asked in data engineering and backend interviews at companies like Amazon, Google, and Meta. While the exact problem may vary, the concept of multi-column sorting with ORDER BY appears frequently.
What data structure is used in Sort the Olympic Table?
The solution primarily relies on SQL sorting performed by the database engine rather than explicit data structures. Internally the database may use sorting algorithms such as quicksort or mergesort to order rows based on the specified columns.
What is the time complexity of Sort the Olympic Table?
The time complexity is O(n log n) because the database must sort the rows according to multiple columns. Space complexity is effectively O(1) at the query level since no additional data structures are created by the SQL query.

Ready to solve this problem?

Practice Sort the Olympic Table with our built-in code editor and test cases.

Practice on FleetCode