zoukankan      html  css  js  c++  java
  • Oracle---day01

    一、简单查询语句

     1.去重查询,和mysql的一样
      select distinct job from emp;
      select distinct job, deptno from emp;  --去除job相等且deptno相等的结果

     2.查询员工年薪
      select ename 姓名, sal 月薪, sal*12 年薪 from emp;

     3.字符串拼接
      select ‘姓名’ || ename from emp;  --Oracle特有的拼接方式
      select concat('姓名', ename) from emp;  --Mysql和Oracle都可以使用

     4.查询能得到奖金的员工信息
      select * from emp where comm is not null;

     5.查询工资在1500~3000之间的员工信息
      select * from emp where sal between 1500 and 3000;

     6.查询姓名在某个范围类的员工信息
      select * from emp where ename in ('WARD','SCOTT','KING');

     7.查询姓名的第三个字母为o的员工信息
      select * from emp where ename like '__0%';

     8.查询部门编号和工资,按照编号升序,工资降序排列
      select deptno, sal from emp order by deptno asc, sal desc;

     二、函数

     1.查询工资和
      select sum(sal) from emp;

     2.查询平均工资
      select avg(sal) from emp;

     3.数值函数向上取整、向下取整和四舍五入、截断
      select ceil(45.887) from dual;  --向上取整  46
      select floor(45.887) from dual;    --向下取整  45

      
    select round(45.826, 2) from dual; --45.83  --四舍五入   参数2 为保留小数位
      select round(45.826, 1) from dual; --45.8
      select round(45.826, 0) from dual; --46
      select round(45.826, -1) from dual; --50
      select round(45.826, -2) from dual; --0

      select trunc(45.826, 2) from dual; --45.82  --截断
      select trunc(45.826, 1) from dual; --45.8
      select trunc(45.826, 0) from dual; --45
      select trunc(45.826, -1) from dual; --40
      select trunc(45.826, -2) from dual; --0

     4.字符串的截取
      select substr('hello_world', 3, 4)  --表示从第3个字母开始截取4个字母   llo_

     5.日期函数
      select sysdate from dual;  --当前时间
      select months_between(sysdate, hiredate) from emp;  --查询员工入职到现在的月数

     6.转换函数
      字符转数值 to_number(str)
      select to_number('123') from dual;
      日期转字符串 to_char
      select to_char(hiredate, 'yyyy-mm-dd hh:mi:ss') from emp;
      select to_char(sysdate, 'd') from dual;   --一周的第几天
      select to_char(sysdate, 'dd') from dual;    --一个月的第几天
      select to_char(sysdate, 'ddd') from dual;   --一年的第几天
      字符串转日期 --to_date
      select * from emp where hiredate between to_date(1981, 'yyyy') and to_date(1986, 'yyyy');  --查询在1981~1986年之间入职的员工信息

     7.通用函数,nvl、nvl2、nullif
      nvl(arg1, arg2)     --如果arg1=null,返回arg2
      nvl2(arg1, arg2, arg3) -- 如果arg1=null,返回arg3,否则返回arg2 
      nullif(arg1, arg2)       -- 如果arg1==arg2,返回null,否则返回arg1
      select nvl2(comm, 1, 0) from emp  --查询员工是否有奖金

     8.条件表达式
      select      --这种方法在Oracle和Mysql都适用
       case deptno
       when 10 then '10号部门'
       when 20 then '20号部门'
       when 30 then '30号部门’
      end from emp;

      select decode(deptno,10, '10号部门', 20, '20号部门', 30, '30号部门','其他部门') from emp;  --Oracle特有的写法

    三、练习

     1.查询工资大于12000的员工姓名和工资
      select first_name,salary from employees where salary > 12000;

     2.查询员工号为176的员工的姓名和部门号
      select first_name, department_id from employees where employee_id=176;

     3.选择工资不在5000到12000的员工的姓名和工资
      select frist_name,salary from employees where salary not between 5000 and 12000;

     4.选择雇用时间在1998-02-01到1998-05-01之间的员工姓名,job_id和雇用时间
      select first_name, job_id, hire_date from employees
      where hire_date between to_date('1998-02-01', 'yyyy-mm--dd') and to_date('1998-05-01', 'yyyy-mm-dd');

     5.选择在20或50号部门工作的员工姓名和部门号
      select first_name, department_id from employees where department_id=20 or department_id=50;

     6.选择在1994年雇用的员工的姓名和雇用时间
      select first_name, hire_date from employees where to_char(hire_date, 'yyyy') = '1994';

     7.选择公司中没有管理者的员工姓名及job_id
      select first_name, job_id from employees where manager_id is null;

     8.选择公司中有奖金的员工姓名,工资和奖金级别
      select first_name, salary, commission_pct from employees where commission_pct is not null

     9.选择员工姓名的第三个字母是a的员工姓名
      select first_name from employees where first_name like '__a%';

     10.选择姓名中有字母a和e的员工姓名
      select first_name from employees where first_name like '%a%' and first_name like '%e%';

     11.显示系统时间
      select sysdate from dual;

     12.查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new?salary)
      select employee_id, first_name, salary, salary+salary*0.2 "new salary" from employees;

     13.将员工的姓名按首字母排序,并写出姓名的长度(length)
      select first_name, length(first_name) from employees order by first_name;

     14.查询各员工的姓名,并显示出各员工在公司工作的月份数
      select first_name, months_between(sysdate,hire_date) from employees;

     15.查询员工的姓名,以及在公司工作的月份数(worked_month),并按月份数降序排列
      select first_name, months_between(sysdate, hire_date) "worked_months" from employees order by months_between(sysdate, hiredate) desc;

     

  • 相关阅读:
    2017-2018-1 20155214 实验五 通讯协议设计
    2017-2018-1 20155214 《信息安全系统设计基础》第13周学习总结
    2017-2018-1 20155214 《信息安全系统设计基础》 第9周课下测试-mypwd
    2017-2018-1 20155214 《信息安全系统设计基础》 第11周学习总结
    2017-2018-1 20155214 20155335《信息安全技术基础》 实验3
    2017-2018-1 20155214 《信息安全系统设计基础》实验三 并发程序
    2017-2018-1 20155214 《信息安全系统设计基础》 第9周学习总结
    2017-2018-1 20155214 《信息安全系统设计基础》 第8周学习总结
    2017-2018-1 20155214 《信息安全系统设计基础》 第8周课下作业
    2017-2018-1 20155214 《信息安全系统设计基础》 第7周学习总结
  • 原文地址:https://www.cnblogs.com/zy-Luo/p/11520021.html
Copyright © 2011-2022 走看看