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;

原文地址:https://www.cnblogs.com/guog1/p/8508308.html

时间: 2024-11-08 22:24:23

oracle 常用语句3的相关文章

Oracle 常用语句整理

Oracle 常用语句整理 最近做了份大型数据库作业.遇到了一些问题,在网上找的很是辛苦,于是,将一些常用的语句记录下来,方便大家学习.都是一些基本的东西.如果忘了,可以来看看. 1.创建临时表空间 create temporary tablespace car_data //car_data,表空间名 tempfile 'C:\Users\Administrator\Desktop\car_data.dbf'//表空间路径 size 50m autoextend on next 50m max

oracle 常用语句

1.decode 函数(小版本的case when) select  decode(pd.discount_id,null,'','购买'||pd.product_count||'个,'||pd.product_discount_rate||'折优惠') as discount from b2b_product d right join b2b_product_hot ph on d.product_id = ph.product_id  left join b2b_dictionary aon

整理的Oracle常用语句

3个默认用户 sys change_on install [as sysdba] system manager scott tiger创建用户 create user 用户名 identified by 密码 defaul tablespace 表空间(Users) temporary tablespace 表空间(Temp) quota 整数 on 表空间(Users);--quota(限额) 临时表空间不能使用限额修改密码 alter user scott identified by tig

Oracle 常用语句1

-- 我是注释信息 sql语句 -- 创建用户: create user 用户名 identified by 密码; create user jack identified by j123; -- lacks CREATE SESSION priviledge 用户没有权限连接数据库 -- 授权用户: grant 权限1,权限2 to 用户名; -- 管理员 dba -- 普通用户 connect,resource grant connect,resource to jack; -- 收回权限:

Oracle常用语句

1.查看某个字段在哪张表 select owner, table_name from dba_tab_columns where lower(column_name)='字段名'; 2.导出序列 SELECT ' create sequence username.' || SEQUENCE_NAME ||          ' minvalue ' || MIN_VALUE ||         ' maxvalue ' || MAX_VALUE ||         ' start with

ORACLE常用语句合集

查询库中所有表空间: select tablespace_name from dba_tablespaces; select tablespace_name from user_tablespaces; 查询使用过的表空间: select distinct tablespace_name from dba_all_tables; select distinct tablespace_name from user_all_tables; 查询表空间中所有表名: select table_name

oracle 常用语句2

-- number(38) -- char(2000) -- varchar(4000) create table student( sno number(3) primary key, sname varchar2(40) default ('佚名'), sex char(2) check(sex in('男', '女', '中')), age number(2) check( age between 20 and 30 ), birthday date, sclass varchar2(10

oracle 常用相关sql 语句

一.oracle 常用相关sql 语句 1. 连接数据库       su - oracle -c " sqlsplus 用户/密码     注:首次登陆用  sqlplus / as sysdba            注: 关闭数据库:注:shutdown可加关闭选项,从最温和到最粗暴的行为选项为(shutdown.shutdown transactional.shutdown immediate.shutdown abort)                 shutdown:关闭,等待每

oracle常用SQL语句(汇总版)

Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象 一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSERT INTO 表名(字段名1, 字段名2, ……) SELECT (字段名1, 字段名2, ……) FROM 另外的表名; 字符串类型的字段值必须用单引号括起来, 例如: ’GOOD DAY’ 如果字段值里包含单引号’ 需要