17.7.31

有员工的部门名称;

  SQL> select department_name from departments where department_id in(select department_id from employees where department_id is not null);     (in)

  SQL> select department_name from departments d where exists(select department_id from employees where department_id=d.department_id);    (exists  关联子查询 )

  SQL> select distinct d.department_name from employees e,departments d where e.department_id=d.department_id;   (多表连接)

没有员工的部门名称;

  SQL> select department_name from departments where department_id not in(select department_id from employees where department_id is not null);

  SQL> select department_name from departments d where not exists(select department_id from employees where department_id=d.department_id);

所有管理者的姓名;

  SQL> select last_name from employees where employee_id in (select manager_id from employees);

  SQL> select last_name from employees e where exists(select 1 from employees where manager_id=e.employee_id);

所有普通员工的姓名

  SQL> select last_name from employees where employee_id not in (select manager_id from employees where manager_id is not null);

  SQL> select last_name from employees e where not exists(select 1 from employees where manager_id=e.employee_id);

(把不确定的空值排除掉)

rownum 只能 <=

top-N查询:

SQL> select last_name, salary from employees where rownum<=3 order by salary desc;

SQL> select * from (select last_name, salary from employees order by salary desc) where rownum<=3;

子查询

单行子查询的思路:

SQL> select salary from employees where last_name=‘Feeney‘;

SQL> select last_name from employees where salary>3000;

SQL> select last_name from employees where salary>(select salary from employees where last_name=‘Feeney‘);

多行子查询的思路:

SQL> select distinct department_id from employees where department_id is not null;

SQL> select department_name from departments where department_id in (10, 20,30);

SQL> select department_name from departments where department_id in (select department_id from employees where department_id is not null);

用多表连接改写:

select distinct d.department_name

from employees e, departments d

where e.department_id=d.department_id

for dept in 1..27

for emp in 1..107

查看emp中是否出现deptid

练习:

工资大于全公司平均工资的员工姓名。

SQL> select last_name from employees where salary>(select avg(salary) from employees);

和Feeney同年入职的员工姓名

select last_name, hire_date

from employees

where extract(year from hire_date)=

(select extract(year from hire_date) from employees where last_name=‘Feeney‘)

and last_name != ‘Feeney‘;

select last_name, hire_date

from employees

where hire_date between

(select to_date(to_char(hire_date, ‘yyyy‘)||‘0101‘, ‘yyyymmdd‘) from employees where last_name=‘Feeney‘)

and

(select to_date(to_char(hire_date, ‘yyyy‘)||‘1231‘, ‘yyyymmdd‘) from employees where last_name=‘Feeney‘)

在Seattle工作的所有员工姓名

select last_name

from employees

where department_id in

(select department_id from departments

where location_id=

(select location_id from locations where city=‘Seattle‘));

查找符合下列条件的员工姓名:和Abel在同一个部门,工资比Olson高

select last_name from employees

where department_id=

(select department_id from employees where last_name=‘Abel‘)

and salary >

(select salary from employees where last_name=‘Olson‘);

配对子查询:

和Feeney在同一个部门、做同一职位的员工姓名:

select last_name, department_id, job_id

from employees

where department_id=

(select department_id from employees where last_name=‘Feeney‘)

and job_id=

(select job_id from employees where last_name=‘Feeney‘)

and last_name != ‘Feeney‘;

select last_name, department_id, job_id

from employees

where (department_id, job_id)=

(select department_id, job_id from employees where last_name=‘Feeney‘)

and last_name != ‘Feeney‘;

in和not in受null值的影响:

(把不确定的空值排除掉)

所有管理者的姓名:

SQL> select last_name from employees where employee_id in (select manager_id from employees);

所有普通员工的姓名:

SQL> select last_name from employees where employee_id not in (select manager_id from employees where manager_id is not null);

关联子查询:

工资大于所在部门平均工资的员工姓名。

for i in 1..107所有员工

{

select avg(salary) from employees where department_id=i.department_id

if i.salary > i所在部门的平均工资

保留此记录

}

select last_name

from employees outer

where salary >

