• oracle数据库笔记1oracleSQL查询


    查询功能主要由 select 语句实现 ,查询命令的基本形式:   

          select<列名表或*>

          from<表名/视图名>

          [where<条件>]

          [group  by<分组内容>]

          [having<组内条件>]

          [order  by<排序方式>]

    简单查询(无条件查询,条件查询:多条件查询),

    分组查询,

    单表查询

    多表查询(用连接查询或嵌套查询)等。

    例如:查找出10号部门中全体雇员姓名,工资,工种情况

    sql>select ename,sal,job    

          from emp

         where deptno=10;

    找出其奖金超过其工资的雇员

    SQL>select ename,sal,comm

        from emp

        where comm>sal;

    查询20号部门中有多少种不同工种

    SQL>select distinct job

        from emp

        where deptno=20;

    ps:若不要保留字distinct则显示每个雇员工种。若要,这样只显示不同的,它只修饰查结果的行

    在多条件查询命令中使用的运算符:

    SQL语言中,可使用算术,关系,逻辑运算符组成表达

    算术符:  +,-,*,/,|| 式作条件

    先 *, /,  后 +,-,||

    关系符:  =,!= 或 <> ,> ,>= ,< ,<=

    IN:(等价=ANY)

    NOT IN:(等价 !=ALL)

    BETWEEN… AND: (在两个值之间)

    LIKE:(使字符型匹配)

    IS NULL

    IS NOT NULL (NULL不同于数值0,也不同于空格符)

    逻辑符:

    NOT, AND, OR

    关系、逻辑表达式一般用在where子句中和having子句中

    查找20部门中不是服务员雇员的姓名,工种

    sql>select ename,job

        from emp

        where deptno=20 and job!='CLERK';

    计算每个销售人员的年度总报酬

    sql>select ename,sal,comm, 12*(sal+comm) SUMPRICE

        from emp

        where job='SALESMAN'

        order by 12*(sal+comm) DESC;

    查找出全部经理和第10号部门服务员的有关信息

    sql>select ename,job,sal,comm,deptno

        from emp

        where job='MANAGER' or (job='CLERK' and deptno=10);

    找出不在30号部门中的所有经理

    sql>ename,job,deptno

        from emp

        where job='MANAGER' and deptno!=30;

    列出10号部门中,既不是经理也不是服务员的所有信息

    sql>select ename,job,sal,comm,deptno

        from emp

        where deptno=10 and job NOT IN('MANAGER','CLERK');

    sql>select *

        from emp

        where NOT(job='MANAGER' OR job='CLERK')

              AND deptno=10;

    查找出工资在$2000与$3000之间职工姓名,工种、工资

    SQL>select ename,job,sal

        from emp

        where sal between 2000 and 3000;

    sql>select ename,job,sal

        from emp

        where sal>2000 and sal<3000;

    列出全部服务员、分析员或推销员名字、所在部门号

    sql>select ename,deptno,job

        from emp

        where job IN('CLERK','ANALYST','SLAESMAN');

    列出名字以’L1’开头的全部职工姓名及部门号

    sql>select ename,deptno

        from emp

        where ename like 'L1%';

    串匹配查找,在信息系统中使用很多,

    用’%’表示任意长度的字符串 进行匹配,

    用 ‘_’ 下划线表示仅一个字符匹配

    若选用’%K’表示以’K’结尾任意长字串, ’K_ _ _’ 以’K’开头,长度为4

    在查询命令中集函数应用:

    count (<表达式>  )

    count(*  )

    avg(  )

    min(  )

    max(  )

    sum(  )

    这些函数提供对多行统计,汇总功能

    计算公司职工最低,最高,平均及总工资

    sql>select min(sal),max(sal),avg(sal),sum(sal)

         from emp;

    计算公司雇员总人数及工种数

    sql>select count(*),count(distinct job)

        from emp;

    计算全部销售员的年平均报酬

    sql>select 12*avg(sal+comm) "year aversal"

        from emp

        where job='SALESMAN';

    查找出具有最高月工资的雇员姓名,工种,工资

    sql>select ename,job,sal

        from emp

        where sal=(select max(sal)

                   from emp);

    排序显示查询结果: 命令中选用order  by  子句

    按工种,同工种按工资降序排列显示全部雇员有关情况:

    sql>select ename,job,sal,deptno,comm

        from emp

        order by job ASC,sal DESC;

    查找奖金超过本人基本工资5%的雇员按百分比高低排序显示:

    sql>select ename,job,sal,comm,comm/sal pecent

        from emp

        where comm>0.05*sal

        order by comm/sal desc;

    分组查询: 分组查询就是将数据库表中的数据按给定条件分类组合,然后再根据需要分别操作

    命令中的group by子句

    计算公司支付每一种工种的总工资

    sql>select job,sum(sal)

        from emp

        group by job;

    计算每个部门中每种工种各有多少职工

    sql>select deptno,job,count(*)

        from emp

        group by deptno,job;

    带条件的分组查询:

    命令中的having 子句

    查找出各工种组年平均工资,要求每组在2人以上

    sql>select job,12*avg(sal+comm) "year aversal",count(*)

        from emp

        group by job

        having count(*)>2;

    列出至少有两名服务员的所有部门号

    sql>select deptno,count(*)

        from emp

        where job='CLERK'

        group by deptno

        having count(*)>2;

    统计并显示能获取奖金的职工的各部门各工种的工资总数在9000以上非经理的总工资数,按部门排序

    sql>select deptno,job,sum(sal)

        where comm>0 and job!='MANAGER'

        group by deptno,job

        having sum(sal)>9000    

       order by deptno desc;

    sql>select deptno,job,sum(sal)

        where comm>0

        group by deptno,job

        having sum(sal)>9000 and job!='MANAGER'

        order by deptno desc;

    多表连接查询:

    在关系数据库中, 各相关表之间的连接关系是通过字段(共有字段)值来体现的

    1.等值连接

    查找名字为ALLEN雇员在哪工作:

    sql>select ename,deptname

        from emp,dept

        where ename='ALLEN' AND emp.deptno=dept.deptno;

    查部门号是 20,30,40 的职工的有关情况

    sql>select e.*,d.*

        from emp e,dept d

        where e.deptno IN(20,30,40) and e.deptno=d.deptno;

    2.自连接:

    对同一个表做自身连接,使同一表的不同行连接起来

    查找出雇员与其经理的名字

    sql>select worker.ename employee,manar.ename manager

        from emp worker,emp manar

        where worker.mar=manar.empno;

    3.不等连接

    在前面介绍的关系运算符中,除去“=“外的其它运算符所进行   的连接。

    查找出比JONES挣钱多的雇员的名字,工资,工种

    sql>select obj.ename,obj.sal,obj.job

        from emp obj,emp tem

        where obj.sal>(select sal from tem where job='JONES');

    sql>select obj.ename,obj.sal,obj.job

        from emp obj,emp tem

        where obj.sal>tem.sal and tem.job='JONES';

    4.外连接 外部链接操作符(+)

    显示出所有部门名称及其职员姓名:

    sql>select d.deptno,d.dname,enmae,job

        from emp,dept d

        where dept.deptno=emp.deptno(+)  

       order by d.deptno;

    列出无雇员的部门情况

    sql>select dept.deptno,dname,loc

        from dept,emp

        where dept.deptno=emp.deptno(+) and emp.empno IS NULL;

    子查询(嵌套查询):

    在一个select命令中的where子句中,若又出现一个select命令,该查询称为嵌套查询

    where子句中包括的查询称为子查询(或嵌入的  查询块),相应外层的select命令为主查询 在子查询中的where子句中还可包含子查询,一层层嵌套。

    仅嵌套一    层的子查询称单层嵌套, 多于一层的称为多层嵌套查询, 使用子查询能  使一系列简单查询构成复杂的查询, 但是子查询结果总是用于建立主  查询(外层查询)的查询条件

    1. 单层子查询

    查找出与 SMITH 在同一个部门工作的雇员

    SQL>select ename,deptno,job

        from emp

        where deptno=(select deptno from emp where ename='SMITH');

    查找出在’BOSTON’工作的雇员

    sql>select ename,job,deptno

        from emp

        where deptno=(select deptno from dept where loc='BOSTON');

    找出比30号部门中任意雇员工资高的雇员情况

    sql>select *

        from emp

        where sal > ALL(select sal from emp where deptno='30') and deptno!=30

        order by sal desc;

    查找部门10中同部门30中工种相同的雇员

    sql>select *

        from emp

        where deptno=10 and job IN(select job from emp where deptno=30);

    sql>select *

        from emp

        where deptno=10 and job=ANY(select job from emp where deptno=30);

    找出与”FORD” 工种相同,工资相同的雇员

    sql>select ename,job,sal

        from emp

        where job=(select job from emp where ename='FORD')

              AND sal=(select sal from emp where ename='FORD');

    子查询的结果可以返回多个列的值

    sql>select ename,job,sal

        from emp

        where (job,sal)=(select job,sal from emp where ename='FORD');

    找出工资比 SCOTT 高且在 NEW YORK工作的雇员

    sql>select ename,job,sal

        from emp,dept

        where loc='NEW YORK' AND emp.deptno=dept.deptno AND

              sal>(select sal from emp where ename='SCOTT')

        order by sal;

    ps:在子查询中不能用order by    

  • 相关阅读:
    Design Patterns
    Interview
    ListView Optimization
    android onclick onLongClick ontouch dispatchTouchEvent onInterceptTouchEvent
    java hashcode equals
    Android res/raw vs assets
    HttpClient -- 血的教训
    How Android Draws Views
    元数据 metadata
    Git-2
  • 原文地址:https://www.cnblogs.com/wust221/p/3060531.html
Copyright © 2020-2023  润新知