1:常用的函数

to_date()函数,将字符串转换为日期格式
select to_date('2015-09-12','yyyy-MM-dd') from dual; --其中后面的日期格式要和前面要转化的匹配

to_number()函数,将字符串转换为数字格式
select ename,sal from emp where sal>to_number('$5000.00','$9999.99');

清屏命令:clear screen;

2:常用的组函数 max() min() avg() sum() count()

select max(sal) from emp; --最高薪水是多少

select min(sal) from emp; --最低薪水是多少

select avg(sal) from emp; --平均薪水是多少

select sum(sal) from emp; --薪水总和是多少

select count(ename) from emp; --总共有多少员工

3:group by 求部门的平均薪水
select deptno,avg(sal) from emp group by deptno;

4:执行select一般顺序
select deptno,avg(sal) from emp(表名) where sal>5000(where限定条件) group by deptno(按照部门分组) having avg(sal)>5000

order by avg(sal) desc;

5:处理重复记录的问题
select empno from emp group by empno having count(empno)>1; --查询出重复的empno
select * from emp where empno in(select empno from emp group by empno having count(empno)>1); --查询出重复的记录
delete from emp where empno in (select empno from emp group by empno having count(empno)>1) and rowid not in (select min

(rowid) from emp group by empno having count(empno)>1); --删除重复记录,保留第一条

6:子查询
select ename,sal from emp where sal=(select max(sal) from emp); --查询公司中薪水最高的员工姓名
select ename,sal from emp where sal in(select max(sal) from emp group by deptno);--查询每个部门薪水最高的员工姓名 --错误的

(所有员工只要和其中一个匹配就会被查出)
select ename,sal from emp e join (select max(sal) max_sal,deptno from emp group by deptno) t on (e.sal=t.max_sal and

e.deptno=t.deptno);
或者select ename,sal from emp e,(select max(sal) max_sal, deptno from emp group by deptno) t where e.sal=t.max_sal and

e.deptno=t.deptno;
select empno,count(1) from emp group by empno having count(1)>1; --查询员工号重复的员工

92年标准:
select ename,sal,grade from emp e,grade g where e.sal between g.lowsal and g.higsal; --求所有员工薪水等级
select ename,sal,grade from emp e join (select max(sal) max_sal,deptno from emp group by deptno) t on (e.sal=t.max_sal and

e.deptno=t.deptno) join grade g on (t.max_sal bwtween g.lowsal and g.higsal);--求部门中薪水最高员工姓名、等级

--求各部门平均工资等级
select deptno ,avg_sal,grade from (select avg(sal) avg_sal,deptno from emp group by deptno) t join grade g on (t.avg_sal

between g.lowsal and g.higsal);

--求各部门的平均薪水等级
select ename,deptno,sal,grade from emp e join grade g on(e.sal between g.lowsal and g.higsal); --每个员工的薪水等级
select deptno,avg(grade) from (select ename,deptno,sal,grade from emp e join grade g on(e.sal between g.lowsal and

g.higsal)) t group by deptno;

99年sql标准:
select ename,sal,grade from emp e join grade g on (e.sal between g.lowsal and g.higsal);

自连接:在同一张表进行操作
select e1.ename,e2.ename from emp e1 join emp e2 on (e1.mgr=e2.empno);或者select e1.ename,e2.ename from emp e1,emp e2 where

e1.mgr=e2.empno;
左外连接:
select e.ename,d.loc from emp e left join dept d on (e.deptno=d.deptno);--左外连接会把左边那张表中没有关联到的字段也给查询

出来
右外连接:
select e.ename,d.loc from emp e right join dept d on(e.deptno=d.deptno);--右外连接会把右边那张表中没有关联到的字段也给查询

出来
全外连接:
select e.ename,d.loc from emp e full join dept d on(e.deptno=d.deptno); --全连接会把左右两边表中没有关联到的字段都给查询出

7:--查询所有的经理人
select ename from emp where empno in(select distinct mgr from emp);

