、查看表结构用desc
    desc emp;
 
2、空表dual,最常用的空表,如:
    select 2 * 4 from dual;
    select sysdate from dual;
 
3、双引号能保持格式
    如:select sysdate “toDay 日 期” from dual;
 
4、|| 字符串连接
    如:select 2*3 || 8 from dual;
    select ename || sal from scott.emp;
    select ename || ‘ORACLE’ from scott.emp;
 
5、单引号,如:select 2 * 2 || 'abc''efg' from dual;
    用两个单引号表示一个单引号
 
6、去掉重复数据distinct
    select distinct deptno from scott.emp;
    去掉重复组合:select distinct deptno,job from scott.emp;
 
7、where查询
    A、=查询,select * from scott.emp where sal = 1500;
 
    B、比较<、>、>=、<=
        select * from scott.emp where sal > 1500;
    C、and or
        select * from scott.emp where sal > 1500 and sal <= 5000 or deptno = 10;
    D、in、not in
        select * from scott.emp where sal in (1500, 800) and deptno not in (10, 20)
 
    E、like模糊 escape 转义
        Select * from scott.emp where ename like ‘%in%’;
        Select * from scott.emp where ename like ‘%in\%k%’;
        Select * from scott.emp where ename like ‘%in#%k%’ escape ‘#’;
        表示like中的#号是转义字符,相当于\
    F、is null、is not null
    K、    order by
        select sal, ename from scott.emp order by sal;
        select sal, ename from scott.emp order by sal asc;
        select sal, ename from scott.emp order by sal desc;
        select sal, ename from scott.emp where sal > 2000 order by sal desc;
        select sal, deptno, ename from scott.emp order by sal,deptno desc;
    
8、function
    A、lower、upper、substr
        select lower(‘abcABC’) from dual;
        select upper(‘abcABC’) from dual;
        substr(target, startIndex, length)
        select substr(‘abcABC’, 1, 3) from dual;
 
    B、chr、ascii
        将数字安装ascii值转换成字符:select char(65) from dual;
        将字符转换成ascii值:select ascii(‘Z’) from dual;
    
    C、round、to_char
        精确小数
        select round(22.456) from dual;
        保留2位小数:select round(22.456, 2) from dual;
        精确到个位:select round(22.456, -1) from dual;
    
        货币
        设置货币格式,000前面不足就用0代替
        select to_char(sal, '$000,000.00') from scott.emp;
        999就不会替换不足的地方,只会安装格式输出
        select to_char(sal, '$999,999.99') from scott.emp;
        本地货币格式
        select to_char(sal, 'L999,999.99') from scott.emp;
 
        日期
        日期格式 
        格式控制 描述 
        YYYY、YYY、YY 分别代表4位、3位、2位的数字年 
        YEAR 年的拼写 
        MM 数字月 
        MONTH 月的全拼 
        MON 月的缩写 
        DD 数字日 
        DAY 星期的全拼 
        DY 星期的缩写 
        AM 表示上午或者下午 
        HH24、HH12 12小时制或24小时制 
        MI 分钟 
        SS 秒钟 
        SP 数字的拼写 
        TH 数字的序数词 
 
        “特殊字符” 假如特殊字符 
        HH24:MI:SS AM 15:43:20 PM
        select to_char(sysdate, 'YYYY-MM-DD HH:MI:SS') from dual;
        select to_char(sysdate, 'YYYY-MM-DD HH24:MI:SS') from dual;
 
     D、to_date、to_number、nvl
        to_date(target, current_format)
        select to_date('2011-4-2 17:55:55', 'YYYY-MM-DD HH:MI:SS') from dual;
        select to_number('$12,322.56', '$999,999.99') + 10 from dual;
        select to_number('$12,322.56', '$00,000.00') + 10 from dual;
        select to_number('22.56') + 10 from dual;
        nvl可以将某个字段的空值转换成指定的值
        select ename, sal, nvl(comm, 1.00) from scott.emp;
 
