oracle课堂笔记--第十三天

自连接:

empid ename mgrid

100 abc

101 def 100

102 xyz 100

emp: mgr:

empid ename mgrid empid mgrname

100 abc 100 abc

101 def 100

102 xyz 100

101 def 100 100 abc

102 xyz 100 100 abc

select emp.ename, mgr.mgrname

from emp, mgr

where emp.mgrid=mgr.empid

emp: mgr:

empid ename mgrid empid ename mgrid

100 abc 100 abc

101 def 100 101 def 100

102 xyz 100 102 xyz 100

select e.last_name, m.last_name

from employees e, employees m

where e.manager_id=m.employee_id;

有经理的员工数:106

SQL> select count(*) from employees where manager_id is not null;

没有经理的员工数:1

SQL> select count(*) from employees where manager_id is null;

练习:

显示所有员工姓名和经理姓名,没有经理的显示“无”。

select e.last_name, nvl(m.last_name, ‘N/A‘)

from employees e, employees m

where e.manager_id=m.employee_id(+);

不等值连接:

conn scott/tiger

select e.ename, sg.grade

from emp e, salgrade sg

where e.sal between sg.losal and sg.hisal;

练习:

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

create table avg_sal_dept as select department_id, avg(salary) avg_sal from employees where department_id  is not null group by department_id;

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

from employees e, avg_sal_dept asd

where e.department_id=asd.department_id

and e.salary>asd.avg_sal;

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;

子查询

第一写成子查询   第二写成主查询

单行子查询的思路:

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工作的所有员工姓名

在Seattle的部门1 Seattle的location id是多少  2 location id 下的部门信息

在这些部门中的员工 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‘));

查找符合下列条件的员工姓名:和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值的影响:

有员工的部门名称

select department_name from  departments where department_id in (select department_id from employees);

没有员工的部门名称select department_name from  departments where department_id not in (select department_id from employees where department_id is  not 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);

练习:

在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‘));

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-10-03 21:54:34

oracle课堂笔记--第十三天的相关文章

oracle课堂随笔----第二十三天

RMAN配置 $ rman target / 或rman target sys/[email protected] RMAN> show all; 所有备份相关设置 RMAN> CONFIGURE CONTROLFILE AUTOBACKUP On; RMAN> CONFIGURE CONTROLFILE AUTOBACKUP clear; RMAN> backup tablespace users;  备份表空间用户 RMAN> list backup;  查看列表 SQL

oracle课堂笔记--第二十一天

死锁: session1: SQL> select * from t1; X ---------- 1 2 SQL> update t1 set x=11 where x=1; session2: SQL> update t1 set x=22 where x=2; session1: SQL> update t1 set x=222 where x=2; 阻塞 session2: SQL> update t1 set x=111 where x=1; 死锁 ERROR at

oracle课堂笔记--第十八天

Oracle NET 1.客户端通过@ora10g的名字去tnsname.ora文件获取服务器的具体连接信息 2.客户端通过tnsname.ora中的描述向服务器发出链接请求服务器端 3.服务器的监听器接收到连接请求后,验证请求的服务的有效性 4.服务器端产生一个服务进程和客户端进程建立连接 查看会话建立过程: $ netstat -tlnp | grep 1521 $ sqlplus sys/[email protected] as sysdba $ netstat -tnp | grep s

oracle课堂笔记--第十二天

分组函数   ,多行函数 对非空的数据总结 只有在列里才先把空值排除 分组不需要 SQL> select count(*), sum(salary), avg(salary), min(salary), max(salary) from employees; SQL> create table t1(x int); SQL> insert into t1 values (null); SQL> insert into t1 values (1); SQL> commit; S

Oracle课堂笔记

/* *创建表空间(以system的身份) */CREATE TABLESPACE spc_myself--表空间名称(spc_myself)DATAFILE 'E:\datafiles\MYSELF.DBF'--DATAFILE指定表空间的一个或多个数据文件,'数据文件的路径和名称'SIZE 30M--文件大小,M兆的字节大小AUTOEXTEND OFF--禁止数据文件自动扩展 /* *删除表空间 */DROP TABLESPACE spc_myself /* *创建用户 */CREATE U

oracle 课堂笔记

分页查询语句 语法简介  三重嵌套 select * from ( Select emp.*,rownum as rn from (seect * from emp ) where rownum <=9 ) where rn>=7 一.表字段的增删改: 添加字段的语法:alter table tablename add (column datatype [default value][null/not null],….); 修改字段的语法:alter table tablename modif

Oracle笔记(十三) 视图、同义词、索引

Oracle笔记(十三) 视图.同义词.索引 一.视图 在之前所学习过的所有的SQL语法之中,查询操作是最麻烦的,如果程序开发人员将大量的精力都浪费在查询的编写上,则肯定影响代码的工作进度,所以一个好的数据库设计人员,除了根据业务的操作设计出数据表之外,还需要为用户提供若干个视图,而每一个视图包装了一条条复杂的SQL语句,视图的创建语法如下: CREATE [OR REPLACE] VIEW 视图名称 AS 子查询; 范例:创建一张视图 CREATE VIEW myview AS SELECT

2017年5月12号课堂笔记

2017年5月12号 星期五 空气质量:轻度污染(昨天的北风转今天的南风) 内容:html表格的基本使用,表格跨行跨列,高级表格,播放音乐,播放视频,网页布局,iframe内联框架: 文本框,密码框,单选按钮,复选框,下拉框  备注:周日晚想起来补上的周五课堂笔记(一带一路今天开会天气好晴朗) 一.html表格的基本使用 模仿老师代码: <!DOCTYPE html><html><head lang="en"> <meta charset=&q

?统计学习精要(The Elements of Statistical Learning)?课堂笔记(一)

前两天微博上转出来的,复旦计算机学院的吴立德吴老师在开?统计学习精要(The Elements of Statistical Learning)?这门课,还在张江...大牛的课怎能错过,果断请假去蹭课...为了减轻心理压力,还拉了一帮同事一起去听,eBay浩浩荡荡的十几人杀过去好不壮观!总感觉我们的人有超过复旦本身学生的阵势,五六十人的教室坐的满满当当,壮观啊. 这本书正好前阵子一直在看,所以才会屁颠屁颠的跑过去听.确实是一本深入浅出讲data mining models的好书.作者网站上提供免