8:--不使用组函数,查询薪水的最高值(先按照薪水排序,然后查询第一条)
select sal from (select sal from emp order by sal desc) where rownum=1;
或者
select e1.sal from emp where sal not in (select e1.sal from emp e1 join emp e2 on (e1.sal<e2.sal)); --自连接表,那么表e1中

只有最大值不会关联到

9:--求平均薪水最高的部门的名称
1:--求各部门平均薪水
select deptno,avg(sal) from emp group by deptno;
2: --求平均薪水最高为多少
select max(avg_sal) from (select deptno,avg(sal) avg_sal from emp group by deptno);
3: --求平均薪水最高部门的编号
select deptno from (select deptno,avg(sal) avg_sal from emp group by deptno) where avg_sal =(select max(avg_sal) from

(select deptno,avg(sal) avg_sal from emp group by deptno));
4:--求平均薪水最高部门的名称
select dname from dept where deptno = (select deptno from (select deptno,avg(sal) avg_sal from emp group by deptno) where

avg_sal =(select max(avg_sal) from (select deptno,avg(sal) avg_sal from emp group by deptno)));

10: --求平均薪水等级最低的部门名称
1:--求各部门平均薪水
select avg(sal),deptno from emp group by deptno;
2:--求各部门平均薪水等级
select deptno,avg_sal,grade from (select avg(sal) avg_sal,deptno from emp group by deptno) t join grade g on(t.avg_sal

between g.lowsal and g.higsal);
3:--求平均薪水等级最低
select min(grade) from (select grade from (select avg(sal) avg_sal,deptno from emp group by deptno) t join grade g on

(t.avg_sal between g.lowsal and g.higsal));
4:--求平均薪水等级最低的部门编号
select deptno from (select deptno,avg_sal,grade from (select avg(sal) avg_sal,deptno from emp group by deptno) t join grade

g on(t.avg_sal between g.lowsal and g.higsal)) where grade =(select min(grade) from (select grade from (select avg(sal)

avg_sal,deptno from emp group by deptno) t join grade g on(t.avg_sal between g.lowsal and g.higsal)));

5:--求平均薪水等级最低的部门编号
select dname from dept where deptno in (select deptno from (select deptno,avg_sal,grade from (select avg(sal)

avg_sal,deptno from emp group by deptno) t join grade g on(t.avg_sal between g.lowsal and g.higsal)) where grade =(select

min(grade) from (select grade from (select avg(sal) avg_sal,deptno from emp group by deptno) t join grade g on(t.avg_sal

between g.lowsal and g.higsal))));

11:oracle数据库用户切换
conn sys/root(用户名/密码) as sysdba;

授权语句:
grant create table,create view to system(用户名);
grant select,update,insert on emp to hr; --一次可以授权多种操作,但是只能针对一张表

12:--求比普通员工最高工资还要高的经理人
1:求普通员工的最高工资
select max(sal) from emp where empno not in(select distinct mgr from emp where mgr is not null)
2:求还要高的经理人
select ename from emp where empno in (select distinct mgr from emp where mgr is not null) and sal > (select max(sal) from

emp where empno not in(select distinct mgr from emp where mgr is not null));

13:--求薪水最高的前2名员工
select ename,sal from (select ename,sal from emp order by sal desc) where rownum <3;

14:rownum 子查询,实现分页,首先是排序,然后是查询rownum,第三步限定条件
select ename,sal from
(select ename,sal,rownum r from
(select ename,sal from emp order by sal desc)) where r>=2 and r<=3

对比:
mysql:select ename,sal from (select ename,sal from emp order by sal desc) t where id limit 2,2;--从2开始取,取2条
db2: 使用fetch

13:dml语句(数据操纵语句)
update emp set ename='tom' where empno=1001;
delete from emp where empno=1002;
insert into emp (empno,deptno) values(1005,20);

14:事务控制(dcl语句)
a:commit提交 rollback回滚 transcation事务结束
b:当执行ddl语句之后,之前的事务也会被提交
c:正常的退出连接,exit,也会自动提交事务