9、group function 组函数:min、max、avg、sum、count
        select max(sal) from scott.emp;
        select min(sal) from scott.emp;
        select avg(sal) from emp;
        select round(avg(sal), 2) from emp;
        select to_char(avg(sal), 'L999,999.99') from emp;
        select sum(sal) from emp;
        select count(comm) from emp;
        select count(distinct deptno) from emp;
 
10、group by 分组
    select deptno, avg(sal) from emp group by deptno;
    select deptno, job, avg(sal) from emp group by deptno, job;
    求部门最高工资的所在部门的员工信息:
    select deptno, ename, sal from emp where sal in (select max(sal) from emp group by deptno);
 
11、having 对分组数据进行过滤
    求部门评价工资:
    select * from (select avg(sal) sal, deptno from emp group by deptno) where sal > 2000;
    select avg(sal) sal, deptno from emp group by deptno having avg(sal) > 2000;
 
12、子查询
    求部门分组后工资最高的员工信息
    select emp.ename, emp.sal, emp.deptno from emp, (select max(sal) max_sal, deptno from emp group by deptno) t where emp.sal = t.max_sal and emp.deptno = t.deptno;
    求部门平均工资等级
    select s.grade, t.deptno, t.avg_sal from scott.salgrade s, (select deptno, avg(sal) avg_sal from emp group by deptno) t where t.avg_sal > s.losal and t.avg_sal < s.hisal;(between)
 
13、自连接
    select a.ename, b.ename mgr_name from emp a, emp b where a.empno = b.mgr;
 
14、 连接查询
    select dname, ename from dept, emp where dept.deptno = emp.deptno;
    select dname, ename from dept join emp on dept.deptno = emp.deptno;
    select dname, ename from dept join emp using(deptno);
    select dname, ename from dept left join emp on dept.deptno = emp.deptno;
    select dname, ename from dept right join emp on dept.deptno = emp.deptno;
    select dname, ename from dept full join emp on dept.deptno = emp.deptno;
    select a.ename, b.ename mgr_name from emp a join emp b on a.mgr = b.empno;
    select a.ename, b.ename mgr_name from emp a left join emp b on a.mgr = b.empno;
 
15、 Rownum
    select rounum, deptno, dname from dept;
    select * from (
         select rownum r, dept.* from dept
    ) t where  t.r > 2;
 
16、树状结构查询
    select level, empno, ename, mgr from emp
    connect by prior mgr = empno;
 
17、排序函数
    --按部门分组,给出分组后的序号
    select row_number() over(partition by deptno order by sal), emp.* from emp;
    
    --rank排序,空出相同部分
    select rank() over(partition by deptno order by sal), emp.* from emp;
    select rank() over(order by deptno), emp.* from emp;
    select rank() over(order by sal), emp.* from emp;
    
    --dense_rank排序给出相同序号,不空留序号
    select rank() over(order by sal), emp.* from emp;
    select dense_rank() over(order by sal), emp.* from emp;
 
18、交集、并集、割集查询
    --并集:不带重复数据
    select * from emp
    union
    select * from emp2;
    
    --并集:带重复数据
    select * from emp
    union all
    select * from emp2;        
    
    --割集,显示不同部分
    select * from emp
    minus
    select * from emp2;
 
19、 查询系统表、视图
    select owner, object_name, object_type, status, dba_objects.* from dba_objects where object_type = 'view' and status = 'invalid';
 
    select * from user_objects where object_type like 'PROCEDURE';
 
