-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2377-SortTheOlympicTable.sql
More file actions
65 lines (61 loc) · 3.6 KB
/
2377-SortTheOlympicTable.sql
File metadata and controls
65 lines (61 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- 2377. Sort the Olympic Table
-- 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.
-- Create table If Not Exists Olympic (country varchar(50), gold_medals int, silver_medals int, bronze_medals int)
-- Truncate table Olympic
-- insert into Olympic (country, gold_medals, silver_medals, bronze_medals) values ('China', '10', '10', '20')
-- insert into Olympic (country, gold_medals, silver_medals, bronze_medals) values ('South Sudan', '0', '0', '1')
-- insert into Olympic (country, gold_medals, silver_medals, bronze_medals) values ('USA', '10', '10', '20')
-- insert into Olympic (country, gold_medals, silver_medals, bronze_medals) values ('Israel', '2', '2', '3')
-- insert into Olympic (country, gold_medals, silver_medals, bronze_medals) values ('Egypt', '2', '2', '2')
-- Write your MySQL query statement below
SELECT
*
FROM
Olympic
ORDER BY
gold_medals DESC, -- The country with more gold medals comes first.
silver_medals DESC, -- If there is a tie in the gold medals, the country with more silver medals comes first.
bronze_medals DESC, -- If there is a tie in the silver medals, the country with more bronze medals comes first.
country ASC -- If there is a tie in the bronze medals, the countries with the tie are sorted in ascending order lexicographically.