Oracle-高级子查询

简介: Oracle-高级子查询

1.多列子查询

--查询与141号或174号员工的manager_id department_id
--相同的其他员工的employee_id  manager_id department_id
【old】
/*
select employee_id,manager_id,department_id from employees
where manager_id in (
select manager_id from employees where employee_id in (141,174)
)
and department_id in (
select department_id from employees where employee_id in (141,174)
)
and employee_id not in (141,174)
*/
【new】
select employee_id,manager_id,department_id from employees
where (manager_id,department_id) in (
select manager_id,department_id from employees where employee_id in (141,174)
)
and employee_id not in (141,174)

2.from字句中使用子查询

--在from字句中使用子查询
--返回比本部门平均工资高的员工的last_name,department_id,salary及平均工资
--【old】
/*
select last_name,department_id ,salary,
(select avg(salary) from employees e3
where e3.department_id = e1.department_id group by department_id) as "avg(salary)"
from employees e1
where salary > (
 select avg(salary) from employees e2
 where e2.department_id  = e1.department_id group by department_id
)*/
--【new1】
select e1.last_name,e1.department_id,e1.salary,e2."avg_sal"
from employees e1,
(select department_id,avg(salary) as "avg_sal"
from employees  group by department_id) e2
where e2.department_id = e1.department_id
and e1.salary > e2."avg_sal"
--【new2】--e2.avg_sal 没有添加双引号
select e1.last_name,e1.department_id,e1.salary,e2.avg_sal
from employees e1,
(select department_id,avg(salary) avg_sal
from employees  group by department_id) e2
where e2.department_id = e1.department_id
and e1.salary > e2."avg_sal"

3.条件表达式中使用子查询

case..when..then..else..end

--显示员工的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.order by中使用子查询

--查询员工的employee_id,last_name,要求按照员工的department_name 排序
select employee_id,last_name 
from employees e1
order by(
select department_name from departments d
where e1.department)id = d.department_id
)

5.where字句中使用子查询

--若employees表中employee_id与job_history表中employee_id
--相同的数目不小于2,则输出这些相同id的员工的employee_id,last_name和job_id
select employee_id,last_name,job_id
from employees e1 
where 2 <= (
select count(*) from job_history
where employee_id = e1.employee_id
)

6.exists操作符

--查询公司管理者的employee_id,last_name,job_id,department_id的信息
/*
select employee_id,manager_id,last_name,job_id,department_id
from employees e1
where exists(
select 'a' from employees e2 
where e1.employee_id = e2.manager_id
)
*/
--查询departments表中,不存在与employees表中的部门的department_id和department_name
select department_id,department_name from departments d
where not exists (
select 'c' from employees where department_id = d.department_id
)

7.with字句

--查询公司中各部门的总工资大于公司中各部门的平均工资的部门信息
with dept_sumsal as (
select department_name,sum(salary) sum_sal1
from departments d,employees e
where d.department_id = e.department_id
group by department_name
),
dept_avgsal as(
select sum(sum_sal1)/count(*) avg_sum_sal2
from dept_sumsal
)
select * from dept_sumsal
where sum_sal1 > (
select avg_sum_sal2 from dept_avgsal
)
目录
相关文章
|
6月前
|
SQL Oracle 关系型数据库
JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询
JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询
112 0
JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询
|
6月前
|
SQL Oracle 关系型数据库
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作
140 0
|
SQL XML Oracle
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作(下)
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作(下)
|
SQL XML Oracle
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作(中)
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作(中)
|
SQL Oracle 关系型数据库
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作(上)
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作(上)
|
SQL 机器学习/深度学习 移动开发
Oracle多表查询,子查询,分页查询
🍅程序员小王的博客:程序员小王的博客 🍅 欢迎点赞 👍 收藏 ⭐留言 📝 🍅 如有编辑错误联系作者,如果有比较好的文章欢迎分享给我,我会取其精华去其糟粕 🍅java自学的学习路线:java自学的学习路线
338 0
Oracle多表查询,子查询,分页查询
|
SQL Oracle 关系型数据库
Oracle中的分组查询、子查询、多表连接
Oracle中的分组查询、子查询、多表连接
270 0
Oracle中的分组查询、子查询、多表连接
|
Oracle 关系型数据库 OLAP
Oracle:一篇文章理解model高级语句
Oracle 高级语句model的概念和基本用法,对数据仓库有了解,想要在数据库上实现数据仓库功能的可以看看
538 0
|
SQL Oracle 关系型数据库
|
Oracle 关系型数据库
oracle学习60-oracle之高级子查询2
oracle学习60-oracle之高级子查询2
95 0
oracle学习60-oracle之高级子查询2

推荐镜像

更多