☺☺☺
题号 | 知识点 |
---|---|
1 | GROUP BY 分组;COUNT() 统计个数;HAVING 对GROUP BY 后的数据进行限制 |
2 | DISTINCT 去重 |
3 | % 求余;!= 不等于 |
4 | AVG()求平均值 |
5 | limit x, y 分句表示: 跳过 x 条数据,读取 y 条数据 |
6 | MAX() 嵌套 |
从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
CREATE TABLE IF NOT EXISTS “titles” (
emp_no
int(11) NOT NULL,
title
varchar(50) NOT NULL,
from_date
date NOT NULL,
to_date
date DEFAULT NULL);
GROUP BY
对title进行分组,COUNT()
统计分组后每组的个数,HAVING
来对每组个数进行限制SELECT title, COUNT(title) AS num
FROM titles
GROUP BY title
HAVING num>=2
从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t。
注意对于重复的emp_no进行忽略(即emp_no重复的title不计算,title对应的数目t不增加)。
CREATE TABLE IF NOT EXISTStitles
(
emp_no
int(11) NOT NULL,
title
varchar(50) NOT NULL,
from_date
date NOT NULL,
to_date
date DEFAULT NULL);
DISTINCT
对emp_no值去重,COUNT()
统计出GROUP BY
聚合之后每组的不重复emp_no值。SELECT title, COUNT(DISTINCT emp_no) AS t
FROM titles
GROUP BY title
HAVING t >= 2
查找employees表所有emp_no为奇数,且last_name不为Mary(注意大小写)的员工信息,并按照hire_date逆序排列(题目不能使用mod函数)
CREATE TABLEemployees
(
emp_no
int(11) NOT NULL,
birth_date
date NOT NULL,
first_name
varchar(14) NOT NULL,
last_name
varchar(16) NOT NULL,
gender
char(1) NOT NULL,
hire_date
date NOT NULL,
PRIMARY KEY (emp_no
));
!=
(;д;)SELECT *
FROM employees
WHERE emp_no%2=1
AND last_name != 'Mary'
ORDER BY hire_date
DESC
统计出当前(titles.to_date=‘9999-01-01’)各个title类型对应的员工当前(salaries.to_date=‘9999-01-01’)薪水对应的平均工资。结果给出title以及平均工资avg。
CREATE TABLEsalaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
CREATE TABLE IF NOT EXISTS “titles” (
emp_no
int(11) NOT NULL,
title
varchar(50) NOT NULL,
from_date
date NOT NULL,
to_date
date DEFAULT NULL);
avg
求平均工资,where将量表链接对应。SELECT b.title, AVG(a.salary)
FROM salaries a, titles b
WHERE b.to_date='9999-01-01'
AND a.to_date='9999-01-01'
AND a.emp_no = b.emp_no
GROUP BY b.title
获取当前(to_date=‘9999-01-01’)薪水第二多的员工的emp_no以及其对应的薪水salary
CREATE TABLEsalaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
LIMIT x,y
去除第二员工的薪水。SELECT emp_no, salary
FROM salaries
WHERE to_date='9999-01-01'
ORDER BY salary
DESC
LIMIT 1,1
查找当前薪水(to_date=‘9999-01-01’)排名第二多的员工编号emp_no、薪水salary、last_name以及first_name,你可以不使用order by完成吗
CREATE TABLEemployees
(
emp_no
int(11) NOT NULL,
birth_date
date NOT NULL,
first_name
varchar(14) NOT NULL,
last_name
varchar(16) NOT NULL,
gender
char(1) NOT NULL,
hire_date
date NOT NULL,
PRIMARY KEY (emp_no
));
CREATE TABLEsalaries
(
emp_no
int(11) NOT NULL,
salary
int(11) NOT NULL,
from_date
date NOT NULL,
to_date
date NOT NULL,
PRIMARY KEY (emp_no
,from_date
));
MAX()
嵌套。还有一点注意:若工资相同要去重,但本题的方法刚好完美的避开了这个需要驱虫的过程,直接将重复值全部过滤掉了。
SELECT a.emp_no, MAX(b.salary), a.last_name, a.first_name
FROM employees a, salaries b
WHERE b.salary<(SELECT MAX(salary) FROM salaries)
AND b.to_date="9999-01-01"
AND a.emp_no = b.emp_no
或:
SELECT e.emp_no, MAX(s.salary) AS salary, e.last_name, e.first_name
FROM employees AS e INNER JOIN salaries AS s
ON e.emp_no = s.emp_no
WHERE s.to_date = '9999-01-01'
AND s.salary NOT IN (SELECT MAX(salary) FROM salaries WHERE to_date = '9999-01-01')