• Oracle 数据库常用SQL语句(2)查询语句


    一、SQL基础查询

    1、select语句

      格式:select 字段 from 表名;

      2、where 用于限制查询的结果。

      3、查询条件 > < >= <= = !=

      4、与 或(AND,OR)
      
      5、在 不在(IN,NOT IN)
      练习:查询工号为1,9,11,16且工资低于1000的员工。

      6、在 [a,b] (between val1 and val2)
        查询工资大于1000,小于1500的员工。

    select * from s_emp where salary between 1000 and 1500;

      7、空 非空(IS NULL,NOT NULL)

      8、全部 任一(ALL,ANY)
        不能单独使用,必须要与关系运算符配合。
        查询职位是仓库管理员的工资。

    select * from s_emp where title = 'Stock Clerk';


        查询工资比任一仓库管理员高的员工信息。

    select * from s_emp where salary > any(select salary from s_emp where title = 'Stock Clerk');

      9、排重 DISTINCT
        练习:工资在1000~1500之间的职位有哪些。

    select distinct title from s_emp where salary>=1000 and salary <1500 ;

    二、排序

    1、使用 ORDER BY 语句
      格式:select 字段 from 表名 where 条件 ORDER BY 字段;
        显示员工名字、职位、工号,按年薪从低到高的排列显示。

    select last_name,title,id,salary from s_emp order by salary;


    2、设置升序降序(ASC,DESC)
      格式:select 字段 from 表名 where 条件 ORDER BY 字段 ASC|DESC;
        显示员工名字、职位、工号,按年薪从高到低的排列显示。

    select last_name,title,id,salary from s_emp order by salary desc; 

    3、多项排序
      格式:select 字段 from 表名 where 条件 ORDER BY 字段 ASC|DESC,字段 ASC|DESC;

    三、聚合函数

    注意:在使用比较运算符时NULL为最大值,在排序时也会受影响。
    把select语句的多条查询结果,汇聚成一个结果,这样的函数叫聚合函数。

    1、MAXMIN
      获取最大值和最小值,可以是任何数据类型,但只能获取一个字段。

    select * from s_emp where salary = (select MAX(salary) from s_emp);

    2、AVGSUM
      获取平均值、总和。
    3、COUNT
      统计记录的数量。

    四、分组查询

    1、GROUP BY
      格式:select 组函数 from 表 group by 字段。

    select count(last_name) from s_emp group by dept_id;

    2、HAVING 组判断条件。
      它的真假决定一组数据是否返回。
        计算部门中最低工资小于700的部门平均工资。

    select count(last_name) from s_emp group by dept_id having max(salary) > 2000;

    五、查询语句的执行顺序

    格式:select sum(salary) from 表名 where bool order by group by having
      a、from 表名,先确定数据的来源。
      b、where 确定表中的哪些数据有效。
      c、group by 字段名,确定数据的分组依据。
      d、having 确定组数据是否返回。
      e、order by 对组数据进行排序。

    六、关联查询

    1、多表查询
      select 字段 from 表1,表2 where;
    2、多表查询时有相同字段怎么办
      1、表名.字段名
      2、表名如果太长,可以给表起别名 (from 表 别名)
      3、笛卡尔积
      a 8条数据 b 9条数据 = 笛卡尔积
      在多表查询时,一定要设置where条件,否则将得到笛卡尔积。

    七、连接查询

    当使用多个表进行关联查询时,根据设置的条件会得到不同的结果(能匹配成功和不能匹配成功的)。
      1、内连接:右边两连能匹配成功的数据。

    select last_name,name from s_emp,s_dept where dept_id = s_dept.id;

      2、外连接:左右两边不能匹配的数据。

    select last_name,name from s_emp, left outer join s_dept on dept_id = s_dept.id。

      3、左外连接:匹配成功的数据+左表不能匹配的数据

    select last_name,name from s_emp, left outer join s_dept on dept_id = s_dept.id。

      4、右外连接:匹配成功的数据+右表不能匹配的数据

    select last_name,name from s_emp, right outer join s_dept on dept_id = s_dept.id。

      5、全外连接:匹配成功的数据+左表不能匹配的数据+右表不能匹配的数据

    select last_name,name from s_emp, full outer join s_dept on dept_id = s_dept.id。
  • 相关阅读:
    JAVA后端方面,如何快速达到能实习的程度
    如何高效地把Spring boot学到能干活的程度
    零高并发项目经验的人如何通过面试得到实践机会?
    Java学到什么程度可以面试工作?
    Java培训班学员如何找工作?如何过试用期?
    作为Java技术面试官,我如何深挖候选人的技能
    今年我拿到了期望中的收入,同时更希望能在睡后收入上有进一步的发展——2021年我的总结与思考
    程序员月薪一万到底难不难?
    自学java,如何快速地找到工作
    搞IT的应届生如何写好简历?
  • 原文地址:https://www.cnblogs.com/unjack/p/9789280.html
Copyright © 2020-2023  润新知