zoukankan      html  css  js  c++  java
  • sql语句基本查询操作

    表结构

    SQL> desc emp
    Name Type Nullable Default Comments
    -------- ------------ -------- ------- --------
    EMPNO NUMBER(4)
    ENAME VARCHAR2(10) Y 员工姓名
    JOB VARCHAR2(9) Y
    MGR NUMBER(4) Y
    HIREDATE DATE Y
    SAL NUMBER(7,2) Y
    COMM NUMBER(7,2) Y
    DEPTNO NUMBER(2) Y

    查询语句

    --查询EMP表显示所有雇员名及其全年收入(月收入=工资+补助),处理NULL行,
    --并指定列别名为"年收入"。(NVL(comm,0) comm取空值时用0替代)

    select ename,12*(sal+nvl(comm,0)) 年收入 from emp;

    --)查询EMP表显示工资超过2850的雇员姓名和工资
    select ename,sal from emp where sal > 2850;

    --查询EMP表显示工资不在1500~2850之间的所有雇员及工资
    select ename,sal from emp where sal not between 1500 and 2850;

    --查询EMP表显示代码为7566的雇员姓名及所在部门代码
    select ename,deptno from emp where mgr=7566;

    --查询EMP表显示部门10和30中工资超过1500的雇员名及工资


    select ename,sal,deptno from emp where deptno in(10,30) and sal > 1500;
    select ename,sal,deptno from emp where (deptno=10 or deptno=30) and sal>1500;

    --查询EMP表显示第2个字符为"A"的所有雇员名其工资。
    select ename,sal from emp where ename like '_A%';

    select * from emp;

    insert into emp(empno,ename,sal,deptno) values(8888,'zhang%san',8888,30);

    select ename from emp where ename like '%x%%' escape('x');


    --查询EMP表显示补助非空的所有雇员名及其补助。
    select ename,comm from emp where comm is not null;

    --查询EMP表显示所有雇员名、工资、雇佣日期,并以雇员名的升序进行排序。
    select ename,sal,hiredate from emp order by ename asc;


    --查询EMP表显示在1981年2月1日到1981年5月1日之间雇佣的雇员名、岗位及雇佣日期,并
    --以雇佣日期进行排序。
    select ename, job, hiredate
    from emp
    where hiredate between to_date('1981-2-1', 'yyyy-mm-dd') and
    to_date('1981-5-1', 'yyyy-mm-dd')
    order by hiredate desc;

    --查询EMP表显示获得补助的所有雇员名、工资及补助,并以工资升序和补助降序排序
    select ename,sal,comm from emp where comm is not null order by sal asc,comm desc;

    --查询82年员工
    select ename, hiredate
    from emp
    where hiredate between to_date('1982-1-1', 'yy-mm-dd') and
    to_date('1982-12-31', 'yy-mm-dd');

    --查询32年工龄的人员
    select ename,hiredate,Months_between(sysdate,hiredate)/12 from emp where Months_between(sysdate,hiredate)/12 between 31 and 32;

    --显示员工雇佣期 6 个月后下一个星期一的日期
    select ename,hiredate,next_day(add_months(hiredate,6),'星期一') from emp;

    --找没有上级的员工,把mgr的字段信息输出为 "boss"
    select mgr,nvl(to_char(mgr),'boss') from emp;

    --为所有人长工资,标准是:10部门长10%;20部门长15%;30
    --部门长20%其他部门长18%
    select ename,
    sal,
    deptno,
    case
    when deptno = 10 then
    (sal * 0.1 + sal)
    when deptno = 20 then
    (sal * 0.15 + sal)
    when deptno = 30 then
    (sal * 0.2 + sal)
    else
    (sal * 0.18 + sal)
    end
    from emp;


    --查询10号部门中编号最新入职的员工,工龄最长的员工的个人信息。
    select *from emp where hiredate in
    ((select max(hiredate)from emp where deptno = 10),
    (select min(hiredate) from emp where deptno = 10
    ));

    select * from emp where deptno =10;

    --从“software”找到‘f’的位置,用‘*’左右填充到15位,去除其中的‘a’
    select instr('software','f') from dual;
    select lpad('software',15,'*') from dual;
    select rpad('software',15,'*') from dual;
    select replace('software','a') from dual;

    --查询员工的奖金,如果奖金不为NULL显示‘有奖金’,为null则显示无奖金
    select ename,comm,decode(comm,null,'无奖金','有奖金') from emp;

    --写一个查询显示当前日期,列标题显示为Date。再显示六个月后的日期,下一个
    --星期 日的日期,该月最后一天的日期。
    select sysdate "Date",add_Months(sysdate,6),next_day(sysdate,'星期日'),last_day(sysdate) from dual;

    -- 查询EMP表按管理者编号升序排列,如果管理者编号为空则把为空的在最前显示
    select * from emp order by mgr asc nulls first;

    --求部门平均薪水
    select deptno,avg(sal) from emp group by deptno;


    --找出每个部门的平均、最小、最大薪水
    select deptno,avg(sal),max(sal),min(sal) from emp group by deptno;
    select * from emp;

    --按部门求出工资大于1300人员的 部门编号、平均工资、最小佣金、最大佣金,并
    --且最大佣金大于100
    select deptno,avg(sal),min(comm),max(comm) from emp where sal>1300 group by deptno having max(nvl(comm,0))>100;

    select e.deptno,avg(e.sal),min(e.comm),max(e.comm) from emp e where e.sal>1300 group by e.deptno having max(nvl(e.comm,0))>100;

    --查询出雇员名,雇员所在部门名称, 工资等级。
    select * from emp e ,dept d where e.deptno = d.deptno;
    select * from salgrade;
    select e.ename, d.dname, sg.grade
    from emp e, dept d, salgrade sg
    where e.deptno = d.deptno
    and e.sal between sg.losal and sg.hisal;

  • 相关阅读:
    arcgis api 3.x for js 入门开发系列八聚合效果(附源码下载)
    arcgis api 3.x for js 入门开发系列七图层控制(附源码下载)
    arcgis api 3.x for js 入门开发系列六地图分屏对比(附源码下载)
    arcgis api 3.x for js 入门开发系列五地图态势标绘(附源码下载)
    arcgis api 3.x for js 入门开发系列四地图查询(附源码下载)
    Java里面获取当前服务器的IP地址
    Flutter at Google I/O 2018
    Modbus RTU 协议使用汇总
    plsql 创建表空间、用户、赋予权限
    Oracle:ODP.NET Managed 小试牛刀
  • 原文地址:https://www.cnblogs.com/cqming/p/10722057.html
Copyright © 2011-2022 走看看