从一个数据库切换到另外一个数据库的命令
use scott;          //切换到scott数据库
查询【最重要的 难度最大】关键字是顺序
   1.计算列
        select * from emp   //表示从emp表查询 *表示所有的
        select empno, ename from emp;  //查询empno , ename 从emp表中
        select ename , sal * 12 as "年薪" from emp;  // 查询 ename sal * 12  as可以省略“年薪”要用双引号,年薪为别名、
         select ename , sal * 12 as "年薪" ,sal "月薪" ,job from emp;//中间用逗号输出  
        select 5 from emp;  //输出的行数是emp表的行数,每行只有一个字段,值为5  无实际意义
        select 5; //也可以,不推荐
        注意:在oracle中字段的别名不允许用单引号括起来,但是在SQL server2005却允许,因此为了兼容性能最好字段别名使用双引号括起来,不要用单引号
 2.distinct【不允许重复的】
        select deptno from emp; //emp表中有14行记录,就会显示14行记录
        select distinct deptno from emp;  //distinct deptno 会过滤掉重复的 deptno 
        select distinct comm from emp; //comm中有null,distinct也可以过滤重复的null,或者说有多个null只输出一个
        select distinct comm ,deptno from emp;  //把comm和deptno的足额和进行过滤
        select deptno, distinct comm  from emp;   //是有语法错误的。逻辑长有冲突
        select   deptno, * from emp;  //在SQL server中写没问题,但是在Oracle11G中会出错,因此不建议这样写            
    3.between【在某个范围】
        //查找工资在1500到3000之间(包括1500到3000)的所有员工信息
        select * from emp
            where sal  >= 1500 and sal <= 3000
       等价于
        select * from emp
            where sal between 1500 and 3000
        //查询工资小于1500大于3000之间的所有员工的信息        
        select * from emp
            where sal  < 1500 or sal > 3000       
        select * from emp
            where sal  not where 1500 and 3000
    4.in【在属于若干个孤立的值】
        select * from emp where sal in (1500,3000,5000)
        等价于
        select * from emp
             where sal = 1500 or sal = 3000 or sal = 5000
///
        select * from emp where sal not in (1500,3000,5000) //既不是1500也不是3000
        等价于
        select * from emp
             where sal != 1500 and sal != 3000 and sal != 5000  //数据库不等于有两种表示!=  <>  推荐第二种,对或取反是并且   非并且是或  对并且取反是或             
  5.top【最前面的若干记录】
         select top 5 * from emp;   //输出前五个记录
        select top 15 percent * from emp; //输出前面15%的记录
如何把1500到3000之间的工资最高的4个输出
        select top 4 *  from emp
            where sal between 1500 and 3000
desc 降序  不写默认是升序
    6.null【没有值 空值】
      零和null是不一样的,null表示空值,没有值,零表示一个确定的值
         select * from emp;  //输出奖金非空员工的值
        select * from emp where comm <> null;  //输出为空 errori
        select * from emp where comm != null;  //输出为空 error
        select * from emp where comm = null;  //输出为空 error
              总结:null 不能参与<> ,!= ,=,运算  null可以参与is  not  is
        select * from emp where comm is null;  //输出奖金为空的员工信息
       select * from emp where comm not is null;  //输出奖金不为的员工信息
        任何类型的数据都允许为null
                           create table t1(name, nvarchar(20),cnt,int,riqi,datetime);                   
                          insert into t1 values(null,null,null)
                          select * from t1;
null 不能和具体值做四则运算 否则永远为null
        select ename , sal * 12 + isnull(comm,0) from emp  //1.(comm, 0)如果comm是null 就返回零,否则返回comm的值
  //isnull是个函数        
   7.order by【字段某个字段排序】  sac是升序的意思  desc是降序
          order by a, b          --a和b都是升序
          order by a, b desc   --a升序,b降序
          order by a desc, b   --a降序,b升序
          order by a desc, b desc   --a和b都是降序
         文字描述:
               如果不指定排序的标准,则默认是升序,升序用asc,默认可以不写,如果为一个字段指定的排序标准并不会对另外一个字段产生影响,强烈建议为每一个字段指定排序标准。
         select * from emp order by sal //  从emp表中查询按sal排序默认是升序
         select * from emp order by deptno ,sal;   //先按deptno升序排列,再按sal升序排序
desc , sal;  //先按deptno降序排序,再按sal升序排序
  8.模糊查询  【搜索时经常使用】
like '%A%'   //搜索ename中有A字母的记录
            格式:
                select 字段的集合 from 表名  where 某字段的名字 like 匹配的条件
                匹配的条件通常含有通配符
          通配符:
                    %:任意零个或多个字符
                   _【这是下划线,不是减号】:任意单个字符
                [a-f]:a到f中的任意单个字符,只能是a  b  c  d  e  f中的任意单个字符
                [a, f]:a或f
                [^a-c]:除了a到c的任意单个字符
        select * from student where name like '%\_%' escape '\'   //escape 关键字
        这表示把所有记录中name列包含的下划线'_'字符的记录输出
         escape '\' 表示把'\'字符当作转义字符的标志
        在sql中可以把任意字符当作转义字符的标志,具体是把那个字符当作转义字符,这是由excape要制定为转义字符的字符来决定的
注意:匹配字符要用单引号括起来 不能省略也不能改用双引号,通配符作为特殊的字符使用问题
    9.聚合函数【多行记录返回一个值 通常用于统计分组的信息】
              函数的分类:
                   单行函数
                              每一行返回一个值
                   多行函数
                             多行返回一个值
                             聚合函数是多行函数
                例子:
                       select lower(ename) from emp;  //lower()是单行函数最终返回14行
                       select max(ename) from emp; //返回1行 max是多行函数
 注意:单行函数和多行函数不可以同时使用
聚合函数的分类
                          max()
                          min()
                         avg()  平均值 
                        count()  求个数  //count(*) 返回表中所有的记录的个数,count(字段名)返回字段值非空的记录的个数,重复的记录也会被当作有效值,count(distinct 字段名)  返回字段不重复并且非空的记录的个数
                   select count(*) from emp ; //返回emp表所有记录的个数  14个
                   select count(deptno)  from emp;  //返回值是14  说明重复的记录也被有效记录,
                   select (distinct deptno) from emp; //返回值是3,统计deptno不重复的记录个数
null的记录不会被当作有效的记录        
  10.group by【分组】
 
                功能:        把表中的记录按照字段分成不同的分组
                例子:    //输出每个部门的编号和该部门的平均工资
                select deptno avg(sal) as "部门的平均工资" from emp group by deptno
     总结:使用了 group by 之后select 中只能出现分组后的整体信息不能出现组内详细信息   
              select  deptno, job, avg(sal) from emp group by deptno, job  
              注意:理解group by a, b, c 的用法先按a分组如果a相同,再按b分组,如果b相同,再按c分组,最终统计的是最小分组的信息。 
  11.having【对分组之后的信息进行过滤】
              输出部门平均工资大于1500的部门编号 部门的平均工资
               select deptno, avg(sal) from emp  group by deptno having avg(sal) > 1500
        having【对分组之后的信息进行过滤】
              1.having子句是用来对分组之后数据进行过滤,因此使用having时通常都会先使用group by
              2.如果每使用group by 但使用了having则意味着having把所有的记录当作一组来过滤,极少用
              3.having子句出现的字段必须时分组之后的组的整体信息,having子句不允许出现组内的详细信息
              4.尽管select可以出现别名但是having子句不能出现字段别名,只能使用字段的最原始的名字
              5.  having和where的异同
                     相同:
                        都是对数据过滤,只保留有效的数据,where和having一样都不允许出现字段别名,值允许出现最原始字段的名字 
                    不同:
where是对原始数据的记录过滤,having是对分组之后的记录过滤两者的顺序
                          where必须写在having之前顺序不可颠倒,否则会出错     
12.连接查询【】
            定义:将两个表或者两个以上的表以一定的连接条件连接起来从中检索出满足条件的数据。 
            分类
内连接【重点的重点 难点的难点】
 1.  select   ...   from A, B  的用法
                                        产生的结果:
                                               行数是A和B的乘积
                                               列数是A和B之和
                                              或者说,把A表的每一条记录都和B表的每一条记录组合在一起形成的是个笛卡尔积
                       2.  select ...   from A, B where ... 的用法
                                 对 select ... from A,B产生的笛卡尔积用where进行过滤
                                例子:select *
                                                      from emp, dept
                                                       where empo = 7659
                       3.  select ...  from A join B on ...   的用法
 jion是连接的意思   on表示连接条件on不可以省略,有join必须有on
                       4.SQL92与SQL99标准的区别         
                                select   ...from A, B where ... 是SQL92标准
                                select    ...from A join B on ...是SQL99标准
                               输出结果是一样的,推荐使用SQL99标准
                                      因为SQL99更容易理解,在SQL99中on和where可以做不同的分工,where对连接之后的临时表的数据进行过滤。
                               
                       5. select from where join on group by   order top  having  混合使用
                                 select
                                           from emp  "D"
                                            join dept  "E"
                                           on "E".deptno  = "D".deptno 
 
               查询顺序
                        select top ...
                              from A 
                              join  B
                              on ...
                             join C
                             on ....
                             where ..............
                            group by ...............
                           having .....
                         order by ..............
           外连接
                      
 
 
           完全连接
           交叉连接
           自连接 
           联合