Oracle数据库语句练习

select userenv('language') from dual;--AMERICAN_AMERICA.ZHS16GBK
select * from V$NLS_PARAMETERS--AMERICAN

–===========解锁scott用户并重新设置密码

alter user scott account unlock;
alter user scott identified by tiger;

–===========基本查询
–1.查询出所有emp中的信息,并用中文进行字段重命名

select * from emp;

–2.查询emp表中员工的job信息,并去除重复信息

select distinct( job) from emp;

–3.查询emp表中员工的全年的工资总和(sal总和)

select ename, sal*12 from emp;

–4.查询emp表中员工的全年收入总和(sal+comm的总和)

select 12*(sal+nvl(comm,0)) from emp;  --nvl 遇到空值取0 或者自定义写1,2,3,4

–5.查询emp表中员工编号,姓名

select empno,ename from emp;

–输出格式如下:编号:xxx,姓名:xxx

----Concat拼接方式
select concat( concat('编号:',empno),concat(',姓名:',ename)) from emp;
----Oracle的||方式
select '编号'||empno||',姓名'||ename from emp;

–=============================================条件查询
–1.查询工资大于1500的员工

select * from emp where sal>1500;

–2.查询工资大于1500并且有奖金的雇员

select * from emp where sal>1500 and comm>0;

–3.查询工资大于1500或者有奖金的雇员

select * from emp where sal>1500 or comm is not null;

–4.查询工资大于1500并且没有奖金的雇员

select * from emp where sal>1500 and comm is null;

–5.查询员工姓名为smith的员工 应该改为大写的SMITH

select * from emp where ename='SMITH';

**

**–=============================================范围查询**

**

1.查询工资大于1500但小于3000的全部雇员
---->=,<=方式

select * from emp where sal<=3000 and sal>=1500;

----between and方式

select * from emp where sal between 1500 and 3000;

–2.查询1981-1-1到1981-12-31号入职的雇员(between and)

select * from emp where to_char( hiredate,'yyyy-mm-dd') between '1981-1-1' and '1981-12-31'; --这种方式查出来不全
select * from emp where  hiredate between to_date('1981-01-01','yyyy-mm-dd') and to_date('1981-12-31','yyyy-mm-dd');

–3.查询员工编号是7369,7654,7566的员工
----OR方式

select * from emp where empno=7369 or empno=7654 or empno=7566;

----IN方式

select * from emp where empno in (7369,7654,7566);

–4.查询雇员姓名是’SMITH’,‘ALLEN’,'WARD’的雇员信息
----IN方式

select * from emp where ename in ('SMITH','ALLEN','WARD');

–=============================================模糊查询like
–1.查询所有雇员姓名中第二个字符有‘M’的雇员

select * from emp where ename like '_M%';

–2.查询名字中带有‘M’的雇员

select * from emp where ename like '%M%';

–3.查询雇员编号不是7369的雇员信息
----<>方式

select * from emp where empno <> 7369;

----!=方式

select * from emp where empno != 7369;

–=============================================排序 order by
–1.查询雇员的工资进行降序排序

select * from emp  order by sal asc;  --降序desc     升序(默认)asc

–2.查询雇员的奖金并做降序排序(关于nulls first/nulls last)

select * from emp  order by comm desc nulls last; 

–3.查询雇员的工资做降序排列并且其中奖金部分是升序排序

select * from emp  order by sal desc , comm asc; 

**–===========单行函数**
/*
伪表,虚表:dual 没有任何的实际意义,只是为了补全Oracle查询语法
*/
–字符函数
–1.将’smith’转换成大写–关键字:upper

select upper('smith') from dual;

–2.将’SMITH’转换成小写–关键字:lower

select lower('SMITH') from dual;

–2.1 将emp表的name转换成小写–关键字:lower

select lower(ename) from emp;

–3.将’emp表名字的’首字母大写–关键字:initcap

select initcap(ename) from emp;

–4.将’helloworld’截取字符串成’hello’–关键字substr 此关键在 左右都闭合取值

select substr('helloworld',1,5) from dual;   

–5.获取’hello’的字符串长度–关键字length

select length('hello')  from dual;

–6.将’hello’中的l用x进行替换–关键字replace

select replace('hello','l','x') from dual;

–数值函数
–1.将15.66进行四舍五入(从-2到2)–关键字round

