avatar
请教SQL面试题# JobHunting - 待字闺中
o*b
1
我自己感觉我做对了 但是面我的烙印非说不对 我把我的答案贴在这里
Given two tables
1. Candidate: Id , Name
2. Vote: Id, CandidateId(this is the id being voted)
Give query to give the name of the winning candidate
我的答案 因为也没有指定具体那个SQL 所以我认为把logic回答出了就可以了
select Name, Votes from (
select c.Name as Name, count(v.Id) as Votes
from Candidate c join Vote v on c.Id = v.CandidateId
group by c.Name
order by COUNT(v.Id) desc )
limit 1
avatar
h*3
2
我试着写了一个,用的是 implicit join
select Name from Candidate c,
(select CandidateId from votes order by count(CandidateId) desc limit 1 )
votes
where c.Id = votes.CandidateId.
会不会看起来更加清楚些。

【在 o*********b 的大作中提到】
: 我自己感觉我做对了 但是面我的烙印非说不对 我把我的答案贴在这里
: Given two tables
: 1. Candidate: Id , Name
: 2. Vote: Id, CandidateId(this is the id being voted)
: Give query to give the name of the winning candidate
: 我的答案 因为也没有指定具体那个SQL 所以我认为把logic回答出了就可以了
: select Name, Votes from (
: select c.Name as Name, count(v.Id) as Votes
: from Candidate c join Vote v on c.Id = v.CandidateId
: group by c.Name

avatar
o*b
3
请问
select CandidateId from votes order by count(CandidateId) desc
count(CandidateId)什么意思? 难道是产生 每个CandidateId的频率统计? 我的理
解是 count(CandidateId)产生CandidateId到底有多少个distinct value?

【在 h*******3 的大作中提到】
: 我试着写了一个,用的是 implicit join
: select Name from Candidate c,
: (select CandidateId from votes order by count(CandidateId) desc limit 1 )
: votes
: where c.Id = votes.CandidateId.
: 会不会看起来更加清楚些。

avatar
h*3
4
有个地方需要 clarify 一下,就是 votes 表中一个Id对应多个 CandidateId ,
语法应该是?
select count(id) IDs, CandidateId from votes group by CandidateId order by
IDs desc

【在 o*********b 的大作中提到】
: 请问
: select CandidateId from votes order by count(CandidateId) desc
: count(CandidateId)什么意思? 难道是产生 每个CandidateId的频率统计? 我的理
: 解是 count(CandidateId)产生CandidateId到底有多少个distinct value?

avatar
o*b
5
你刚开始的那个答案应该是不对的吧?
但是我的答案没有错误啊

【在 h*******3 的大作中提到】
: 有个地方需要 clarify 一下,就是 votes 表中一个Id对应多个 CandidateId ,
: 语法应该是?
: select count(id) IDs, CandidateId from votes group by CandidateId order by
: IDs desc

avatar
b*u
6
会不会是因为存在并列第一?
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。