Oracle子查询之简单子查询
Oracle 简单子查询
顾名思义,简单子查询是嵌套在 SQL 语句中的另一个SELECT 语句,并且子查询只返回一列数据
1,单行子查询:
子查询 (内查询) 在主查询之前一次执行完成。子查询的结果被主查询(外查询)使用 ,单行子查询,一个子查询语句只返回一行结果,不能返回空值
可以使用>,<,<>(!=),=,<=,>=
select select_list from table
where expr operation --operation为条件语句表达式,
(select select_list
from table
where expr);
例1:查询工资比Abel高的人
select first_name||' '||last_name name,salary
from employees
where salary >
(select salary from employees --该子查询只返回salary一个值
where lower(last_name) = 'abel');
NAME SALARY
---------------------- ----------
Steven King 24000
Neena Kochhar 17000
Lex De Haan 17000
Nancy Greenberg 12000
John Russell 14000
Karen Partners 13500
Alberto Errazuriz 12000
Lisa Ozer 11500
Michael Hartstein 13000
Shelley Higgins 12000
例2:查询最低工资大于50号部门最低工资的部门id和其最低工资
select nvl(department_id,1) department_id,min(salary)
from employees
group by department_id
having min(salary) >
(select min(salary)
from employees
where department_id = 50);
DEPARTMENT_ID MIN(SALARY)
------------- -----------
100 6900
30 2500
1 7000
90 17000
20 6000
70 10000
110 8300
80 6100
40 6500
60 4200
10 4400
例3,单行子查询中使用单行函数
显式员工的employee_id,last_name和location。其中,若员工department_id与location_id为1800的department_id相同,则location为’Canada’,其余则 为’USA’。
select employee_id,last_name,department_id,
(case when department_id = (select department_id
from departments
where location_id = 1800
)
then 'USA'
else 'Canada' end) location
from employees
order by department_id;
或
select employee_id,last_name,department_id,
(case department_id when (select department_id
from departments
where location_id = 1800
)
then 'USA'
else 'Canada' end) location
from employees
order by department_id;
或
select employee_id,last_name,department_id,
decode(department_id,(select department_id
from departments
where location_id = 1800
),'USA',
'Canada') location
from employees
order by department_id;
2,多行子查询:
子查询返回多行值,可使用多行比较符:in(not in)(等于/不等于返回值的任意一个),any(和返回值的某一个值比较),all(和返回值的所有值比较)
例1:查询job_id不为为‘IT_PROG’,并且工资比任意一个job_id为‘IT_PROG’的人都低的员工信息;
或者工资比所有job_id为‘IT_PROG’的人都低的员工信息;
或者工资等于任意一个job_id为‘IT_PROG’的人都低的员工信息;
SQL> select employee_id,first_name||' '||last_name name,job_id,salary
from employees
where salary < any(
select salary
from employees
where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';
EMPLOYEE_ID NAME JOB_ID SALARY
----------- --------------------- ---------- ----------
132 TJ Olson ST_CLERK 2100
136 Hazel Philtanker ST_CLERK 2200
128 Steven Markle ST_CLERK 2200
135 Ki Gee ST_CLERK 2400
127 James Landry ST_CLERK 2400
191 Randall Perkins SH_CLERK 2500
182 Martha Sullivan SH_CLERK 2500
144 Peter Vargas ST_CLERK 2500
...(省略部分)
SQL> select employee_id,first_name||' '||last_name name,job_id,salary
from employees
where salary < all(
select salary
from employees
where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';
EMPLOYEE_ID NAME JOB_ID SALARY
----------- ----------------- -------------------- ----------
185 Alexis Bull SH_CLERK 4100
192 Sarah Bell SH_CLERK 4000
193 Britney Everett SH_CLERK 3900
188 Kelly Chung SH_CLERK 3800
137 Renske Ladwig ST_CLERK 3600
189 Jennifer Dilly SH_CLERK 3600
141 Trenna Rajs ST_CLERK 3500
186 Julia Dellinger SH_CLERK 3400
133 Jason Mallin ST_CLERK 3300
...(省略部分)
SQL> select employee_id,first_name||' '||last_name name,job_id,salary
from employees
where salary in (
select salary
from employees
where job_id = 'IT_PROG')
and job_id <> 'IT_PROG';
EMPLOYEE_ID NAME JOB_ID SALARY
----------- -------------------- -------------------- ----------
158 Allan McEwen SA_REP 9000
152 Peter Hall SA_REP 9000
109 Daniel Faviet FI_ACCOUNT 9000
202 Pat Fay MK_REP 6000
184 Nandita Sarchand SH_CLERK 4200
更多例题:
1. 查询工资最低的员工信息: last_name, salary
select last_name,salary
from employees
where salary = (
select min(salary)
from employees);
2. 查询平均工资最低的部门信息
SQL> select d.*,city
from departments d,locations ll
where ll.location_id = d.location_id
and d.department_id = (
select department_id
from employees
group by department_id
having avg(salary) = ( select min(avg(salary))
from employees
group by department_id));
3*. 查询平均工资最低的部门信息和该部门的平均工资
select d.*,
(select min(avg(salary)) from employees group by department_id) avg_salary,
city
from departments d,locations ll
where ll.location_id = d.location_id
and d.department_id = (
select department_id
from employees
group by department_id
having avg(salary) = ( select min(avg(salary))
from employees
group by department_id));
4. 查询平均工资最高的 job 信息
select * from jobs
where job_id in (
select job_id from employees
group by job_id
having avg(salary) = (
select max(avg(salary))
from employees
group by job_id));
5. 查询平均工资高于公司平均工资的部门有哪些?
select department_id from employees
group by department_id
having avg(salary) >
(select avg(salary) from employees);
6. 查询出公司中所有 manager 的详细信息.
select * from employees
where employee_id in (
select distinct(manager_id)
from employees);
7. 查询各个部门中的最高工资,找出其中中最低的最高工资是那个部门,并查询其最低工资是多少
select department_id,min(salary)
from employees
where department_id in (
select department_id
from employees
group by department_id
having max(salary) in (
select min(max(salary))
from employees
group by department_id))
group by department_id;
8. 查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
select last_name,department_id,email,salary --根据manager_id查询相关信息
from employees
where employee_id in (
select distinct(manager_id) --根据平均工资最高的部门ID,查询这个部门的有哪些manager
from employees
where department_id = (
select department_id --查询平均工资等于最高的部门的ID
from employees
group by department_id
having avg(salary) = (
select max(avg(salary)) --查询平均工资最高的
from employees
group by department_id)));
9. 查询 1999 年来公司的人所有员工的最高工资的那个员工的信息.
select *
from employees
where salary in (
select max(salary)
from employees
where employee_id in (
select employee_id
from employees
where to_char(hire_date,'YYYY') = '1999'))
and to_char(hire_date,'YYYY') = '1999';
本博文系学习尚硅谷网易云课堂课程整理而成。
Oracle子查询之简单子查询的更多相关文章
- oracle学习 第一章 简单的查询语句 ——03
1.1最简单的查询语句 例 1-1 SQL> select * from emp; 例 1-1 结果 这里的 * 号表示全部的列.它与在select 之后列出全部的列名是一样的.查询语句以分号( ...
- Oracle子查询之高级子查询
Oracle 高级子查询 高级子查询相对于简单子查询来说,返回的数据行不再是一列,而是多列数据. 1,多列子查询 主查询与子查询返回的多个列进行比较 查询与141号或174号员工的manager_id ...
- Oracle SQL Lesson (7) - 使用子查询
使用子查询简单子查询SELECT select_listFROM tableWHERE expr operator (SELECT select_list FROM table);子查询可以出现在se ...
- T-SQL 简单子查询
1.使用变量的方式实现的查询 use StudentManageDB go declare @StuId int --查询张永利学号 select @StuId=StudentId from Stud ...
- ORACLE 多表连接与子查询
Oracle表连接 SQL/Oracle使用表连接从多个表中查询数据 语法格式: select 字段列表from table1,table2where table1.column1=table2.co ...
- Oracle子查询和多表查询
多表查询需要用到表的连接 连接可以分为:(自行百度) 交叉连接(数字逻辑的笛卡尔积,不做解释) 等值连接 例如:select * from t_a, t_b where t_a.xx = t_b.xx ...
- Oracle 数据库基础学习 (六) 子查询
子查询在一个select中出现多个嵌套查询语句 1.在where子句中使用子查询(一般返回"单行单列" "单行多列" "多行单列"(可以提供 ...
- Oracle学习DayFour(高级子查询)
一.高级子查询 1.多列子查询 定义:主查询与子查询返回的多个列进行比较 多列子查询中的比较分为两种:成对比较:不成对比较 实例:查询与141号或174号员工的manager_id和departmen ...
- oracle基本查询入门(二) 子查询
一.子查询语法 SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table); 子查询在主查询之前 ...
随机推荐
- Junit 单元测试、BeanUtils、Properties类
一. Junit单元测试 1.1. Junit单元测试框架的基本使用 一.搭建环境: 导入junit.jar包(junit4) 二.写测试类: 0,一般一个类对应一个测试类. 1,测试类与被测试类最好 ...
- Apache服务器运维笔记(5)----容器的处理顺序
容器在配置文件中是可以多次使用的,同时也可以嵌套使用,但是 Apache 在处理容器时却是有一定顺序的,因此在编写容器配置时需要按照一定的顺序来进行,否则Apache处理的结果很可能不是管理员想要的. ...
- CentOS 7运维管理笔记(2)----修改命令提示符颜色
使用 su 命令切换到root用户: 使用 vi /etc/bashrc 命令插入如下代码: PS1="[\e[1;32m\u\e[m\e[1;33m@\e[m\e[1;35m\h\e[m ...
- 目前比较全的CSS重设(reset)方法总结(转)
原文地址 在当今网页设计/开发实践中,使用CSS来为语义化的(X)HTML标记添加样式风格是重要的关键.在设计师们的梦想中都存在着这样的一个完美世界:所有的浏览器都能够理解和适用多有CSS规则,并且呈 ...
- python 二叉树计算器
例子:计算1+2+3+4的值 代码: class Buffer(object): """字符串处理函数""" def __init__(se ...
- Excel 函数使用
字符串 20180613 转为日期 2018-06-13,单元格内输入如下公式 =DATE(LEFT(),MID(,),RIGHT()) IF 函数内的或.与 =IF(AND(A=B,C=D),&q ...
- 关于windows server 里Let's Encrypt续订的问题
引言 Let's Encrypt是什么就不详细说了,它是免费的https证书,优点就是免费,缺点就是每三个月就要自己续上.今天主要介绍的是续上有效期的环节. 1.安装certify 下载地址: htt ...
- IE浏览器 div或者其他容器的height属性无效 滚动条问题解决办法
1.height设置定值是功能好使的 但是如果在不同分辨率的电脑上运行程序 会出现样式上的偏差 2.height的百分比是根据父级来的 所以将父级的height设置好(如果当前容器上方有很多父级 ...
- hydra 常用的命令
1.本地调试模式运行项目 /gaea hydra /zk节点名 -r "zk://zk地址" -t "节点标识" -d -w 项目相对于$GOPATH/src ...
- 二、基于事件的异步编程模式(EAP)
一.引言 在上一个专题中为大家介绍了.NET 1.0中提出来的异步编程模式--APM,虽然APM为我们实现异步编程提供了一定的支持,同时它也存在着一些明显的问题--不支持对异步操作的取消和没有提供对进 ...