I was given this MySQL Interview Question, which made me disqualified for the job.
I went to interview and was asked a question which I was not able to reply and I lost the job.
They asked.
We have two tables, the first table (master table) is CANDIDATES with fields:
candidate_id(primary_key)candidate_name
The Second table (child table) is CANDIDATE_VOTES with fields:
v_id(primary key)candidate_id(foreign key)
Every time a vote is given the candidate’s candidate_key is put in the child.
CANDIDATE:
=================================
|candidate_id | candidate_Name |
|-------------------------------|
| 1 | abc |
|-------------------------------|
| 2 | xyz |
|-------------------------------|
| 3 | etc |
|-------------------------------|
CANDIDATE VOTES
==========================
| votes_id | candidate_id |
|-------------------------|
| 1 | 1 |
|-------------------------|
| 2 | 1 |
|-------------------------|
| 3 | 2 |
|-------------------------|
The Question was how would you declare a winner?
Please help me how to do it.
I tried a lot but could not find the logic.
You should return all candidates who have the most number of votes:
See it on sqlfiddle.