Scott表下有这么几个常用的表,而且还带有数据。分别是emp、dept、salgrade;
1、查看表结构用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
        )
    );

转载于:https://www.cnblogs.com/bokxy/p/4873017.html

Oracle笔记 三、function 、select相关推荐

  1. oracle到神通,我的ORACLE笔记(三)

    我们已经了解了ORACLE MVCC的基本协议,并且知道了用于构造历史版本的UNDO信息被集中存放于回滚段中.在事务处理中,你读到的版本实际上是你能读到的所有数据版本中最新的那个(这点不同于Flash ...

  2. 冯柯《我的ORACLE笔记二:关于回滚段》接力来袭!

    感谢大家对此文的关注!我们的活动还在继续中~您对文章内容有任何疑问时,欢迎您点击原文链接,填写阅读反馈表.我们不仅会邀请作者来回答问题,还会从中抽取一位粉丝赠送精美礼品一份哦~ <我的ORACL ...

  3. oracle笔记一(sql语句方面)

    oracle笔记一(sql语句方面) 一.sql语句 --================================================ 1.增加主键    alter table ...

  4. Hive学习笔记三之函数操作

    文章目录 5 函数 5.1 系统内置函数 5.2 常用内置函数 5.2.1 空字段赋值 5.2.2 CASE WHEN THEN ELSE END(类似于java中的switch case) 5.2. ...

  5. 李兴华oracle ppt,魔乐科技Oracle笔记超经典李兴华doc.ppt

    魔乐科技Oracle笔记超经典李兴华doc 连接符 || 图表 1 多表查询的基本语法 查一张以上的表,就叫做多表查询 例子:查询出雇员名称,部门名称和部门所在地的(一般多表查询要用别名) 统计记录数 ...

  6. 大数据HiveSQL学习笔记三-查询基础语法以及常用函数

    大数据HiveSQL学习笔记三-查询基础语法以及常用函数 一.基础语法 1.SELECT -列名- FROM -表名- WHERE -筛选条件- 如:需要根据城市,性别找出匹配的10个用户 user_ ...

  7. 尚学堂oracle笔记2

    第一课:客户端           1. Sql Plus(客户端),命令行直接输入:sqlplus,然后按提示输入用户名,密码.           2. 从开始程序运行:sqlplus,是图形版的 ...

  8. oracle下使用sql命令,ORACLE笔记(2)ORACLE 学习中用到的SQL命令

    sqlplus sys/****** as sysdba   利用超级用户名的登录(在我使用时没有成功) sqlplus "as sysdba";可以登陆到ORACLE上,这种方式 ...

  9. JQ实现三个Select下拉框互斥

    2019独角兽企业重金招聘Python工程师标准>>> 目的:三个select下拉框中的内容相同,当其中一个下拉框选中了其中一个值后,隐藏掉另外两个下拉框中相同的值. html: & ...

最新文章

  1. DIV+CSS 命名规范
  2. 对于EXCEL模板程序的处理
  3. 5、MySQL修改数据库:ALTER DATABASE用法简介
  4. 潍坊学院的计算机类怎么样,潍坊学院教育技术学专业怎么样?有知道的麻烦说下,谢谢!...
  5. sql程序实现事物锁表和解锁_怎样用SQL给SQL2880特定表加锁解锁
  6. Linux配置虚拟主机后,只能访问到主页怎么办?
  7. 指针和字符串,数组和字符串(1)
  8. 从零构建一个图像分类项目 -- 代码
  9. IE6 position:fixed bug (固定窗口方法)
  10. Excel公式与函数实战应用-陈明霞-专题视频课程
  11. 小米5手机刷成开发版获取root权限
  12. 【PC】如何导出windows锁屏壁纸
  13. JavaScript一些优雅小技巧不得不知
  14. java实现生成验证码图片
  15. 高考倒计时一天,加油!
  16. world标题是大写数字,题注要阿拉伯数字,交叉引用不会出错
  17. Sql server 2016 Always on 实现无域高可用
  18. 开机、重启和用户登录注销
  19. 简单认识向上转型和向下转型
  20. QQSpider qq空间爬虫

热门文章

  1. java ranger rest_kafka ranger integration issuse
  2. cpu模拟器c语言实现_你写出来的C语言是这样调用硬件的!
  3. 服务器:Nginx - 最小配置说明
  4. SQLServer常用的日期和时间函数梳理
  5. 硬件:关于CPU超频知识笔记
  6. 原来Github上也有这么多的JavaScript学习资源!
  7. ASP.NET MVC 4 过滤器(Authorize)
  8. java 多项式拟合最多的项数_Matlab概率统计与曲线拟合
  9. Java-n个人报数
  10. c# 设计原则需要学习吗_向最好的学习:产品设计原则