(select avg(salary) from employees

where department_id = outer.department_id);

select e.last_name, e.salary, asd.avg_sal

from employees e, (select department_id, avg(salary) avg_sal from employees where department_id is not null group by department_id) asd

where e.department_id=asd.department_id

and e.salary>asd.avg_sal;

exists/not exists查询:

for i in 1..27所有部门

{

for j in 1..107所有员工

{

if i.department_id = j.department_id

保留此记录

break

}

}

select department_name

from departments outer

where exists

(select 1 from employees where department_id=outer.department_id);

select department_name

from departments outer

where not exists

(select 1 from employees where department_id=outer.department_id);

练习:

所有管理者的姓名:

for i in 1..107所有员工

{

for j in 1..107所有员工

{

if i.employee_id = j.manager_id

保留此记录

break

}

}

select last_name

from employees outer

where exists

(select 1 from employees where manager_id=outer.employee_id);

所有普通员工的姓名:

select last_name

from employees outer

where not exists

(select 1 from employees where manager_id=outer.employee_id);

子查询和多表连接的转换:

有员工的部门的名称

select department_name

from departments

where department_id in

(select department_id from employees);

select department_name

from departments outer

where exists

(select 1 from employees where department_id=outer.department_id);

select distinct d.department_name

from employees e, departments d

where e.department_id=d.department_id;

练习:

在Seattle工作的所有员工姓名(使用子查询和多表连接两种方式)

