2.在FROM 子句中使用子查询
2.返回比本部门平均工资高的员工的 last_name, department_id, salary 及平均工资
[方式一]
- select last_name,department_id,salary,
- (select avg(salary)
- from employees e3
- where e1.department_id = e3.department_id
- groupby department_id ) avg_salary
- from employees e1
- where salary >(select avg(salary)
- from employees e2
- where e1.department_id = e2.department_id
- group by department_id )
复制代码[方式二]
- SELECT a.last_name,a.salary, a.department_id, b.salavg
- FROM employees a, (SELECT department_id,AVG(salary) salavg
- FROM employees
- GROUP BYdepartment_id) b
- WHERE a.department_id =b.department_id
- AND a.salary> b.salavg
复制代码 3.单列子查询表达式 • Oracle8i只在下列情况下可以使用, 例如:
– SELECT 语句 (FROM 和 WHERE 子句)
– INSERT 语句中的 VALUES 列表中
• Oracle9i中单列子查询表达式可在下列情况下使用:
– DECODE 和 CASE
– SELECT 中除 GROUP BY 子句以外的所有子句中
3. 显示员工的employee_id,last_name 和location。其中,若员工department_id 与 location_id 为1800 的 department_id 相同,则location 为’Canada’,其余则为’USA’。
- SELECT employee_id, last_name,
- ( CASE department_id
- WHEN ( SELECT department_id FROM departments
- WHERE location_id = 1800)
- THEN 'Canada' ELSE 'USA' END) location
- FROM employees;
复制代码
4. 查 询 员 工 的 employee_id,last_name, 要 求 按 照 员 工 的department_name 排序
- SELECT employee_id,last_name
- FROM employeese
- ORDER BY (SELECT department_name
- FROM departments d
- WHERE e.department_id = d.department_id);
复制代码 4.相关子查询5.查询员工中工资大于本部门平均工资的员工的 last_name,salary 和其 department_id
- SELECT last_name, salary, department_id
- FROM employees outer
- WHERE salary > (SELECT AVG(salary)
- FROM employees
- WHERE department_id=outer.department_id);
复制代码6.若 employees 表中employee_id 与 job_history 表中employee_id相同的数目不小于 2,输出这些相同 id 的员工的employee_id,last_name和其 job_id
- SELECT e.employee_id, last_name,e.job_id
- FROM employees e
- WHERE 2<= (SELECT COUNT(*)
- FROM job_history
- WHERE employee_id =e.employee_id);
复制代码 5.EXISTS 操作符 • EXISTS 操作符检查在子查询中是否存在满足条件的行
• 如果在子查询中存在满足条件的行:
– 不在子查询中继续查找
– 条件返回 TRUE
7. 查询公司管理者的employee_id,last_name,job_id,department_id 信息
- SELECT employee_id, last_name, job_id, department_id
- FROM employees outer
- WHERE EXISTS ( SELECT 'X'
- FROM employees
- WHERE manager_id= outer.employee_id);
复制代码
8. 查询 departments 表中,不存在于 employees 表中的部门的department_id 和 department_name
- SELECT department_id,department_name
- FROM departments d
- WHERE NOT EXISTS (
- SELECT'X'
- FROM employees
- WHERE department_id =d.department_id);
复制代码 6.关于数据更新 9.修改表 employees,添加department_name 列,赋予department_id相应的部门名称。
- ALTER TABLE employees ADD(department_name VARCHAR2(14));
- UPDATE employees e
- SET department_name=
- ( SELECT department_name
- FROM departments d
- WHERE e.department_id= d.department_id);
复制代码 7.关于数据删除10.删除表 employees 中,其与emp_history 表皆有的数据
- DELETE FROM employees E
- WHERE employee_id in(
- SELECT employee_id
- FROM emp_history
- WHERE employee_id =E.employee_id);
复制代码 8.WITH 子句 11. 查询公司中各部门的总工资大于公司中各部门的平均总工资的部门信息
- WITH
- dept_costs AS (
- SELECT d.department_name,SUM(e.salary) AS dept_total
- FROM employeese, departments d
- WHERE e.department_id= d.department_id
- GROUPBY d.department_name),
- avg_cost AS(
- SELECT SUM(dept_total) / COUNT(*) AS dept_avg
- FROM dept_costs)
- SELECT *
- FROM dept_costs
- WHERE dept_total> (SELECT dept_avg FROM avg_cost)
- ORDER BY department_name;
复制代码 3 附加题目 NOTNULL/ALL/EXISTS/NOT EXISTS
12. 查询员工的 last_name, department_id, salary. 其中员工的salary,department_id 与有奖金的任何一个员工的 salary,department_id 相同即可
- select last_name, department_id, salary
- from employees
- where(salary,department_id) in (
- select salary,department_id
- from employees
- where commission_pct is not null
- )
复制代码13.选择工资大于所有 JOB_ID = 'SA_MAN' 的员工的工资的员工的last_name,job_id, salary
- select last_name, job_id, salary
- from employees
- where salary > all(
- selectsalary
- from employees
- where job_id = 'SA_MAN' )
复制代码14.选择所有没有管理者的员工的 last_name
- select last_name
- from employees e1
- where not exists (
- select 'A'
- from employees e2
- where e1.manager_id = e2.employee_id
- )
复制代码15. 查询 10,50,20 号部门的job_id,department_id 并且
department_id 按 10,50,20 的顺序排列(union)
- Column dummy noprint;
- select job_id , department_id ,1dummy
- from employees
- wheredepartment_id = 10
- union
- select job_id , department_id , 2
- from employees
- wheredepartment_id = 50
- union
- select job_id , department_id , 3
- from employees
- wheredepartment_id= 20
- order by 3
复制代码 4 总结1 Having 和Where的区别having 和where 都是用来筛选用的 ,但是having 是筛选组, 而where是筛选记录。他们有各自的区别:
1》当分组筛选的时候用having
-----------------------------------------------------
用having就一定要和group by连用
用group by不一有having (它只是一个筛选条件用的)
-------------------------------------------------------
2》其它情况用where
3》where和having的执行级别不同
在查询过程中聚合语句(sum,
min,max,avg,count)要比having子句优先执行。
而where子句在查询过程中优先于聚合语句(sum,
min,max,avg,count)。
having就是来弥补where在
分组数据判断时的不足,因为where执行优先级别要快于聚合语句。
2 With子句with子句-----给子查询定义一个别名,可以通过别名来引用子查询,实现了一次定义多次使用。
WITH子句的格式为: WITH 别名 AS(子查询)
定义好别名就可以在sql中通过别名来引用子查询