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

        )

    );

Oracle笔记 三、function 、select的更多相关文章

  1. oracle 笔记---(三)__体系架构

    查看控制文件位置 SQL> show parameter control_files; NAME TYPE VALUE ------------------------------------ ...

  2. Oracle笔记 目录索引

    Oracle笔记 一.oracle的安装.sqlplus的使用 Oracle笔记 二.常用dba命令行 Oracle笔记 三.function .select Oracle笔记 四.增删改.事务 Or ...

  3. Oracle学习笔记三 SQL命令

    SQL简介 SQL 支持下列类别的命令: 1.数据定义语言(DDL) 2.数据操纵语言(DML) 3.事务控制语言(TCL) 4.数据控制语言(DCL)  

  4. 韩顺平Oracle笔记

    韩顺平Oracle笔记 分类: DataBase2011-09-07 10:24 3009人阅读 评论(0) 收藏 举报 oracle数据库sqljdbcsystemstring   目录(?)[-] ...

  5. oracle笔记

    一.sql*plus常用命令 (1)connect 用法:conn 用户名/密码@网络服务名[as sysdba/sysoper] 当特权用户连接时,必须带上as sysdba或是as sysoper ...

  6. Oracle笔记(1) 简单查询、限定查询、数据的排序

    Oracle笔记(四) 简单查询.限定查询.数据的排序   一.简单查询 SQL(Structured Query Language) 结构化查询语言,是一种数据库查询和程序设计语言,用于存取数据以及 ...

  7. muduo网络库学习笔记(三)TimerQueue定时器队列

    目录 muduo网络库学习笔记(三)TimerQueue定时器队列 Linux中的时间函数 timerfd简单使用介绍 timerfd示例 muduo中对timerfd的封装 TimerQueue的结 ...

  8. Oracle笔记 多表查询

    Oracle笔记  多表查询   本次预计讲解的知识点 1. 多表查询的操作.限制.笛卡尔积的问题: 2. 统计函数及分组统计的操作: 3. 子查询的操作,并且结合限定查询.数据排序.多表查询.统计查 ...

  9. 转:oracle笔记

    oracle笔记1 卸载oracle developer server的方法: 1-1 oracle卸载工具中卸载对应的oracleds项目:在注册表中搜索ORACLEDS HOME对应的别名,删除对 ...

随机推荐

  1. 把centos 的mysql 重装一下 把原来的lnmp删除,怎么备份还原数据库

    mysqldump --lock-all-tables -u root -p --databases mydb > /opt/database/mydb.sql,或者直接备份mysql的数据存储 ...

  2. Spring中bean的配置

    先从IOC说起,这个概念其实是从我们平常new一个对象的对立面来说的,我们平常使用对象的时候,一般都是直接使用关键字类new一个对象,那这样有什么坏处呢?其实很显然的,使用new那么就表示当前模块已经 ...

  3. Python从2.6升级到2.7,使用pip安装module,报错:No Module named pip.log(转载)

    From:http://blog.csdn.net/iefreer/article/details/8086834 python升级后,使用pip安装module,错误: 错误原因:版本升级后,之前的 ...

  4. DevExpress打印功能 z

    一.打印功能说明: 打印功能,我们有多种实现方式,可以根据需要自行选择,我简单的总结下两种方法. (1).使用微软.net框架自带的PrintDocument,这种方式是直接借助Graphics,自行 ...

  5. mac打开.caj格式文件

    以为用mac后使用会变得更方便些,写毕设时终于派上用场,可惜啊,mac竟然打不开.caj文件,这意味着什么?相信所有在做毕设的小伙伴们都能懂其中的凄凉.特别是硕士或博士的论文,你得从知网上下上百篇的文 ...

  6. 最完美的xslt数值函数与字符串函数(转)

    http://www.cnblogs.com/guoxu/articles/1744007.html 任何的编程语言或者是SQL语句都有内置的函数或方法,而强大灵活的xslt技术也是如此.熟练掌握XS ...

  7. c# WMI获取机器硬件信息(硬盘,cpu,内存等)

    using System; using System.Collections.Generic; using System.Globalization; using System.Management; ...

  8. [Java] 读写字符串数据

    package test.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...

  9. [ActionScript 3.0] AS3 3D星形贴图

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.MovieCl ...

  10. [ActionScript3.0] 深表复制

    function clone(obj:Object):Object{ var byteArray:ByteArray = new ByteArray(); byteArray.writeObject( ...