Winning Candidate - Problem

You're organizing a digital election system for a company! ๐Ÿ—ณ๏ธ You have two important tables:

Candidate Table: Contains information about each candidate running for office.
Vote Table: Records every single vote cast in the election.

Your mission is to determine which candidate received the most votes and won the election! ๐Ÿ†

Table: Candidate

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| id          | int      |
| name        | varchar  |
+-------------+----------+

Table: Vote

+-------------+------+
| Column Name | Type |
+-------------+------+
| id          | int  |
| candidateId | int  |
+-------------+------+

Goal: Write a SQL query that returns the name of the winning candidate (the one with the highest vote count).

Note: The test cases guarantee that exactly one candidate wins - no ties to worry about!

Input & Output

example_1.sql โ€” Basic Election
$ Input: Candidate table: +----+-------+ | id | name | +----+-------+ | 1 | A | | 2 | B | +----+-------+ Vote table: +----+-------------+ | id | candidateId | +----+-------------+ | 1 | 2 | | 2 | 4 | | 3 | 3 | | 4 | 2 | | 5 | 5 | +----+-------------+
โ€บ Output: +------+ | name | +------+ | B | +------+
๐Ÿ’ก Note: Candidate B received 2 votes (vote ids 1 and 4), while candidate A received 1 vote (vote id 2). B wins with the most votes.
example_2.sql โ€” Clear Winner
$ Input: Candidate table: +----+--------+ | id | name | +----+--------+ | 1 | Alice | | 2 | Bob | | 3 | Carol | +----+--------+ Vote table: +----+-------------+ | id | candidateId | +----+-------------+ | 1 | 1 | | 2 | 2 | | 3 | 1 | | 4 | 1 | | 5 | 3 | | 6 | 2 | +----+-------------+
โ€บ Output: +-------+ | name | +-------+ | Alice | +-------+
๐Ÿ’ก Note: Vote count: Alice = 3 votes (ids 1, 3, 4), Bob = 2 votes (ids 2, 6), Carol = 1 vote (id 5). Alice wins with 3 votes.
example_3.sql โ€” Single Candidate
$ Input: Candidate table: +----+------+ | id | name | +----+------+ | 1 | John | +----+------+ Vote table: +----+-------------+ | id | candidateId | +----+-------------+ | 1 | 1 | | 2 | 1 | | 3 | 1 | +----+-------------+
โ€บ Output: +------+ | name | +------+ | John | +------+
๐Ÿ’ก Note: Only one candidate John running, and he received all 3 votes. John wins by default.

Visualization

Tap to expand
๐Ÿ—ณ๏ธ Election Vote Counting Process๐Ÿ“ฅ Ballot BoxVote 1: AliceVote 2: BobVote 3: AliceVote 4: Alice๐Ÿ‘ฉ Alice Pile3 votes๐Ÿ‘จ Bob Pile2 votes๐Ÿ† WinnerAliceMost votes: 3SQL Query Explanation:1. JOIN Vote and Candidate tables to get candidate names2. GROUP BY candidate name to create vote piles3. COUNT(*) to count votes in each pile4. ORDER BY COUNT(*) DESC LIMIT 1 to get the winnerSELECT c.name FROM Vote v JOIN Candidate c ON v.candidateId = c.id GROUP BY c.name ORDER BY COUNT(*) DESC LIMIT 1Time Complexity: O(n log k) where n = number of votes, k = number of candidates
Understanding the Visualization
1
Collect All Votes
Gather all voting slips from the ballot box
2
Sort by Candidate
Group all votes by the candidate they're voting for
3
Count Each Pile
Count how many votes each candidate received
4
Find the Winner
The candidate with the most votes wins the election
Key Takeaway
๐ŸŽฏ Key Insight: Use GROUP BY to efficiently aggregate votes per candidate, then ORDER BY COUNT(*) DESC LIMIT 1 to find the winner in a single optimized query!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Dominated by the sorting operation for ORDER BY, where n is the number of candidates

n
2n
โšก Linearithmic
Space Complexity
O(k)

Space for grouping by k candidates

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค candidates โ‰ค 1000
  • 1 โ‰ค votes โ‰ค 105
  • Exactly one candidate will have the maximum votes (no ties)
  • All votes reference valid candidate IDs
  • There will be at least one vote cast
Asked in
Google 28 Amazon 22 Microsoft 18 Meta 15
52.8K Views
High Frequency
~12 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen