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

初学oracle 数据库的初级运用 单表查询的各种基础方法(SQL)

2017-08-17 19:07 477 查看
-- 这是注释
/*
多行注释
*/
-- 关系:整个二维表
-- 关系名:表格名称
-- 元组:行数据(记录)
-- 属性:列数据(字段)
-- 属性名:列名称(字段名)
-- 主键:唯一确定元组的属性组(关键字)
-- 域:属性的取值范围基础 必须会背会默写
-- 单表查询
-- 查询员工表的所有信息
select * from emp;
-- 查询部门表的所有信息
select * from dept;
-- 查询工资表的所有信息
select * from salgrade;
-- 查询奖金表的所有信息
select * from bonus;
-- 查询员工表中的员工编号,姓名,工资
select  empno,ename,sal from emp;
-- 字段别名
-- 重命名查询结果中的字段,以增强可读性
-- 别名如果含有空格或其他特殊字符或大小写敏感,需用双引号引起来。
-- AS可以省略  


-- 使用字段别名
select empno as 员工编号,ename as 员工姓名 from emp;
-- as 可以省略
select empno 员工编号,ename 员工姓名,sal 月薪,sal*12 年薪  from emp;
-- 连接运算符‘||’可以把列与字符、或其它表达式连接在一起,得到一个新的字符串,实现‘合成’列的功能。
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;
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 查询
/*注意:
字符串和日期值要用单引号扩起来                  
字符串大小写敏感
日期值格式敏感,缺省的日期格式是'DD-MON-YY'
-- 查询名字叫SCOTT的员工信息*/
select * from emp where ename  = 'SCOTT';
-- 查询工资为1250的员工信息
select * from emp where sal = 1250;
-- 查询入职时间为1981-2-22的员工信息
select * from emp where hiredate = '22-2月-1981';
/*SQL优化问题:
AND:  把检索结果较少的条件放到后面
OR:  把检索结果较多的条件放到后面*/
-- 查询入职时间大于1980-1-1的员工信息
select * from emp where hiredate > '1-1月-1980';
-- 查询工资在1250及以上的员工信息
select * from emp where sal >= 1250;
-- 查询工资不等于800的员工信息
select * from emp where sal <> 800;
select * from emp where sal != 800;
-- between...and...介于两者之间(包括边界)
-- 查询工资介于800-1000之间的员工信息
select * from emp where sal 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,1250,1000);
select * from emp where sal = 800 or sal = 900 or sal = 1250 or sal = 1000;
-- 查询奖金为空的员工信息
-- is null 不存在;  = '' 空房子 代表写过信息又删除的 曾经存在过
select * from emp where comm is null;
-- 查询奖金不为空的员工信息
select * from emp where comm is not null;
-- like 模糊查询
-- 通配符 % 表示零个或多个 ; _表示一个字符
-- 查询以S开头的员工姓名的员工信息
select * from emp where ename like 'S%';
-- 查询姓李的员工的基本信息
select * from emp where ename like '李%';
-- 查询名字为三个字并且姓李的员工
select * from emp where eanme like '李__';
-- 查询名字为两个字并且以李结尾的员工
select * from emp where ename like '_李';
-- 查询名字第二个字是李的员工
select * from emp where ename like '_李%';
-- 查询名字倒数第二个字是李的员工
select * from emp where ename like '%李_';
-- 查询特殊字符要用escape声明
-- 查询名字中间有\的员工
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;
-- 左移除
select ltrim('123admin','123') from dual;
-- 右移除
select rtrim('123admin','admin') from dual;
-- 左右移空格
select rtrim(ltrim('   123admin   ',' ')) from dual;
-- 翻译 translate('jack','abcd','1234') 有a就换成1,b换成2,c换成3,d换成4
select translate('jack','abcd','1234') from dual;
-- 替换 replace(所选全面替换)
select replace('jackjack','ac','bl') from dual;
-- 查找出现的位置 instr(第一次出现的位置)
select instr('adminin','i') from dual;
-- 查找出现的位置 instr 从第五个开始之后第一个出现的位置
select instr('adminin','i',5) from dual;
-- 截取字符串 从第三个开始(包含第三个) 截取5个(不是截取到第五个)substr
select substr('abcdefghijk',3,5) from dual;
-- 字符串连接 concat
select concat('admin','123456') from dual;
select 'admin'||'123456' from dual;
-- 数值函数
-- 绝对值 abs
select abs(-25) from dual;
-- x的y次幂 power
select power(2,3) from dual;
-- 向上取整 ceil
select ceil(12.5) from dual;
-- 向下取整 floor
select floor(12.5) from dual;
-- round 四舍五入
select round(12.5) from dual;
-- trunc 截断
select trunc(12.5) from dual;
-- sqrt 开平方
select sqrt(9) from dual;
-- mod 取舍数
select mod(12,5) from dual;
-- sign 取符号(正数返回1,负数返回-1,0返回0)
select sign(-125) from dual;
-- 日期函数 当前日期 sysdate
-- 两个日期间隔的月份 moths_between
select months_between(sysdate,'8-4月-94') from dual;
-- 修改月份 add_months
select add_months(sysdate,1) from dual;-- 加1月
select add_months(sysdate,-1) from dual;-- 减1月
-- next_day 返回指定日期后第一个星期几的日期
select next_day(sysdate,'星期六') from dual;
-- last_day 返回指定日期的这个月的最后一天
select last_day(sysdate) from dual;
-- 日期做四舍五入round
-- 年 按月份数超过一半按下一年第一天
select round(sysdate,'YEAR') from dual;
-- 月 按月天数超过一半按下一月第一天
select round(sysdate,'month') from dual;
-- 天 按星期数超过一半按下周一
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 hh12-mi-ss AM DY') from dual;
-- to_date 字符串转日期 注意:转移日期的汉字要用双引号引起来
select to_date('2017年8月16日 22点10分20秒','yyyy"年"mm"月"dd"日" hh24"点"mi"分"ss"秒"') from dual;
-- to_number 字符串转数值类型(钱币类型)
select to_number('$124,567.45','$999,999,999.999') from dual;
-- 本地转法 ¥125,345.56
select to_number('¥125,345.56','L999,999,999.999') from dual;
-- to_char 数值转字符串
select to_char(1232144.75,'L999,999,999.999') from dual;
-- 时间比较问题
select * from emp where hiredate > '1-1月-81';
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为空转n,不为空转m
select ename,nvl2(comm,'没有','有') from emp;
-- decode
select decode(sal,3000,'还不错',5000,'土豪','穷啊') from emp;
-- case when then else end (相当于java中的if else)
select (case
when sal >= 5000 then '5有钱'
when sal >=4000 then '4有钱'
when sal >= 3000 then '3有钱'
when sal >= 2000 then '2有钱'
else '穷'
end)from emp;
-- 相当于switch case的例子
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 sum(sal) from emp;
-- 查询平均工资
select avg(sal) from emp;
-- 查询一共有多少员工
select count(*) from emp;
-- 聚合函数可以写在一行上
select max(sal),min(sal),sum(sal),avg(sal),count(*) from emp;
-- count(*) 统计所有
select sum(sal)/count(*),avg(sal) from emp;
-- count(exp) 统计非空的exp的个数
select count(sal) from emp;
-- count(distinct exp)统计非空不重复的exp的个数
select count(sal),count(distinct sal) from emp;
-- 分组函数 group by
-- 查询部门的平均工资
select deptno,avg(sal) from emp group by deptno;
select deptno,job,avg(sal) from emp group by deptno,job;
-- having 分组后的条件 效果和where相同 但必须用在group by之后
-- 查询每个部门的平均工资 要求平均工资大于2000
select deptno,avg(sal) from emp group by deptno having avg(sal) > 2000;
-- where 过滤行  having 过滤分组
-- select * from 表名 where 单行条件 group by 分组 having 分组条件 order by 排序
-- 查询部门编号为10的部门编号和平均工资
select deptno,avg(sal) from emp where deptno = 10 group by deptno;
select deptno,avg(sal) from emp group by deptno having deptno = 10;
-- 统计人数小于5的部门的平均工资
select deptno,avg(sal) from emp group by deptno having count(*) < 5;
-- 统计各部门的最高工资,排除最高工资小于3000的部门
select deptno,max(sal) from emp group by deptno having max(sal) >= 3000;

针对今天所学几道练习题

-- 查询部门编号为10的员工信息
select * from emp where deptno = 10;
-- 查询年薪大于3万的人员的姓名与部门编号
select ename,deptno from emp where sal*12 > 30000;
-- 查询佣金为null的人员姓名与工资
select ename,sal from emp where comm is null;
-- 查询工资大于1500 且 and 含有佣金的人员姓名
select * from emp where sal > 1500 and comm is not null;
-- 查询工资大于1500 或 or含有佣金的人员姓名
select ename,sal from emp where sal > 1500 or comm is not null;
-- 查询姓名里面含有 S 员工信息 工资、名称
select ename,sal from emp where ename like '%S%';
-- 求姓名以J开头第二个字符O的员工姓名的与工资
select ename,sal from emp where ename like 'JO%';
-- 求包含%的雇员姓名
select ename from emp where ename like '%_%%' escape '%'and comm is not null;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