(在Seattle的部门:

1.Seattle的location_id是多少

2.Location_id下的部门信息

3.在这些部门中的员工:1.哪些部门2.这些部门的员工

select last_name

from employees

where department_id in

(select department_id from departments

where location_id=

(select location_id from locations where city=‘Seattle‘));

select e.last_name

from employees e, departments d, locations l

where e.department_id=d.department_id

and d.location_id=l.location_id

and l.city=‘Seattle‘;

最大值查询:

SQL> select last_name from employees where salary=(select max(salary) from employees);

top-N查询:

SQL> select last_name, salary from employees where rownum<=3 order by salary desc;

SQL> select * from (select last_name, salary from employees order by salary desc) where rownum<=3;

分页查询:

SQL> select * from

(select * from

(select * from

(select last_name, salary from employees order by salary desc)

where rownum<=6)

order by salary)

where rownum<=3

order by salary desc;

SQL> select last_name, salary

from (select rownum row_num, v1.*

from

(select last_name, salary from employees order by salary desc) v1

) v2

where row_num between 4 and 6;

select last_name, salary

from (select rownum row_num, v1.*

from

(select last_name, salary from employees order by salary desc) v1

where rownum<=6

) v2

where row_num >= 4;

时间: 2024-12-25 23:45:56

17.7.31的相关文章

17.12.31 链表合并

链表合并 描述 定义一种单向链表,链表结点LinkNode包含一个整数和一个指向下一个节点的指针.编写下面四个子函数: 1.建立链表: 从标准输入读取数据,并创建链表,返回创建的链表头 LinkNode *CreateLinkList(); 2.合并链表:参数是两个链表的头指针,返回合并后链表的头指针.合并后的链表仍然有序. LinkNode *MergeLinkList(LinkNode *a, LinkNode *b); 3.遍历并显示合并后的链表的元素. void DisplayLinkL

17.10.31&amp;11.01

10.31模拟考试 Prob.1(AC)裸的矩阵幂 Prob.2(WA)(类似括号匹配求合法方案数) 卡特兰数的一个模型运用.可以推出一个式子(推导方法一个erge讲的,一个骚猪讲的) Prob.3(崩溃2个点) 用tarjan求出双联通分量,缩点,然后形成一个无向无环图(本题保证联通,则是一棵树),求树上每一个点到其他点的最远距离. 那个求最远距离,有一个常用方法: 与该点距离最远的点一定是树的直径的一个端点. 我竟然不晓得这个方法!然后就通过旋转树的根等一系列麻烦操作搞这个问题,虽然写了很久

17.8.31

这几天上课上草鸡,也没打什么比赛,看了看线段树做了几道lintcode的题目,发现自己对类的了解有些落后了.... 前几天搞了好一会才弄懂lintcode的提交原理.刷过了几道题之后在做lintcode.245的时候卡住了.正好平台也卡,真的要抓狂.样例 无限99999  ,明白算法有错误,改完之后个例一直runtime error,弄得我很是困惑,也一直没找出错误.尤其今天,做题的一半时间都在卡..很烦

java多线程17:ThreadLocal源码剖析

ThreadLocal源码剖析 ThreadLocal其实比较简单,因为类里就三个public方法:set(T value).get().remove().先剖析源码清楚地知道ThreadLocal是干什么用的.再使用.最后总结,讲解ThreadLocal采取这样的思路. 三个理论基础 在剖析ThreadLocal源码前,先讲一下ThreadLocal的三个理论基础: 1.每个线程都有一个自己的ThreadLocal.ThreadLocalMap对象 2.每一个ThreadLocal对象都有一个

【转】String hashCode 方法为什么选择数字31作为乘子

某天,我在写代码的时候,无意中点开了 String hashCode 方法.然后大致看了一下 hashCode 的实现,发现并不是很复杂.但是我从源码中发现了一个奇怪的数字,也就是本文的主角31.这个数字居然不是用常量声明的,所以没法从字面意思上推断这个数字的用途.后来带着疑问和好奇心,到网上去找资料查询一下.在看完资料后,默默的感叹了一句,原来是这样啊.那么到底是哪样呢?在接下来章节里,请大家带着好奇心和我揭开数字31的用途之谜. 2. 选择数字31的原因 在详细说明 String hashC

String hashCode 方法为什么选择数字31作为乘子

1. 背景 某天,我在写代码的时候,无意中点开了 String hashCode 方法.然后大致看了一下 hashCode 的实现,发现并不是很复杂.但是我从源码中发现了一个奇怪的数字,也就是本文的主角31.这个数字居然不是用常量声明的,所以没法从字面意思上推断这个数字的用途.后来带着疑问和好奇心,到网上去找资料查询一下.在看完资料后,默默的感叹了一句,原来是这样啊.那么到底是哪样呢?在接下来章节里,请大家带着好奇心和我揭开数字31的用途之谜. 2. 选择数字31的原因 在详细说明 String

python数据结构与算法(17)

归并排序归并排序是采?分治法的?个?常典型的应?.归并排序的思想就是先递归 分解数组,再合并数组.将数组分解最?之后,然后合并两个有序数组,基本思路是?较两个数组的 最前?的数,谁?就先取谁,取了后相应的指针就往后移?位.然后再? 较,直??个数组为空,最后把另?个数组的剩余部分复制过来即可.归并排序的分析6 5 3 1 8 7 2 4 def merge_sort(alist): if len(alist) <= 1: return alist # ?分分解 num = len(alist)/

JDK1.8源码学习-String-hashCode方法为什么选择数字31作为乘子

1. 背景 某天,我在写代码的时候,无意中点开了 String hashCode 方法.然后大致看了一下 hashCode 的实现,发现并不是很复杂.但是我从源码中发现了一个奇怪的数字,也就是本文的主角31.这个数字居然不是用常量声明的,所以没法从字面意思上推断这个数字的用途.后来带着疑问和好奇心,到网上去找资料查询一下.在看完资料后,默默的感叹了一句,原来是这样啊.那么到底是哪样呢?在接下来章节里,请大家带着好奇心和我揭开数字31的用途之谜. 2. 选择数字31的原因 在详细说明 String

每天一个linux命令(45):free 命令

free命令可以显示Linux系统中空闲的.已用的物理内存及swap内存,及被内核使用的buffer.在Linux系统监控的工具中,free命令是最经常使用的命令之一. 1.命令格式: free [参数] 2.命令功能?: free 命令显示系统使用和空闲的内存情况,包括物理内存.交互区内存(swap)和内核缓冲区内存.共享内存将被忽略 3.命令参数: -b  以Byte为单位显示内存使用情况. -k  以KB为单位显示内存使用情况. -m  以MB为单位显示内存使用情况. -g   以GB为单