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;

SQL> select count(*) from t1;

SQL> select count(x) from t1;

SQL> select max(x) from t1;

SQL> select min(x) from t1;

SQL> select sum(x) from t1;

SQL> select avg(x) from t1;

SQL> select avg(salary), avg(nvl(commission_pct, 0)) from employees;

SQL> select count(distinct department_id) from employees; 去除重复值

Group by分组:

SQL> select department_id, avg(salary) from employees group by department_id;

多列分组:

SQL> select department_id, job_id, max(salary) from employees group by department_id, job_id;

SQL> select department_id, job_id, max(salary), last_name from employees group by department_id, job_id; 错误语法

练习:

公司中不同职位的数量

select (distinct job_id)from employees;

计算每个部门的人数

Select Department_id,count(employees_id)

From employees

Where department_id is not null

Group by department_id

按年份分组,求员工的工资总和

Select extract(year from hire_date),avg(salary)

From employees

Group by extract(year from hire_date)

Having语句:

SQL> select department_id, avg(salary) from employees where avg(salary)>=5000 group by department_id; 错误语句

SQL> select department_id, avg(salary) from employees group by department_id having avg(salary)>=5000;

练习:

按部门求出所有有部门的普通员工的平均工资,部门平均工资少于5000的不显示,最终结果按平均工资的降序排列。

select department_id, avg(salary) avg_sal

from employees

where job_id not like ‘%\_MGR‘ escape ‘\‘ and department_id is not null

group by department_id

having avg(salary)>=5000

order by avg_sal desc;

多表连接no

emp: dept:

empno ename deptno deptno dname

100 abc 10 10 sales

101 def 10 20 market

102 xyz 20 30 it

103 opq null

for emp in 100 .. 103

for dept in 10 .. 30

emp.deptno=dept.deptno

100         abc         10              10          sales

101         def         10              10          sales

102         xyz         20              20          market

获取如下信息,准备工作:

employees:

员工总数:107

SQL> select count(*) from employees;

有部门的员工数:106

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

SQL> select count(department_id) from employees;

没有部门的员工数:1

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

departments:

部门总数:27

SQL> select count(*) from departments;

有员工的部门数:11

SQL> select count(distinct department_id) from employees;

没有员工的部门数:16

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

for dept in 1..27

for emp in 1..107

dept.deptid不在emp表中出现

select count(*)

from employees e, departments d

where e.department_id(+)=d.department_id

and e.employee_id is null;

select count(*)

from departments d

where not exists

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

select (select count(*) from departments)-(select count(distinct department_id) from employees) from dual;

内连接:106(106, 11)

select e.last_name, d.department_name

from employees e, departments d

where e.department_id=d.department_id;

select e.last_name, d.department_name

from employees e join departments d on e.department_id=d.department_id;

左外连接:107(106+1)

select e.last_name, d.department_name

from employees e, departments d

where e.department_id=d.department_id(+);

select e.last_name, d.department_name

from departments d, employees e

where e.department_id=d.department_id(+);

select e.last_name, d.department_name

from employees e left outer join departments d

on e.department_id=d.department_id;

右外连接:122(106+16)

select e.last_name, d.department_name

from employees e, departments d

where e.department_id(+)=d.department_id;

select e.last_name, d.department_name

from employees e right outer join departments d

on e.department_id=d.department_id;

完全外连接:123(106+1+16)

select e.last_name, d.department_name

from employees e full outer join departments d

on e.department_id=d.department_id;

多表连接的扩展:

n张表连接:

select e.last_name, d.department_name, l.city

from employees e, departments d, locations l

where e.department_id=d.department_id

and d.location_id=l.location_id;

select e.last_name, d.department_name, l.city

from employees e join departments d on e.department_id=d.department_id

join locations l on d.locationy_id=l.location_id;

select e.last_name, d.department_name, l.city

from employees e, departments d, locations l

where e.department_id=d.department_id(+)

and d.location_id=l.location_id(+);

select e.last_name, d.department_name, l.city

from employees e left outer join departments d on e.department_id=d.department_id

left outer join locations l on d.location_id=l.location_id;

时间: 2024-10-14 00:32:36

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

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

备份恢复 配置可恢复性 冗余 控制文件   (镜像) SQL> show parameter control_files SQL> select * from v$controlfile; 修改路径: $ cd $ORACLE_HOME/dbs $ cp spfileorcl.ora spfileorcl.ora.bak SQL> alter system set control_files='/u01/app/oracle/oradata/orcl/control01.ctl', '/

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课堂笔记--第十三天

自连接: 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

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

九章算法系列(#2 Binary Search)-课堂笔记

前言 先说一些题外的东西吧.受到春跃大神的影响和启发,推荐了这个算法公开课给我,晚上睡觉前点开一看发现课还有两天要开始,本着要好好系统地学习一下算法,于是就爬起来拉上两个小伙伴组团报名了.今天听了第一节课,说真的很实用,特别是对于我这种算法不扎实,并且又想找工作,提高自己的情况. 那就不多说废话了,以后每周都写个总结吧,就趁着这一个月好好把算法提高一下.具体就从:课堂笔记.leetcode和lintcode相关习题.hdu和poj相关习题三个方面来写吧.希望自己能够坚持下来,给大家分享一些好的东

Oracle 学习笔记 17 -- 异常处理(PL/SQL)

程序在执行过程中出现异常是正常的,在程序的编写过程中出现异常也是不可避免的.但是要有相应的异常处理的机 制,来保证程序的正常执行.PL/SQL程序执行过程中出现的错误,称为异常.一个优秀的程序都应该能够正确处理 各种出错的情况,并尽可能的从错误中恢复.PL/SQL提供了异常处理机制. 概念: 异常处理(exception)是用来处理正常执行过程中未预料的事件,程序块的异常处理定义的错误和自定义的错误, 由于PL/SQL程序块一旦产生异常而没有指出如何处理时,程序就会异常的终止. 有三种类型的错误

2017年5月12号课堂笔记

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