背景问题:
准备数据:
create table graduates(
name varchar(50),
income float
);
insert into graduates(name, income)
values('桑普森', 40000),
('迈克', 30000),
('怀特', 20000),
('阿诺德', 20000),
('史密斯', 20000),
('劳思伦', 15000),
('哈德逊', 15000),
('肯特', 10000),
('贝克', 10000),
('斯科特', 10000);
/*
name | income
--------+--------
桑普森 | 40000
迈克 | 30000
怀特 | 20000
阿诺德 | 20000
史密斯 | 20000
劳思伦 | 15000
哈德逊 | 15000
肯特 | 10000
贝克 | 10000
斯科特 | 10000
*/
问题: 查找出现最多的几个工资, 也就是工资的众数.
解法:
-- 使用限定谓词all
select income
from graduates
group by income
having count(income) >= all(select count(income)
from graduates
group by income);
-- 使用极值函数max
select income
from graduates
group by income
having count(income) >= (select max(cnt)
from (select count(income) as cnt
from graduates
group by income) as tmp);
解释: group by处理集合的方式