#1:where能不能使用聚合函数进行过滤
#2查询员工工资最大值,最小值,平均值,总和
SELECT MAX(salary),MIN(salary),AVG(salary),
SUM(salary)
FROM employees;
#3
SELECT job_id,MAX(salary),MIN(salary),AVG(salary),SUM(salary)
FROM employees
GROUP BY job_id;
#4选择具有各个job——id的员工人数
SELECT job_id,COUNT(*)
FROM employees
GROUP BY job_id;
#5查询员工最高工资与最低工资的差距(diffrence)
SELECT MAX(salary),MIN(salary),MAX(salary)-MIN(salary) chaju
FROM employees;
#6查询各个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
SELECT manager_id,MIN(salary)
FROM employees
WHERE manager_id is not null
GROUP BY manager_id
HAVING MIN(salary) >= 6000;
#查询所有部门的名字,location_id,员工数量和平均工资
#并按平均工资降序
SELECT department_name,location_id,count(*),
AVG(salary)
FROM employees e RIGHT join departments d
on e.department_id = d.department_id
GROUP BY department_name,location_id
ORDER BY AVG(salary) DESC;
SELECT employee_id,salary,department_name,
location_id
FROM employees e RIGHT JOIN departments d
on e.department_id =d.department_id;
#第8章 子查询
#需求: 谁的工资比Abel高
#方式1
SELECT salary
FROM employees
WHERE last_name = 'Abel';
SELECT last_name,salary
FROM employees
WHERE salary > 11000;
#方式2:自连接
SELECT e2.last_name,e2.salary
FROM employees e1,employees e2
WHERE e2.salary > e1.salary#多表的查询
AND e1.last_name='Abel';
#方式3 子查询
SELECT last_name,salary
FROM employees
WHERE salary > (SELECT salary
FROM employees
WHERE last_name = 'Abel'
);
#称谓的规范:外查询(主查询),内查询(子查询)
/*
子查询(内查询)在主查询(外查询之前一次执行完成)
子查询的结果被主查询(外查询)使用
注意事项
子查询要包括在括号内
将子查询放在比较条件的右侧
单行操作符对应当行子查询,多行操作符对应多行子查询
*/
SELECT last_name,salary
FROM employees
WHERE (SELECT salary
FROM employees
WHERE last_name = 'Abel'
) < salary;#头重脚轻
#子查询的分类
#角度1:从内查询返回的结果条数目
#单行子查询 VS 多行子查询(工资大与11000的姓名)
#角度2:查询是否被执行多次
#相关子查询vs不相关子查询
#举例
#相关子查询:查询工资大于本部门平均工资的员工信息
#不相关子查询:查询工资大于本公司平均工资的员工信息