select round(15.56,-2) from dual; --0 保留-2位 也就是指到1了 舍去位0
select round(15.56,-1) from dual; --20
select round(15.56,0) from dual;  --16
select round(15.56,1) from dual;  --15.6
select round(15.56,2) from dual;  --15.56

–2.将15.66进行截断(从-2到2)–关键字trunc 截断的是小数点后面的位数 0 代表不要小数点 1代表保留一位小数(不舍入) 后面截断

select trunc(15.56,-2) from dual;
select trunc(15.56,-1) from dual;
select trunc(15.56,0) from dual;
select trunc(15.56,1) from dual;
select trunc(15.56,2) from dual;

–3.对15/3进行求余数–关键字mod

select  mod(15, 3) from dual;

–日期函数
–1.查询系统时间–关键字sysdate

select sysdate from dual;

–2.查询雇员进入公司的周数

select (sysdate-hiredate)/7 from emp;
select trunc( (sysdate-hiredate)/7,0 ) from emp;

–3.查询雇员进入公司的月数–关键字months_between

select   ename,months_between(sysdate,hiredate) from emp;

select  ename, trunc(months_between(sysdate,hiredate) )from emp;

–4.求出三个月后的日期–关键字add_months

select   add_months(sysdate,3) from dual;

–转换函数
–1.将系统日期显示为yyyy-mm-dd hh:mi:ss(去掉补零和24小时显示时间)–关键字to_char

select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
select to_char(sysdate,'yyyyfm-mm-dd hh24:mi:ss') from dual; --fm 去掉0 

hh24使用24小时格式

----显示成 年月日

select to_char(sysdate,'yyyy-mm-dd') from dual;
select to_char(sysdate,'yyyy')||'年'||to_char(sysdate,'mm')||'月'||to_char(sysdate,'dd')||'日' from dual;

–2.将字符串’1981-1-1’转换成日期类型–关键字to_date

select to_date('1981-1-1','yyyy-mm-dd') from dual;

–通用函数

--1.空值的处理函数
select ename,nvl(comm,0) from emp ;
--2.nvl2(判断值,空返回值,非空返回值)   判断一个值 是空返回0 不是空返回1
select nvl2(comm,0,1) from emp ;

–条件表达式
–1.查询员工的job内容并转成中文显示

----decode方式
select job from emp;
select ename,decode(job,'CLERK','柜员','SALESMAN','管理') from emp; --其他没有转换的使用默认空代替
select ename,decode(job,'CLERK','柜员','SALESMAN','管理','其他') from emp; --其他没用指定转换的使用 其他 代替

----case when then end方式

select ename,case job
        when 'CLERK' then '柜员' 
          when 'SALESMAN' then '管理' 
            else '其他' end 
              from emp;

**–===========多行函数**
–1.查询所有员工记录数–关键字count

select count(*) from emp;

–2.查询佣金的总数–(如何查询某个字段的总数量)

select sum(sal) from emp;

–3.查询最低工资–关键字min

select min(sal) from emp;

–4.查询最高工资–关键字max

select max(sal) from emp;

–5.查询平均工资–关键字avg

select avg(sal) from emp;
   --可以做个小数位保留操作

select round( avg(sal)) from emp;

–6.查询20号部门的员工工资总和 20号部门信息在去DEPT

select sum(sal) from emp where deptno=20;

**–======================================分组函数**
–1.查询部门编号及人数–分组查询关键字group by

select deptno,count(*) from emp group by deptno;

–2.查询每个部门编号及平均工资

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

–3.查询部门名称,部门编号,平均工资

select dept.dname ,dept.deptno,avg(sal) from dept,emp where dept.deptno=emp.deptno group by dname,dept.deptno;

–oracle规范 在前面出现过的普通字段 必须出现在group by后面

select dname ,dept.deptno,avg(sal) from dept,emp where dept.deptno=emp.deptno group by dname,dept.deptno;

–4.查询出部门人数大于5人的部门 使用dept表的主键 having 对group by做过滤

select deptno,count(*)  from emp  group by deptno  having count(*)>=5;

–5.查询部门编号,部门名称,平均工资且平均工资大于2000

select emp.deptno,dname ,avg(sal) from dept,emp where emp.deptno=dept.deptno  group by  emp.deptno, dname having avg(sal)>=2000;

版权声明:本文为YouCanDoIt_原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。