Skip to main content

LeetCode 574. Winning Candidate SQL Solution

Problem

LeetCode SQL Problem

  1. Winning Candidate

Candidate table

idName
1A
2B
3C
4D
5E

Vote table

idCandidateId
12
24
33
42
55

Solution

Winning Candidate
-- Find the name of the winning candidate
-- Use LIMIT clause as we're told there will be no tie
SELECT C.Name
FROM Vote AS V
INNER JOIN Candidate AS C ON V.CandidateId = C.id
GROUP BY V.CandidateId
,C.Name
ORDER BY count(*) DESC
LIMIT 1

Query Output

Name
B