20、练习题
    --部门最高薪资员工信息
    select ename, sal, deptno from emp 
    where sal in (select max(sal) from emp group by deptno);
    
    --部门最高薪资员工信息
    select ename, sal, emp.deptno from emp 
    join (select max(sal) max_sal, deptno from emp group by deptno) t 
    on emp.deptno = t.deptno and emp.sal = t.max_sal;
    
    --部门平均薪资等级
    select grade, losal, hisal, t.avg_sal from salgrade 
    join (select avg(sal) avg_sal, deptno from emp group by deptno) t
    on t.avg_sal between losal and hisal;
    
    --经理人
    select ename, job from emp where empno in (select mgr from emp);
    
    --不用分组函数,查询薪水最高值
    select * from (select sal, ename from emp order by sal desc) where rownum = 1;
    select distinct a.sal from emp a join emp b on a.sal > b.sal where rownum = 1;
    select sal from emp where sal not in (select distinct a.sal from emp a join emp b on a.sal < b.sal);
    
    --部门平均薪水最高的部门编号
    select deptno, t.avg_sal from (select avg(sal) avg_sal, deptno from emp group by deptno) t
    where avg_sal = (
        select max(avg_sal) max_sal from (select avg(sal) avg_sal, deptno from emp group by deptno)
    );
    
    select deptno, t.avg_sal from (select avg(sal) avg_sal, deptno from emp group by deptno) t
    where avg_sal = (
        select max(avg(sal)) max_sal from emp group by deptno
    );
    
    --部门平均薪水最高的部门名称
    select dname from dept where deptno = (
     select deptno from (select avg(sal) avg_sal, deptno from emp group by deptno) t
     where avg_sal = (
            select max(avg_sal) max_sal from (select avg(sal) avg_sal, deptno from emp group by deptno)
     )
    );
    
    select dname from dept where deptno = (
        select deptno from (select avg(sal) avg_sal, deptno from emp group by deptno) t
        where avg_sal = (
               select max(avg(sal)) from emp group by deptno
        )
    );
    
    --平均薪水最低的部门的部门名称
    select dname from dept where deptno = (
      select deptno from (select avg(sal) avg_sal, deptno from emp group by deptno) 
      where avg_sal = (
        select min(avg_sal) min_sal from (
               select avg(sal) avg_sal from emp group by deptno
        )
      )
    );
    
    select dname from dept where deptno = (
        select deptno from (select avg(sal) avg_sal, deptno from emp group by deptno) 
        where avg_sal = (    
          select min(avg(sal)) avg_sal from emp group by deptno
        )
    );
    
    --平均薪水等级最低的部门的部门名称
    select dname from dept where deptno = (
    select deptno from (
         select grade, t.deptno from salgrade s join (
            select avg(sal) avg_sal, deptno from emp group by deptno
         ) t
         on t.avg_sal between s.losal and s.hisal
      )
      where grade = (
        select min(grade) from salgrade s join (
            select avg(sal) avg_sal, deptno from emp group by deptno
        ) t
        on t.avg_sal between s.losal and s.hisal
      )
    );
    
    --部门经理人中,平均薪水最低的部门名称
    select t.deptno, dname from (
        select sal, deptno from emp where empno in (select distinct mgr from emp)
    ) t join dept 
    on t.deptno = dept.deptno
    where sal = (
        select min(sal) from emp where empno in (select distinct mgr from emp)
    );
    
    --比普通员工的最高薪水还要高的经理人名称
    select * from (
        select empno, ename, sal from emp where empno in (select distinct mgr from emp where mgr is not null)
    ) t
    where t.sal > (
        select max(sal) max_sal from emp where empno not in (
         select distinct mgr from emp where mgr is not null
        )
    );

