返回介绍

solution / 2300-2399 / 2377.Sort the Olympic Table / README_EN

发布于 2024-06-17 01:03:07 字数 2932 浏览 0 评论 0 收藏 0

2377. Sort the Olympic Table

中文文档

Description

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.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT *
FROM Olympic
ORDER BY 2 DESC, 3 DESC, 4 DESC, 1;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文