oracle学习总结2的更多相关文章

  1. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  2. Oracle学习线路

    出自huyangg的博客,地址是:oracle学习路线图 1.sql.pl/sql(网上有很多的视频,可以做一个简单的入手,然后看几本书,多做实验)    作为oracle的基本功,需要大家对sql和 ...

  3. Oracle学习指南

    Oracle学习指南 你走的那天,我决定不落泪,迎着风撑着眼帘用力不眨眼 创建数据库.创建用户.创建表空间.创建表.插入数据..... 1.用系统用户登录,任选系统用户 代码: >>sql ...

  4. Oracle学习系列1-7

    Oracle学习系列1 两个服务必须启动: OracleOraDb10g*TNListener 和 OracleService*** 使用sqlplusw先进行环境的设置 set linesize 3 ...

  5. Oracle学习系列7

    Oracle学习系列7 ************************************************************************************ 关联表 ...

  6. Oracle学习系列6

    Oracle学习系列6 ************************************************************************************ 删除约 ...

  7. Oracle学习系列5

    Oracle学习系列5 ************************************************************************************ ,掌握 ...

  8. Oracle学习系列4

    Oracle学习系列4 ************************************************************************************ 数据库 ...

  9. Oracle学习系列3

    Oracle学习系列3 ************************************************************************************ 多表查 ...

  10. oracle学习笔记(一)用户管理

    --oracle学习第一天 --连接 @后面连接数据库实例,具体连接到那个数据库 conn scott/tiger@MYORA1; --修改密码 passw; --显示用户 show user; -- ...

随机推荐

  1. HDU4289 Control 最大流

    经典题,求去掉若干个点,使得两个点不在连通,总价值最少 所以拆点最小割,除了拆点边,流量都为无穷,拆点边是流量为价值 #include <iostream> #include <cs ...

  2. 软件测试模型汇总-V模型,W模型,X模型,H模型

    V模型 在软件测试方面,V模型是最广为人知的模型,尽管很多富有实际经验的测试人员还是不太熟悉V模型,或者其它的模型.V模型已存在了很长时间,和瀑布开发模型有着一些共同的特性,由此也和瀑布模型一样地受到 ...

  3. 数组(Array)

    1. 数组(Array):相同类型数据的集合就叫做数组. 2. 数组的定义与赋值(系统会默认初始化) 普通数组: package com.li; public class Array{ public ...

  4. leetcode@ [79/140] Trie树应用 Word Search / Word Search II

    https://leetcode.com/problems/word-search/ class Solution { public: struct Trie{ Trie *next[]; bool ...

  5. Linux 中/etc/profile、~/.bash_profile 等几个环境配置文件的执行过程

    环境变量是和Shell紧密相关的,用户登录系统后就启动了一个Shell.对于Linux来说一般是bash,但也可以重新设定或切换到其它的 Shell.对于UNIX,可能是CShelll.环境变量是通过 ...

  6. Android下NFC的简单使用

    现在很多手机已经配备了NFC(Near Field Communication 近场通信)的功能,我就为此专门研究过,可以到本文末尾下载源代码. Android官方资料:http://develope ...

  7. Java异常的面试问题及答案-Part 1

    本文由 ImportNew - 韩远青 翻译自 Journaldev. Java提供了一个健壮的.面向对象的方法来处理出现异常,称为Java异常处理.我以前写过一篇长文章来介绍Java异常处理,今天我 ...

  8. Android实例-监测网络状态及一些事件(XE8+小米2)

    结果: 1.网络连接:是指现在可不可以上网(你非要问我什么是网,我会K你呀的). 2.WIFI网络:是指现在可以上网,用的是不是WIFI网络(如果你打开了WIFI那它会显示正在使用WIFI). 3.移 ...

  9. jquery中 cache: true和false的区别

    true:会读缓存,可能真的到服务器上. 假如上次访问了a.html,第二次的时候得到的是上次访问的a.html的结果,而不是重新到服务器获取. false:会在url后面加一个时间缀,让它跑到服务器 ...

  10. Android的事件处理

    1 android事件处理概述 不论是桌面应用还是手机应用程序,面对最多的就是用户,经常需要处理用户的动作-------也就是需要为用户动作提供响应,这种为用户动作提供响应的机制就是事件处理.andr ...