oracle 常用语句3
- - oracle 函数
- select sign(-3),sign(3), sign(0) from dual;
- select ceil(3.7) from dual;
- select floor(3.7) from dual;
- -- 四舍五入
- select round(123.456, 2) from dual;
- select round(183.456, -2) from dual;
- select round(183.556) from dual;
- select trunc(123.456, 2) from dual;
- select trunc(183.456, -2) from dual;
- select trunc(183.556) from dual;
- -- length() 返回字符串长度
- select ename, length(ename) from emp e;
- -- 查询长度是6个字符的员工名字
- select ename from emp e where length(ename) = 6;
- select ename from emp e where ename like '______';
- -- 查询长度是6个字符并且以M开头的员工名字
- select ename from emp e where length(ename) = 6 and ename like 'M%';
- select ename from emp e where ename like 'M_____';
- select lower('abCdE') from dual;
- select upper('abCdE') from dual;
- select * from emp where lower(ename) = 'scott';
- -- sysdate 当前日期时间
- select sysdate from dual;
- select sysdate, last_day(sysdate) from dual;
- -- to_date()
- select to_date('20170711', 'YYYYMMDD') from dual;
- select to_date('20170711 18:20:45', 'YYYYMMDD hh24:mi:ss') from dual;
- -- to_char()
- select to_char(sysdate, 'mm') from dual;
- select * from emp e;
- -- 查询12月份入职的员工信息
- select * from emp where to_char(hiredate, 'mm') = '12';
- -- 分组函数
- -- max() 最大值
- -- min() 最小值
- -- avg() 平均值
- -- sum() 求和
- -- count() 数目
- -- 分组函数有两种用法
- --1. 单独使用
- -- 查询10号部门最高工资
- select max(e.sal) from emp e where e.deptno = 10;
- -- 查询10号部门最高工资、最低工资
- select max(e.sal), min(e.sal) from emp e where e.deptno = 10;
- select sum(e.sal),count(e.sal) from emp e;
- -- 查询工资高于30号部门所有人的员工信息
- select * from emp where sal > (select max(sal) from emp where deptno = 30);
- -- 查询20号部门的员工数目
- select count(empno) from emp where deptno=20;
- select count(*) from emp where deptno=20;
- -- 查询所有销售(SALESMAN)的最低工资
- select min(e.sal) from emp e where e.job = 'SALESMAN';
- -- 查询30号部门工资最高员工名字
- select ename
- from emp
- where deptno = 30
- and sal = (select max(sal) from emp where deptno = 30);
- --2. 配合group by一起使用
- -- 分组: group by 列
- -- 查询每个部门的最高工资,显示部门编号和最高工资
- select deptno, max(sal) from emp group by deptno;
- -- (分组之后)筛选组: having ...
- -- 查询最高工资超过2900的部门,显示部门编号和最高工资
- select deptno, max(sal) from emp group by deptno having max(sal) > 2900;
- -- 查询最高工资超过2900的部门,显示部门编号和最高工资
- -- 按照最高工资降序排序
- select deptno, max(sal)
- from emp
- group by deptno
- having max(sal) > 2900
- order by max(sal) desc;
- -- 查询语句骨架
- select xxx from xxx
- [where ...]
- [group by ...]
- [having ...]
- [order by ... [asc|desc]]
- --表连接查询
- create table stu(
- sno number(3),
- sname varchar2(20),
- sclass varchar2(10)
- );
- create table cls(
- cid varchar2(10),
- ctype varchar2(20)
- );
- insert into stu values(1, 'tom', 'c01');
- insert into stu values(2, 'mary', 'c02');
- insert into stu values(3, 'jack', 'c05');
- commit;
- insert into cls values('c01', '测试');
- insert into cls values('c02', '测试');
- insert into cls values('c03', '开发');
- commit;
- select * from stu;
- select * from cls;
- -- 查询结果是两张表的笛卡尔积
- select * from stu, cls;
- -- 内连接
- select * from stu s inner join cls c on s.sclass = c.cid;
- select * from stu s join cls c on s.sclass = c.cid;
- select * from stu s, cls c where s.sclass = c.cid;
- -- 左连接
- select * from stu s left join cls c on s.sclass = c.cid;
- select * from stu s, cls c where s.sclass = c.cid(+);
- -- 右连接
- select * from stu s right join cls c on s.sclass = c.cid;
- select * from stu s, cls c where s.sclass(+) = c.cid;
- -- 全连接
- select * from stu s full join cls c on s.sclass = c.cid;
- select * from emp e;
- select * from dept d;
- -- 查询所有员工的名字和所属部门名字
- select e.ename, d.dname from emp e, dept d where e.deptno=d.deptno;
- select e.ename, (select d.dname from dept d where d.deptno = e.deptno) dname
- from emp e;
- -- 查询ALLEN的所属部门名字
- select d.dname
- from emp e, dept d
- where e.deptno = d.deptno
- and e.ename = 'ALLEN';
- -- 查询销售部(SALES)所有员工名字
- select e.ename
- from emp e, dept d
- where e.deptno = d.deptno
- and d.dname = 'SALES';
- select ename
- from emp
- where deptno = (select deptno from dept where dname = 'SALES');
- -- 查询工资超过2000的员工姓名和上班地点
- select e.ename, d.loc
- from emp e, dept d
- where e.deptno = d.deptno
- and e.sal > 2000;
- -- 查询所有员工姓名和其主管姓名,没有主管的主管姓名为空
- select a.ename,b.ename from emp a, emp b where a.mgr = b.empno(+);
- -- 查询ALLEN的主管姓名
- select b.ename
- from emp a, emp b
- where a.mgr = b.empno
- and a.ename = 'ALLEN';
- -- 查询入职时间早于其主管的员工姓名
- select a.ename
- from emp a, emp b
- where a.mgr = b.empno
- and a.hiredate < b.hiredate;
- -- 查询每个部门的最高工资,显示部门编号和最高工资
- select e.deptno,max(e.sal) from emp e group by e.deptno;
- -- 查询每个部门的最高工资,显示部门名字和最高工资
- select d.dname, max(e.sal)
- from emp e, dept d
- where e.deptno = d.deptno
- group by d.dname;
- -- 查询最高工资超过2900的部门,显示部门名字和最高工资
- select d.dname, max(e.sal)
- from emp e, dept d
- where e.deptno = d.deptno
- group by d.dname
- having max(e.sal) > 2900;
- -- 查询最高工资超过2900的部门,显示部门名字和最高工资,按照最高工资升序排序
- select d.dname, max(e.sal)
- from emp e, dept d
- where e.deptno = d.deptno
- group by d.dname
- having max(e.sal) > 2900
- order by max(e.sal) asc;
- -- 查询员工数目超过2个的职位,显示职位和员工数目,按照员工数目降序排序
- select e.job, count(*)
- from emp e
- group by e.job
- having count(*) > 2
- order by count(*) desc;
- -- 查询工资高于平均工资的员工信息
- select * from emp where sal > (select avg(sal) from emp);
- -- 查询工资高于本部门平均工资的员工信息
- -- 方法1
- select *
- from emp e
- where e.sal > (select avg(a.sal) from emp a where a.deptno = e.deptno);
- -- 方法2
- select e.*
- from emp e, (select deptno, avg(sal) av from emp group by deptno) a
- where e.deptno = a.deptno
- and e.sal > a.av;
- -- 查询每个部门的编号、名字和员工数目
- select d.deptno, d.dname, a.c
- from dept d, (select e.deptno, count(*) c from emp e group by e.deptno) a
- where d.deptno = a.deptno(+);
- select d.deptno, d.dname, nvl(a.c, 0)
- from dept d, (select e.deptno, count(*) c from emp e group by e.deptno) a
- where d.deptno = a.deptno(+);
- -- nvl() 为空值赋值函数
- select ename,sal,nvl(comm, 0) from emp;
- -- 查询每个部门工资最高的员工姓名
- select e.ename
- from emp e, (select deptno, max(sal) m from emp group by deptno) a
- where e.deptno = a.deptno
- and e.sal = a.m;
oracle 常用语句3的更多相关文章
- ORACLE常用语句:
ORACLE常用语句: 1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新 ...
- Oracle常用语句集合
oracle常用经典SQL查询 常用SQL查询: .查看表空间的名称及大小 )),) ts_size from dba_tablespaces t, dba_data_files d where t. ...
- Oracle常用语句
Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, ...
- Oracle常用语句语法汇总
第一篇 基本操作 --解锁用户 alter user 用户 account unlock; --锁定用户 alter user 用户 account lock; alter user sco ...
- oracle常用语句总结
一.用户管理类 1.创建用户: Create user username Identified by password Default tablespace tablespacename Tempor ...
- oracle 常用语句
创建用户及授权create temporary tablespace test_temp tempfile 'C:\oracle\product\10.2.0\oradata\hszxdbtemp.d ...
- Oracle 常用语句1
-- 我是注释信息 sql语句 -- 创建用户: create user 用户名 identified by 密码; create user jack identified by j123; -- l ...
- 查锁住的表,以及kill进程,Oracle常用语句
--找出所有被锁的对象,定位出哪个回话占用 select l.session_id,o.owner,o.object_name from v$locked_object l,dba_objects o ...
- Oracle 常用语句备份
1.oracle 11g 用户名和密码默认区分大小写,可更改alter system set sec_case_sensitive_logon=false 设置改为不区分大小写. 2.授权创建视图:G ...
随机推荐
- 用MathType怎么把分数打出来
分数是生活中最常见的数,作为大学生学习高数概率论更是离不开分数.分数是指整体的一部分,或更一般地,任何数量相等的部分.分数是一个整数a和一个正整数b的不等于整数的比. 当在日常用语中说话时,分数描述了 ...
- jQuery 第二章 实例方法 DOM操作选择元素相关方法
进一步选择元素相关方法: .get() .eq() .find() .filter() .not() .is() .has() .add()集中操作 .end()回退操作 .get() $(&qu ...
- api-hook,更轻量的接口测试工具
前言 在网站的开发过程中,接口联调和测试是至关重要的一环,其直接影响产品的核心价值,而目前也有许多技术方案和工具加持,让我们的开发测试工作更加便捷.接口作为数据传输的重要载体,数据格式和内容具有多样性 ...
- 基于gin的golang web开发:永远不要相信用户的输入
作为后端开发者我们要记住一句话:"永远不要相信用户的输入",这里所说的用户可能是人,也可能是另一个应用程序."永远不要相信用户的输入"是安全编码的准则,也就是说 ...
- 【mq读书笔记】mq消息消费
消息消费以组的的模式开展: 一个消费组内可以包含多个消费者,每一个消费组可订阅多个主题: 消费组之间有集群模式与广播模式两种消费模式:集群模式-主题下的同一条消息只允许被其中一个消费者消费.广播模式- ...
- 【mq学习笔记】mq 过期文件删除机制
broker不会关注这个文件上的消息是否全部被消费.默认每个文件的过期时间为72小时.
- python应用(7):输入与输出
如前文,流程是程序的主角,而流程一般都需要处理数据,那数据如何进到流程,而最终处理后的数据又如何表现,这就是流程的输入与输出的问题. 本文介绍流程处理的数据的输入与输出. 流程中的输入,一般都会先保存 ...
- 5.3 Spring5源码--Spring AOP使用接口方式实现
Spring 提供了很多的实现AOP的方式:Spring 接口方式,schema配置方式和注解. 本文重点介绍Spring使用接口方式实现AOP. 使用接口方式实现AOP以了解为目的. 更好地理解动态 ...
- PyQt(Python+Qt)学习随笔:树型部件QTreeWidget中使用findItems搜索项
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 在QTreeWidget类实例的树型部件中,可以根据文本.搜索列以及匹配模式来搜索满足条件的项,调用 ...
- PyQt(Python+Qt)学习随笔:QListWidget插入多项的insertItems方法
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 除了insertItem方法能插入项外,QListWidget支持一次插入多个项,对应的方法就是in ...