File tree Expand file tree Collapse file tree 1 file changed +0
-35
lines changed Expand file tree Collapse file tree 1 file changed +0
-35
lines changed Original file line number Diff line number Diff line change 1- -- 574. Winning Candidate
2- -- Table: Candidate
3- --
4- -- +-----+---------+
5- -- | id | Name |
6- -- +-----+---------+
7- -- | 1 | A |
8- -- | 2 | B |
9- -- | 3 | C |
10- -- | 4 | D |
11- -- | 5 | E |
12- -- +-----+---------+
13- -- Table: Vote
14- --
15- -- +-----+--------------+
16- -- | id | CandidateId |
17- -- +-----+--------------+
18- -- | 1 | 2 |
19- -- | 2 | 4 |
20- -- | 3 | 3 |
21- -- | 4 | 2 |
22- -- | 5 | 5 |
23- -- +-----+--------------+
24- -- id is the auto-increment primary key,
25- -- CandidateId is the id appeared in Candidate table.
26- -- Write a sql to find the name of the winning candidate, the above example will return the winner B.
27- --
28- -- +------+
29- -- | Name |
30- -- +------+
31- -- | B |
32- -- +------+
33- -- Notes:
34- -- You may assume there is no tie, in other words there will be at most one winning candidate.
35-
361select Name from Candidate as Name where id =
372(select CandidateId from Vote group by CandidateId order by count (CandidateId) desc limit 1 );
You can’t perform that action at this time.
0 commit comments