首发于公众号:小肖学数据分析
描述
请你查找薪水记录超过15条的员工号emp_no以及其对应的记录次数t,例子输出如下:
emp_no | t |
---|---|
10001 | 16 |
测试样例
CREATE TABLE salaries (
emp_no INT,
salary INT,
from_date DATE,
to_date DATE
);
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 60117, '1986-06-26', '1987-06-26');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 62102, '1987-06-26', '1988-06-25');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 66074, '1988-06-25', '1989-06-25');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 66596, '1989-06-25', '1990-06-25');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 66961, '1990-06-25', '1991-06-25');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 71046, '1991-06-25', '1992-06-24');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 74333, '1992-06-24', '1993-06-24');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 75286, '1993-06-24', '1994-06-24');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 75994, '1994-06-24', '1995-06-24');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 76884, '1995-06-24', '1996-06-23');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 80013, '1996-06-23', '1997-06-23');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 81025, '1997-06-23', '1998-06-23');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 81097, '1998-06-23', '1999-06-23');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 84917, '1999-06-23', '2000-06-22');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 85112, '2000-06-22', '2001-06-22');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10001, 85097, '2001-06-22', '2002-06-22');
INSERT INTO salaries (emp_no, salary, from_date, to_date) VALUES (10002, 72527, '1996-08-03', '1997-08-03');
-- 利用 group by having 实现
select emp_no,count(1) as t from salaries
group by emp_no
having count(1)>15
GROUP BY和HAVING是一起使用的,用于在查询结果中对分组后的数据进行过滤。
GROUP BY语句用于将结果集按照一个或多个列进行分组,然后对每个分组进行聚合计算。
HAVING语句用于在GROUP BY的结果集中对分组后的数据进行过滤,只返回满足条件的分组。
语法:
SELECT 列1, 列2, ... 列n
FROM 表名
GROUP BY 列1, 列2, ... 列n
HAVING 条件;