zoukankan      html  css  js  c++  java
  • oracle 常见查询题

    查询比本部门平均薪水高的员工信息

    解法一:

    select d.deptno,e.empno,e.ename,e.sal,d.avgsal
    from emp e, (select deptno,avg(sal) avgsal from emp group by deptno) d
    where e.deptno = d.deptno and e.sal > d.avgsal
    order by 1;

    解法二:相关子查询:将主查询中的某些值作为参数传递给子查询

    select e.empno,e.ename,e.sal,(select avg(sal) from emp where deptno = e.deptno) avgsal
    from emp e
    where e.sal > (select avg(sal) from emp where deptno = e.deptno);

    查询每年入职的员工数

    select count(*) total,
    sum(decode(to_char(hiredate, 'yyyy'), '1980', 1, 0)) "1980",
    sum(decode(to_char(hiredate, 'yyyy'), '1981', 1, 0)) "1981",
    sum(decode(to_char(hiredate, 'yyyy'), '1982', 1, 0)) "1982",
    sum(decode(to_char(hiredate, 'yyyy'), '1987', 1, 0)) "1987"
    from emp

    涨工资问题,总裁1000 经理800 其他400

    解法一:

    select ename,job,sal 涨前,
    case job when 'PRESIDENT' then sal+1000
    when 'MANAGER' then sal+800
    else sal + 400
    end 涨后
    from emp;

    解法二:

    select ename,job,sal 涨前,
    decode(job, 'PRESIDENT', sal+1000,
    'MANAGER', sal+800,
    sal+400) 涨后
    from emp;

    查询员工信息:员工号 姓名 月薪工资级别(不等值连接)

    select e.empno,e.ename,s.grade
    from emp e,salgrade s
    where e.sal between s.losal and s.hisal

    按部门统计员工数:部门号 部门名称 人数

    select d.deptno,d.dname,count(e.empno)
    from emp e,dept d
    where e.deptno(+) = d.deptno
    group by d.deptno,d.dname

    层次查询

    select level,empno,ename,mgr
    from emp
    connect by prior empno=mgr
    start with mgr is null
    order by 1

  • 相关阅读:
    linux使用windows中编辑的文件,格式问题
    模拟退火算法c++
    progress第三方框架和二维码第三方框架的选择
    iOS 初始化项目内容
    github上使用SSH和gitignore
    wordpress 如何设置自定义的首页
    wordpress 删除底部"自豪地采用 WordPress"
    masonry注意事项
    iOS修改工程名
    iOS版本更新在APP中直接访问AppStore
  • 原文地址:https://www.cnblogs.com/fuyiming/p/6497041.html
Copyright © 2011-2022 走看看