Winning Candidate


  • 0

    Click here to see the full article post


  • 0
    S

    Alternate solution can be below:

    select c.Name
    from Candidate c
    join Vote v
    on c.id=v.CandidateId
    group by v.CandidateId
    order by count(v.CandidateId) desc limit 1;


  • 0
    N

    @sumitac
    Actually they are not the same.


  • 0
    N

    @sumitac said in Winning Candidate:

    select c.Name
    from Candidate c
    join Vote v
    on c.id=v.CandidateId
    group by v.CandidateId
    order by count(v.CandidateId) desc limit 1;

    Try the following test case
    {"headers": {"Candidate": ["id", "Name"], "Vote": ["id", "CandidateId"]}, "rows": {"Candidate": [ [3, "C"]], "Vote": [[1, 2],[2, 4],[3, 3],[4, 2],[5, 5]]}}
    you got the different answers.


  • 0
    R
    SELECT
        name 
    FROM 
        candidate
    WHERE 
        id in 
            (SELECT
                winner.candidateid 
            FROM (
                SELECT 
                    candidateid,
                    count(candidateid) cnt 
                FROM 
                    vote 
                GROUP BY
                    candidateid 
                ORDER BY 
                    cnt DESC
                limit 1) as winner)
    

  • 1
    SELECT 
      name
    FROM
      Candidate c,
    ( SELECT
        CandidateId, COUNT(*)
      FROM
        Vote
      GROUP BY CandidateId
      ORDER BY COUNT(*) DESC
      LIMIT 1 ) AS winner
    WHERE
      c.id = winner.CandidateId
    ;
    

  • 0
    select name
    from candidate
    where id = (
        select candidateid
        from vote
        group by candidateid
        order by count(*) desc
        limit 1
    )
    

  • 0
    J

    my accept solution. However, if I change = to in, it's wrong. Anyone can help me? Thank you

    select Name
    from Candidate
    where id = (select CandidateId
    from Vote
    group by CandidateId
    order by count(*) desc
    limit 1
    )


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.