sql 学习的更多相关文章

  1. ORALCE PL/SQL学习笔记

    ORALCE  PL/SQL学习笔记 详情见自己电脑的备份数据资料

  2. TERADATA SQL学习随笔<一>

    此博客内容简介及目录 http://www.cnblogs.com/weibaar/p/6644261.html 最近在TERADATA环境学习SQL.在这里记录一下学习中查过的知识点,作为备案. 目 ...

  3. SQL学习(时间,存储过程,触发器)

    SQL学习 几个操作时间的函数 --datapart 获取时间中的年月日时分秒等部分 select DATEPART(year,current_timestamp); select DATEPART( ...

  4. (013)每日SQL学习:日期的各种计算

    1.确定两个日期之间的工作日天数 --确定两个日期之间的工作日天数with x0 as (select to_date('2018-01-01','yyyy-mm-dd') as 日期 from du ...

  5. SQL学习日记

    目录 SQL学习日记 1. 常见的数据库对象 2. DDL 定义语句 3. DML 操作语句 4. DQL 查询语句 5. DCL 控制语句 SQL学习日记 1. 常见的数据库对象 对象名 关键字 描 ...

  6. Oracle之PL/SQL学习笔记

    自己在学习Oracle是做的笔记及实验代码记录,内容挺全的,也挺详细,发篇博文分享给需要的朋友,共有1w多字的学习笔记吧.是以前做的,一直在压箱底,今天拿出来整理了一下,给大家分享,有不足之处还望大家 ...

  7. SQL学习笔记

    SQL(Structured Query Language)学习笔记 [TOC] Terminal登录数据库 1.登录mysql -u root -p ; 2.显示所有数据库show database ...

  8. 9月18日,SQL学习基础1

    数据库管理和应用 Oltp是小型的管理,OLAP是大型的管理 开发的内容如触发器 数据库管理系统(Database Management System,简称为DBMS)是位于用户与操作系统之间的一层数 ...

  9. SQL学习:查询的用法(1)

    在SQL servre的使用中,查询的用法是最多的.最重要的,也是最难学习的,因此掌握查询的用法很重要. 先将表的示例上图 员工表: 部门表:                             ...

  10. [SQL学习笔记][用exists代替全称量词 ]

    学习sql的必经问题. 学生表student (id学号 Sname姓名 Sdept所在系) 课程表Course (crscode课程号 name课程名) 学生选课表transcript (studi ...

随机推荐

  1. SPI接口扫盲 SPI定义/SPI时序(CPHA CPOL)

    SPI接口扫盲   douqingl@gmail.com   为何要写这篇文档?百度上找出来的SPI接口中文描述都说的太过简略,没有一篇文档能够详尽的将SPI介绍清楚的.wikipedia英文版[注释 ...

  2. 迅雷thunder://协议解密

    echo -n 'thunder://''Cg==' | sed 's?thunder://??' | base64 -d | sed 's/^AA//; s/ZZ$//' 将thunder://替换 ...

  3. [POI2012]BON-Vouchers----你敢模拟吗?

    链接:https://www.luogu.org/problemnew/show/P3536 题意: 定义n个数为幸运数字,一共有n批人,设第i批人有x个,则它们会依次取走余下的x的倍数中最小的x个, ...

  4. MySQL的1067错误解决方法

    今天在学校的时候MySQL还运行的好好的,关机来公司后MySQL一直报错,错误为1067,网上找了好多办法,但是大都没效果,因此对这个错误做个总结: 打开你的安装目录下,查看my.ini文件中MySQ ...

  5. JAVA Swing使用JFreeChart实现折线图绘制

    效果如下: 实现步骤: 1.导入JAR包 jfreechart官网下载的zip文件中包含这两个jar包 2.代码编写 import org.jfree.chart.ChartFactory; impo ...

  6. non-member function cannot have cv-qualifier

    Q: non-member function unsigned int abs(const T&) cannot have cv-qualifier. template<typename ...

  7. django 执行 python manage.py makemigrations 报错

    RuntimeError: Model class app_anme.models.xxx doesn't declare an explicit app_label and isn't in an ...

  8. Golang面向过程编程-函数

    Golang面向过程编程-函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.什么是函数 简单的说函数的作用就是把程序里多次调用的相同的代码部分定义成一份,然后起个名字,所有的 ...

  9. GO语言的进阶之路-流程控制

    GO语言的进阶之路-流程控制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 流程控制在编程语言中是最伟大的发明了,因为有了它,你可以通过很简单的流程描述来表达很复杂的逻辑.流程控制 ...

  10. Linux记录-集群时间同步解决方案

    1.各个主机安装ntpdate 2.编写shell #!/bin/sh array=("root@master" "root@slave002" "r ...