您的位置:首页 > 数据库 > Oracle

Oracle中的SQL基础查询语句--上(值得一看,例题非常多,也很清晰)

2017-08-16 18:27 555 查看
-- 这是注释

/*

多行

*/

select * from emp;

select * from dept;

select * from salgrade;

select * from bonus;

select p.ename,p.sal from emp p;

select p.*,p.ename,p.sal from emp p;

select emp.ename,emp.sal from emp;

-- 单表查询

-- 查询员工表的所有信息

select * from emp;

-- 查询部门表的所有信息

select * from dept;

-- 查询员工表中的员工编号,姓名,工资

select empno,ename,sal from emp;

-- 查询员工表中员工编号,姓名,工资,和年薪

select empno,ename,sal,sal*12 from emp;

-- as 别名(小名)

select empno as 员工编号,ename as 员工姓名 from emp;

-- as 可以省略

select empno 员工编号,ename 员工姓名,sal 月薪,sal*13 年薪 from emp;

select empno "eo" from emp;

-- 连接符 相当于java中的+号

-- 查询出员工“姓名的月薪是XXX”这种格式的数据

select ename || '的月薪是' || sal 简介 from emp;

-- 除去重复行 distinct

-- 查询各个员工的工资分布

select distinct sal from emp;

-- 查询员工和工资的分布

select distinct ename,sal from emp;

-- order by 进行排序 asc 升序(默认),desc 降序

-- 查询所有数据按照工资升序排序

select * from emp order by sal asc;

-- 查询所有数据按照工资降序排序

select * from emp order by sal desc;

-- 多字段排序,分为主和次

-- 查询所有员工数据先按照工资降序,在按照编号升序

select * from emp order by sal desc,empno asc;

select ename 姓名,sal 工资,empno 编号 from emp order by 工资 desc,编号 asc;

-- where 条件查询

-- 查询名字叫SCOTT的员工信息

select * from emp where ename = 'SCOTT';

-- 查询工资为1250的员工信息

select * from emp where sal = 1250;

-- 查询入职时间为1981-2-22的员工信息

select * from emp where hiredate = '22-2月-81';

-- 查询入职如期大于1980-1-1的员工信息

select * from emp where hiredate > '1-1月-80';

select * from emp where hiredate < '1-1月-80';

-- 查询工资在1250元及以上的员工信息

select * from emp where sal >= 1250;

-- 查询工资不等于800的员工信息

select * from emp where sal <> 800;

select * from emp where sal != 800;

-- between A and B 在 A和B之间(包含)

-- 查询员工工资在800和1000之间的员工信息

select * from emp where sal between 800 and 1000;

select * from emp where sal >= 800 and sal <=1000;

-- in(集合) 属于集合中的一个

-- 查询工资为800,900,1250,1000的员信息

select * from emp where sal in(800,900,1000,1250);

select * from emp where sal =800 or sal=900 or sal=1000 or sal=1250;

-- 查询奖金为空的员工信息

select * from emp where comm is null;

select * from emp where comm ='';

-- 查询奖金不为空的员工信息

select * from emp where comm is not null;

select * from emp where comm !='';

-- like 模糊查询 通配符 % 代表所有 _代表一个字符

-- 查找员工姓名以S开头的员工信息

select * from emp where ename like 'S%';

-- 查询姓李员工的基本信息

select * from emp where ename like '李%';

-- 查询名字为三个字并且姓李的员工

select * from emp where ename like '李__';

select * from emp where ename like '%\%' escape '\';

-- and or not 逻辑运算符

-- 查询工资大于900并且名字以S开头

select * from emp where sal>900 and ename like 'S%';

-- 查询工资大于900或者名字以S开头

select * from emp where sal>900 or ename like 'S%';

-- 查询工资不是800,900,1250的员工信息

select * from emp where sal not in(800,900,1250);

-- 查询名字不以S开头的员工信息

select * from emp where ename not like 'S%';

-- 常用字符函数

-- 查询员工表的姓名,并且首字母大写

select initcap(ename) from emp;

-- 查询员工表的姓名,全部转化为小写

select lower(ename) from emp;

-- 查询员工表的姓名,全部转化为大写

select upper(ename) from emp;

-- 左移除 ltrim

select ltrim('123admin','123') from dual;

-- 右移除

select rtrim('123admin','admin') from dual;

-- 左右移除空格

select ltrim(rtrim('  123admin  ')) from dual;

