forked from havanagrawal/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_574.sql
37 lines (36 loc) · 964 Bytes
/
_574.sql
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
--574. Winning Candidate
--Table: Candidate
--
--+-----+---------+
--| id | Name |
--+-----+---------+
--| 1 | A |
--| 2 | B |
--| 3 | C |
--| 4 | D |
--| 5 | E |
--+-----+---------+
--Table: Vote
--
--+-----+--------------+
--| id | CandidateId |
--+-----+--------------+
--| 1 | 2 |
--| 2 | 4 |
--| 3 | 3 |
--| 4 | 2 |
--| 5 | 5 |
--+-----+--------------+
--id is the auto-increment primary key,
--CandidateId is the id appeared in Candidate table.
--Write a sql to find the name of the winning candidate, the above example will return the winner B.
--
--+------+
--| Name |
--+------+
--| B |
--+------+
--Notes:
--You may assume there is no tie, in other words there will be at most one winning candidate.
select Name from Candidate as Name where id =
(select CandidateId from Vote group by CandidateId order by count(CandidateId) desc limit 1);