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
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
โก Linearithmic
Space Complexity
O(k)
Space for grouping by k candidates
โ 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code