zoukankan      html  css  js  c++  java
  • SQL——SQL语句总结(8)

    连接查询

    1.内连接查询

    (1)查询员工姓名、员工工资以及员工所属部门名称。

    select ename, sal, dname from emp e, dept d where e.deptno = d.deptno;

    结果:

    (2)使用 inner join 查询员工姓名、员工工资以及员工所属部门名称。

    两个表之间的关系通过 inner join 指定。使用这种语法的时候,连接的条件使用 on 子句给出,而不是where。

    select ename, sal, dname from emp e inner join dept d on e.deptno = d.deptno;

    结果:

    与(1)结果一样:

    提示:使用where子句定义连接条件比较简明了,而 inner join 语法是 ANSI SQL 的标准规范,
    使用 inner join 连接语法能够确保不会忘记两表连接条件,
    而且 where 子句在某些时候会影响查询的性能。
    ANSI SQL:“美国国家标准化组织(ANSI)”是一个核准多种行业标准的组织。SQL作为关系型数据库所使用的标准语言,
    最初是基于IBM的实现在1986年被批准的。1987年,“国际标准化组织(ISO)”把ANSI SQL作为国际标准。
    这个标准在1992年进行了修订(SQL-92),1999年再次修订(SQL-99)。目前最新的是SQL-2011

    (3)查询emp表中的员工姓名以及直接上级的姓名。

    select e1.ename '员工姓名', e2.ename '直接上级' from emp e1 inner join emp e2 on e1.mgr = e2.empno;
    或
    select e1.ename '员工姓名', e2.ename '直接上级' from emp e1, emp e2 where e1.mgr = e2.empno;

    结果:

    (4)查询 smith 的上级姓名。

    select e1.ename '员工姓名', e2.ename '直接上级' from emp e1, emp e2 where e1.mgr = e2.empno and e1.ename = 'smith';
    或  内连接 inner join
    select e1.ename '员工姓名', e2.ename '直接上级' from emp e1 inner  join emp e2 on e1.mgr = e2.empno and e1.ename = 'smith';
    或
    select e1.ename '员工姓名', e2.ename '直接上级' from emp e1 inner join emp e2 on e1.mgr = e2.empno where e1.ename = 'smith';

    结果:

    (5)查询出雇员名,雇员工资,工资等级。

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

    结果:

    (6)查询出雇员名,雇员所在部门名称,工资等级。

    select e.ename, d.dname, s.grade from emp e, dept d, salgrade s 
    where e.sal between s.losal and s.hisal and d.deptno = e.deptno;

    结果:

    2.外连接查询

    (1)使用外连接查询员工信息和部门信息

    首先我们需要先在员工表里添加一条数据,如下:

    insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO) 
    values (1234, 'fei', 'boss', null, null, null, null, null);
    #左外连接 left outer join
    select * from emp e left outer join dept d on e.deptno = d.deptno;

    结果:

    #右外连接 right outer join
    select * from emp e right outer join dept d on e.deptno = d.deptno;

    结果:

    3.子查询

    (1)有哪些人的薪水是在整个雇员的平均薪水之上的

    #1.查处各部门的平均薪水
    select avg(e.sal) from emp e;
    #2.所有雇员的薪水与平均工资作对比
    select e.ename, e.sal from emp e having e.sal > (select avg(e.sal) from emp e);

    结果:

    1)                           2) 

    (2)查询在雇员中有哪些人是经理人(哪些人有下级)

    #1.先查询说有经理人的编号
    select distinct e.mgr from emp e;
    #2.根据查询到的经理人编号查询经理人姓名
    select e.empno, e.ename from emp e where e.empno in (select distinct e.mgr from emp e); 

    结果:

    1)                   2)

    (3)求部门平均薪水的等级

    #1.查询每个部门的平均薪水
    select e.deptno, avg(e.sal) from emp e group by e.deptno;
    #2.关联工资等级表,求出平均薪水的等级
    select a.deptno, s.grade from salgrade s 
    right join (select e.deptno, avg(e.sal) asal from emp e group by e.deptno) a
            on asal between s.losal and s.hisal;

    结果:

    1)           2)

    (4)求部门平均的薪水等级

    #先求出每个人的薪水等级
    select e.ename, e.deptno, s.grade from emp e 
    left join salgrade s 
    on sal between losal and hisal order by deptno;
    #按照部门求等级的平均等级
    select a.deptno, avg(a.grade) '部门平均等级' from 
    (select e.ename, e.deptno, s.grade from emp e 
    left join salgrade s 
    on sal between losal and hisal order by deptno) a 
    group by a.deptno;

    结果:

    1)            2)

    (5)求薪水最高的前五名员工信息

     #薪水从高到低排列
     select * from emp e group by e.sal desc;
     #分页
     select * from (select * from emp e group by e.sal desc) a limit 0,5;

        limit(m,n) n 为 最大显示数,m 为从第几行显示(表格行数从 0 开始)。

    结果:

    1)

     2)

    (6)求薪水最高的6到10名雇员

     #薪水从高到低排列
     select * from emp e group by e.sal desc;
     #分页
     select * from (select * from emp e group by e.sal desc) a limit 5,5;

     结果:

     2)

  • 相关阅读:
    在Oracle的FORM中高亮显示鼠标点击或光标所在的行
    Attempt to refer to a unregistered pool by its alias 'MySQL'
    C#中手机号验证,邮箱验证
    Caused by: java.lang.ClassNotFoundException:org.apache.commons.logging.LogFactory
    org.apache.jasper.JasperException: /existingstudent.jsp(4,4) Invalid directive
    C#中使用cookies
    【old】简单易用的鹰眼类源代码下载
    MapGuide Tips如何限制MapGuide Ajax Viewer的缩放范围
    MapGuide应用开发系列(八)MapGuide Studio准备地图之地图(Map)
    基于MapGuide的在线WebGIS站点介绍
  • 原文地址:https://www.cnblogs.com/nyfq/p/14110484.html
Copyright © 2011-2022 走看看