-- 翻译 translate

select translate('jack','aj','12') from dual;

-- 替换 replace

select replace('jaak','aa','bl') from dual;

-- 查找出现的位置instr 第一次出现的位置

select instr('adminin','i') from dual;

-- substr(eg,n,m) 截取字符串 n开始包含n 截取m个

select substr('abcdefghijk',3,5) from dual;

-- concat 字符串连接

select concat('admin','123456') from dual;

select 'admin'||'123456' from dual;

-- 数值函数

-- 绝对值

select abs(-25) from dual;

-- x的y次幂

select power(2,3)from dual;

-- ceil 向上取整

select ceil(12.5) from dual;

-- floor 向下取整

select floor(12.5) from dual;

-- trunc 截断:第二个参数可以为负数,表示为小数点左边指定位数后面的部分截去,即均以0记

select trunc(12.156,2) from dual;

-- round 四舍五入

select round(12.156,2) from dual;

-- sqrt 开平方

select sqrt(4) from dual;

select power(8,1/3)from dual;

-- mod 取余数

select mod(10,3) from dual;

-- sign 取符号

select sign(-25) from dual;

-- 日期函数

-- months_between 两个日期间隔的月份

select months_between(sysdate,'9-12月-1996') from dual;

-- add_months 修改月份

select add_months(sysdate,1) from dual;

select add_months(sysdate,-1) from dual;

-- next_day 返回指定日期后的一个星期几的日期

select next_day('9-12月-1996','星期日') from dual;

-- last_day 返回指定日期的这个月的最后一天

select last_day(sysdate) from dual;

-- 日期做四舍五入 round

select round(sysdate,'YEAR') from dual;-- 超过6月去下一年的第一天

select round(sysdate,'month') from dual;-- 超过月的一半去下一个的1号

select round(sysdate,'day') from dual;-- 超过星期的一半去下一周周日(第一天)

-- 日期做截断 trunc

select trunc(sysdate,'YEAR') from dual;-- 本年第一天

select trunc(sysdate,'month') from dual;-- 本月第一天

select trunc(sysdate,'day') from dual;-- 本周第一天

-- to_char 日期转字符串

select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;

-- to_date 字符串转日期

select to_date('2017年8月16日 16点40分30秒','yyyy"年"mm"月"dd"日" hh24"点"mi"分"ss"秒"') from dual;

-- to_number 字符串转数值类型 $99,999,999.99 ¥125,152.25

select to_number('$12,025,897.01','$999,999,999.99') from dual;

select to_number('¥125,152.25','L999,999,999.99') from dual;

-- to_char 数值转字符串 

select to_char(125152.25,'L999,999,999.99') from dual;

-- 时间比较问题

select * from emp where hiredate>'1-1月-81';

-- 1981-1-1

select * from emp where hiredate>to_date('1981-1-1','yyyy-mm-dd');

select * from emp where to_char(hiredate,'yyyy-mm-dd')>'1981-1-1';

-- nvl 空转数

select ename,nvl(comm,0) from emp;

-- nvl2(e,n,m) e为空转m 不为空转n

select nvl2(comm,'有','太惨了') from emp;

-- decode 

select decode(sal,1250,'有钱',5000,'真有钱','穷鬼') from emp;

-- case when then else end (if else)

select (case

         when sal >= 5000 then

          '有钱'

         when sal >= 4000 then

          '1有钱'

         when sal >= 3000 then

          '2有钱'

         when sal >= 2000 then

          '3有钱'

         else

          '穷'

       end)

       -- switch case

  from emp;

  select (case sex when 0 then '男' when 1 then '女' end) from dual;

  --sum 求和 max 最大 min 最小 avg 平均 count 统计

  -- 查询最大工资

  select max(sal) from emp;

  -- 查询最小工资

  select min(sal) from emp;

  -- 查询平均工资

  select avg(sal) from emp;

  -- 查询所有工资的和

  select sum(sal) from emp;

  -- 查询一共有几个员工

  select count(*) from emp;

-- 聚合函数可以写在一行上

select max(sal),min(sal),avg(sal),sum(sal),count(*) from emp;

-- 聚合函数不可以和单行数据同时展示

select ename,max(sal) from emp;

-- count(*) 统计所有 

select sum(sal)/count(*),avg(sal) from emp;

--count(exp)统计非空的exp的个数

--count(distinct exp)统计非空不重复的exp的个数

select count(sal),count(distinct sal) from emp;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: