1. - oracle 函数
  2.  
  3. select sign(-3),sign(3), sign(0) from dual;
  4.  
  5. select ceil(3.7) from dual;
  6. select floor(3.7) from dual;
  7. -- 四舍五入
  8. select round(123.456, 2) from dual;
  9. select round(183.456, -2) from dual;
  10. select round(183.556) from dual;
  11.  
  12. select trunc(123.456, 2) from dual;
  13. select trunc(183.456, -2) from dual;
  14. select trunc(183.556) from dual;
  15.  
  16. -- length() 返回字符串长度
  17.  
  18. select ename, length(ename) from emp e;
  19.  
  20. -- 查询长度是6个字符的员工名字
  21. select ename from emp e where length(ename) = 6;
  22.  
  23. select ename from emp e where ename like '______';
  24.  
  25. -- 查询长度是6个字符并且以M开头的员工名字
  26. select ename from emp e where length(ename) = 6 and ename like 'M%';
  27.  
  28. select ename from emp e where ename like 'M_____';
  29.  
  30. select lower('abCdE') from dual;
  31. select upper('abCdE') from dual;
  32.  
  33. select * from emp where lower(ename) = 'scott';
  34.  
  35. -- sysdate 当前日期时间
  36. select sysdate from dual;
  37.  
  38. select sysdate, last_day(sysdate) from dual;
  39.  
  40. -- to_date()
  41. select to_date('20170711', 'YYYYMMDD') from dual;
  42. select to_date('20170711 18:20:45', 'YYYYMMDD hh24:mi:ss') from dual;
  43.  
  44. -- to_char()
  45. select to_char(sysdate, 'mm') from dual;
  46.  
  47. select * from emp e;
  48.  
  49. -- 查询12月份入职的员工信息
  50. select * from emp where to_char(hiredate, 'mm') = '12';
  51.  
  52. -- 分组函数
  53. -- max() 最大值
  54. -- min() 最小值
  55. -- avg() 平均值
  56. -- sum() 求和
  57. -- count() 数目
  58.  
  59. -- 分组函数有两种用法
  60. --1. 单独使用
  61. -- 查询10号部门最高工资
  62. select max(e.sal) from emp e where e.deptno = 10;
  63.  
  64. -- 查询10号部门最高工资、最低工资
  65. select max(e.sal), min(e.sal) from emp e where e.deptno = 10;
  66.  
  67. select sum(e.sal),count(e.sal) from emp e;
  68.  
  69. -- 查询工资高于30号部门所有人的员工信息
  70. select * from emp where sal > (select max(sal) from emp where deptno = 30);
  71.  
  72. -- 查询20号部门的员工数目
  73. select count(empno) from emp where deptno=20;
  74.  
  75. select count(*) from emp where deptno=20;
  76.  
  77. -- 查询所有销售(SALESMAN)的最低工资
  78. select min(e.sal) from emp e where e.job = 'SALESMAN';
  79.  
  80. -- 查询30号部门工资最高员工名字
  81.  
  82. select ename
  83. from emp
  84. where deptno = 30
  85. and sal = (select max(sal) from emp where deptno = 30);
  86.  
  87. --2. 配合group by一起使用
  88.  
  89. -- 分组: group by
  90.  
  91. -- 查询每个部门的最高工资,显示部门编号和最高工资
  92. select deptno, max(sal) from emp group by deptno;
  93.  
  94. -- (分组之后)筛选组: having ...
  95. -- 查询最高工资超过2900的部门,显示部门编号和最高工资
  96. select deptno, max(sal) from emp group by deptno having max(sal) > 2900;
  97.  
  98. -- 查询最高工资超过2900的部门,显示部门编号和最高工资
  99. -- 按照最高工资降序排序
  100. select deptno, max(sal)
  101. from emp
  102. group by deptno
  103. having max(sal) > 2900
  104. order by max(sal) desc;
  105.  
  106. -- 查询语句骨架
  107.  
  108. select xxx from xxx
  109. [where ...]
  110. [group by ...]
  111. [having ...]
  112. [order by ... [asc|desc]]
  113.  
  114. --表连接查询
  115.  
  116. create table stu(
  117. sno number(3),
  118. sname varchar2(20),
  119. sclass varchar2(10)
  120. );
  121.  
  122. create table cls(
  123. cid varchar2(10),
  124. ctype varchar2(20)
  125. );
  126.  
  127. insert into stu values(1, 'tom', 'c01');
  128. insert into stu values(2, 'mary', 'c02');
  129. insert into stu values(3, 'jack', 'c05');
  130. commit;
  131.  
  132. insert into cls values('c01', '测试');
  133. insert into cls values('c02', '测试');
  134. insert into cls values('c03', '开发');
  135. commit;
  136.  
  137. select * from stu;
  138. select * from cls;
  139.  
  140. -- 查询结果是两张表的笛卡尔积
  141. select * from stu, cls;
  142.  
  143. -- 内连接
  144. select * from stu s inner join cls c on s.sclass = c.cid;
  145. select * from stu s join cls c on s.sclass = c.cid;
  146.  
  147. select * from stu s, cls c where s.sclass = c.cid;
  148.  
  149. -- 左连接
  150. select * from stu s left join cls c on s.sclass = c.cid;
  151. select * from stu s, cls c where s.sclass = c.cid(+);
  152.  
  153. -- 右连接
  154. select * from stu s right join cls c on s.sclass = c.cid;
  155. select * from stu s, cls c where s.sclass(+) = c.cid;
  156.  
  157. -- 全连接
  158. select * from stu s full join cls c on s.sclass = c.cid;
  159.  
  160. select * from emp e;
  161. select * from dept d;
  162.  
  163. -- 查询所有员工的名字和所属部门名字
  164. select e.ename, d.dname from emp e, dept d where e.deptno=d.deptno;
  165.  
  166. select e.ename, (select d.dname from dept d where d.deptno = e.deptno) dname
  167. from emp e;
  168.  
  169. -- 查询ALLEN的所属部门名字
  170. select d.dname
  171. from emp e, dept d
  172. where e.deptno = d.deptno
  173. and e.ename = 'ALLEN';
  174.  
  175. -- 查询销售部(SALES)所有员工名字
  176. select e.ename
  177. from emp e, dept d
  178. where e.deptno = d.deptno
  179. and d.dname = 'SALES';
  180.  
  181. select ename
  182. from emp
  183. where deptno = (select deptno from dept where dname = 'SALES');
  184.  
  185. -- 查询工资超过2000的员工姓名和上班地点
  186. select e.ename, d.loc
  187. from emp e, dept d
  188. where e.deptno = d.deptno
  189. and e.sal > 2000;
  190.  
  191. -- 查询所有员工姓名和其主管姓名,没有主管的主管姓名为空
  192. select a.ename,b.ename from emp a, emp b where a.mgr = b.empno(+);
  193.  
  194. -- 查询ALLEN的主管姓名
  195. select b.ename
  196. from emp a, emp b
  197. where a.mgr = b.empno
  198. and a.ename = 'ALLEN';
  199.  
  200. -- 查询入职时间早于其主管的员工姓名
  201. select a.ename
  202. from emp a, emp b
  203. where a.mgr = b.empno
  204. and a.hiredate < b.hiredate;
  205.  
  206. -- 查询每个部门的最高工资,显示部门编号和最高工资
  207. select e.deptno,max(e.sal) from emp e group by e.deptno;
  208.  
  209. -- 查询每个部门的最高工资,显示部门名字和最高工资
  210. select d.dname, max(e.sal)
  211. from emp e, dept d
  212. where e.deptno = d.deptno
  213. group by d.dname;
  214.  
  215. -- 查询最高工资超过2900的部门,显示部门名字和最高工资
  216. select d.dname, max(e.sal)
  217. from emp e, dept d
  218. where e.deptno = d.deptno
  219. group by d.dname
  220. having max(e.sal) > 2900;
  221.  
  222. -- 查询最高工资超过2900的部门,显示部门名字和最高工资,按照最高工资升序排序
  223. select d.dname, max(e.sal)
  224. from emp e, dept d
  225. where e.deptno = d.deptno
  226. group by d.dname
  227. having max(e.sal) > 2900
  228. order by max(e.sal) asc;
  229.  
  230. -- 查询员工数目超过2个的职位,显示职位和员工数目,按照员工数目降序排序
  231. select e.job, count(*)
  232. from emp e
  233. group by e.job
  234. having count(*) > 2
  235. order by count(*) desc;
  236.  
  237. -- 查询工资高于平均工资的员工信息
  238. select * from emp where sal > (select avg(sal) from emp);
  239.  
  240. -- 查询工资高于本部门平均工资的员工信息
  241. -- 方法1
  242. select *
  243. from emp e
  244. where e.sal > (select avg(a.sal) from emp a where a.deptno = e.deptno);
  245.  
  246. -- 方法2
  247.  
  248. select e.*
  249. from emp e, (select deptno, avg(sal) av from emp group by deptno) a
  250. where e.deptno = a.deptno
  251. and e.sal > a.av;
  252.  
  253. -- 查询每个部门的编号、名字和员工数目
  254.  
  255. select d.deptno, d.dname, a.c
  256. from dept d, (select e.deptno, count(*) c from emp e group by e.deptno) a
  257. where d.deptno = a.deptno(+);
  258.  
  259. select d.deptno, d.dname, nvl(a.c, 0)
  260. from dept d, (select e.deptno, count(*) c from emp e group by e.deptno) a
  261. where d.deptno = a.deptno(+);
  262.  
  263. -- nvl() 为空值赋值函数
  264. select ename,sal,nvl(comm, 0) from emp;
  265.  
  266. -- 查询每个部门工资最高的员工姓名
  267.  
  268. select e.ename
  269. from emp e, (select deptno, max(sal) m from emp group by deptno) a
  270. where e.deptno = a.deptno
  271. and e.sal = a.m;

oracle 常用语句3的更多相关文章

  1. ORACLE常用语句:

    ORACLE常用语句: 1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新 ...

  2. Oracle常用语句集合

    oracle常用经典SQL查询 常用SQL查询: .查看表空间的名称及大小 )),) ts_size from dba_tablespaces t, dba_data_files d where t. ...

  3. Oracle常用语句

    Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, ...

  4. Oracle常用语句语法汇总

    第一篇  基本操作 --解锁用户   alter user 用户 account unlock; --锁定用户   alter user 用户 account lock; alter user sco ...

  5. oracle常用语句总结

    一.用户管理类 1.创建用户: Create user username Identified by password Default tablespace tablespacename Tempor ...

  6. oracle 常用语句

    创建用户及授权create temporary tablespace test_temp tempfile 'C:\oracle\product\10.2.0\oradata\hszxdbtemp.d ...

  7. Oracle 常用语句1

    -- 我是注释信息 sql语句 -- 创建用户: create user 用户名 identified by 密码; create user jack identified by j123; -- l ...

  8. 查锁住的表,以及kill进程,Oracle常用语句

    --找出所有被锁的对象,定位出哪个回话占用 select l.session_id,o.owner,o.object_name from v$locked_object l,dba_objects o ...

  9. Oracle 常用语句备份

    1.oracle 11g 用户名和密码默认区分大小写,可更改alter system set sec_case_sensitive_logon=false 设置改为不区分大小写. 2.授权创建视图:G ...

随机推荐

  1. 用MathType怎么把分数打出来

    分数是生活中最常见的数,作为大学生学习高数概率论更是离不开分数.分数是指整体的一部分,或更一般地,任何数量相等的部分.分数是一个整数a和一个正整数b的不等于整数的比. 当在日常用语中说话时,分数描述了 ...

  2. jQuery 第二章 实例方法 DOM操作选择元素相关方法

    进一步选择元素相关方法:  .get() .eq() .find() .filter() .not() .is() .has() .add()集中操作  .end()回退操作 .get() $(&qu ...

  3. api-hook,更轻量的接口测试工具

    前言 在网站的开发过程中,接口联调和测试是至关重要的一环,其直接影响产品的核心价值,而目前也有许多技术方案和工具加持,让我们的开发测试工作更加便捷.接口作为数据传输的重要载体,数据格式和内容具有多样性 ...

  4. 基于gin的golang web开发:永远不要相信用户的输入

    作为后端开发者我们要记住一句话:"永远不要相信用户的输入",这里所说的用户可能是人,也可能是另一个应用程序."永远不要相信用户的输入"是安全编码的准则,也就是说 ...

  5. 【mq读书笔记】mq消息消费

    消息消费以组的的模式开展: 一个消费组内可以包含多个消费者,每一个消费组可订阅多个主题: 消费组之间有集群模式与广播模式两种消费模式:集群模式-主题下的同一条消息只允许被其中一个消费者消费.广播模式- ...

  6. 【mq学习笔记】mq 过期文件删除机制

    broker不会关注这个文件上的消息是否全部被消费.默认每个文件的过期时间为72小时.

  7. python应用(7):输入与输出

    如前文,流程是程序的主角,而流程一般都需要处理数据,那数据如何进到流程,而最终处理后的数据又如何表现,这就是流程的输入与输出的问题. 本文介绍流程处理的数据的输入与输出. 流程中的输入,一般都会先保存 ...

  8. 5.3 Spring5源码--Spring AOP使用接口方式实现

    Spring 提供了很多的实现AOP的方式:Spring 接口方式,schema配置方式和注解. 本文重点介绍Spring使用接口方式实现AOP. 使用接口方式实现AOP以了解为目的. 更好地理解动态 ...

  9. PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中使用findItems搜索项

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在QTreeWidget类实例的树型部件中,可以根据文本.搜索列以及匹配模式来搜索满足条件的项,调用 ...

  10. PyQt(Python+Qt)学习随笔:QListWidget插入多项的insertItems方法

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 除了insertItem方法能插入项外,QListWidget支持一次插入多个项,对应的方法就是in ...