SQL 基本查询语句

SQL 基本查询语句

  1. from子句(查询所有)   select * from  table ;
  2. 使用别名(别名可以直接写,如果别名中需要区分大小写字符或别命中包含字符或空格,则必须用双引号引起来)                       select  empno e,ename “NAME”  from table ;
  3. 条件查询(Where语句)                      select * from table where deptno=10;    
  4. (当查询条件是字符或日期类型的数据时,条件需要使用单引号引起来)                        select * from table where job=’SALESMAN’;
  5. 如果只查询表的部分列,则需要在select后面指定列名                       select id,name,sale_price from table;
  6. 使用比较运算符(>,<,>=,<=,!=,<>,=)做条件查询                     select * from table where sal<200;                                        select name,price,id from table where deptno !=10;(!=等价于<>);
  7. 如果查询条件是多个,则使用and,or关键字进行条件查询              select * from table where id>100 and sal<20;                      select * from table where sal>15 or job=’CLERK’,
  8. 模糊查询(like条件查询)  *通配符:  %:表示0到多个字符()    _:表示单个字符              select * from table where name like ‘_A%’ ;
  9. 使用IN和NOT IN

查询职位是MANAGER和CLERK的员工:

Select ename,job from table where job IN (‘MANAGER’,’CLERK’);    查询不是部门10和20的员工:

    Select ename,job from table where deptno NOT IN (10,20);

  1. 查询符合某个范围的数据(Between…and….)

Select * from table where sal between 1500 and 200 ;

  1. 空值null是特殊值,不能用=,!=,必须使用is null和is not null;        select * from table where comm. Is null ;
  2. 使用any(任一)和all(全部)进行条件查询

>any :大于最小        <any :小于最大                                         >all :大于最大    <all :小于最小

           查询薪水比职位是SALESMAN的人高的员工信息

Select empno,ename,job,sal,deptno from table  where sal> any(select sal from table where job=’SALSMAN’) ;

  1. 查询条件中使用表达式和函数

字符串函数upper,将条件中的字符串变大写后再进行比较

      Select ename,sal,job from table where ename=upper(‘rose’) ;

      使用算术表达式,查询年薪大于10万元的员工

扫描二维码关注公众号,回复: 1447257 查看本文章

      Select ename,sal,job from table where sal*12>100000 ;

  1. 使用distinct 过滤重复

查询员工的部门编码,去掉重复值:

Select distinct deptno from table

查询每个部门的职位,去掉重复值。注意是deptno和job联合起来不重复:

Select distinct deptno,job from table ;

  1. 使用order by 字句排序

Select * from table order by  sal ;

ASC(升序排列,不写默认)和DESC(降序排列)

Select empno,ename,job from table where deptno=10 order by job;

多个列排序:

Select ename,deptno,sal from table order by sal asc,deptno desc ;

  1. 聚合函数

Max(最大值),Min(最小值):

猜你喜欢

转载自www.cnblogs.com/wnadd/p/9131710.html
今日推荐