oracle--单表查询

---单表的查询学习
--查询表的所有数据 select * from 表名;*代表所有
select * from emp;
--查询表中指定字段的值 select 字段名1,字段名2,...from表名
select empno from emp;
select empno,ename from emp;
--给查询结果中的字段使用别名
--在字段名后使用关键字 字段名 as "别名"
--作用:方便查看查询结果
--注意:as关键字可以省略不写,别名中没有特殊字符双引号也可以省略不写。
select empno 员工编号,ename"员工 姓名",job as 工作,mgr as "领导编号" from emp;
--连接符:select 字段名||'字符'||字段名||..... from 表名
--||为sql语句的字符链接符,使用在select和from之间
--字符链接格式为 字段名||'字符'||字段名
--注意:一个拼接好的连接在结果集中是作为一个新的字段显示,可以使用别名优化字段显示。
select empno||'的姓名是'||ename as"信息",job||'哈哈'||mgr from emp;
--去除重复 select distinct 字段名,字段名,...fromn 表名
---注意:去除重复的规则是按照行进行去除的,多行数据完全相同取其一
select distinct job ,mgr from emp;
--排序
--单字段排序
--select * from 表名 order by 字段名 asc 升序排序 asc可以省略不写
--select * from 表名 order by 字段名 desc 降序序排序
--多字段排序
--select * from emp order by 字段名1,字段名2...
--先按照字段1排序,如果字段1的值相同,则按照字段2排序,....
select * from emp order by empno desc--单字段排序 降序
select empno,ename,job from emp order by ename asc--单字段排序 升序
select * from emp order by empno,ename--多字段排序
--字段的逻辑运算
--select关键字和from关键字之间的字段可以直接进行四则运算
--字段与字段之间也可以直接进行运算
--注意:字段值为数值类型
select * from emp
select empno,ename,job,sal*2+1000,sal+comm from emp
-----------------------------------------------------------------
--使用where子句查询筛选
--select 字段名,字段名,...from表名 where 筛选条件
--单筛选条件
--使用运算符进行筛选 =,>,>=,<,<=,<> 单个条件中
--注意:如果条件中的值为字符,必须使用单引号括起来
--查询所有的员工的工资信息
select empno,ename,sal+comm as 薪资 from emp
--查询SMITH的个人信息
select * from emp where ename='SMITH'
--查询SMITH的薪资信息,逻辑运算符=
select empno,ename,sal,sal+comm from emp where ename='SMITH'
--查询工资大于1000的员工信息,逻辑符>
select * from emp where sal>'2000'
--查询工资不等于3000的员工信息
select * from emp where sal<>3000 order by sal
--练习:
--查看工资等于1250的员工信息
select *from emp where sal='1250'
--查看工作等于CLERK的员工信息
select * from emp where job='CLERK'
--查看工资大于1250的员工姓名和工作
select ename,job from emp where sal>1250
--查看工资大于等于2000的员工信息
select * from emp where sal>=2000;
--查看工资小于等于2000的员工信息;
select * from emp where sal<=2000;
--查看工资不等于1500的员工信息
select * from emp where sal<>1500;
--查看入职日期在81年后的员工信息
--注意:oracle默认的日期格式为 日-月-年,示例'03-1月-1981'
select * from emp order by hiredate
select * from emp where hiredate>='01-1月-1981' order by hiredate
--多条件筛选(where子句关键字:and,or,like,is null,is not null, in ,between and)
--查询工资在2000-3000之间的员工信息
--使用and关键字,多条件同时成立的筛选使用and关键字进行条件连接
select * from emp where sal>=2000 and sal<3000
--使用between and 关键字进行条件连接,包含两头的数据
select * from emp where sal between 2000 and 3000
--查询工作为SALESMAN,ANALYST,MANAGER的员工信息
--使用or关键字,进行或条件的筛选。
select * from emp where job='SALESMAN' or job='ANALYST' or job='MANAGER' order by job
--使用in关键字,也可以进行或筛选,但是in中的内容只能为一个字段的值。
select * from emp where job in('SALESMAN','ANALYST','MANAGER')
--查询姓名中包含s的,以s开头的,以s结尾的,第二个字符为A的。(模糊查询)
--%号表任意多个的任意字符
--select * from 表名 where 字段名 like '%字符%' 查询包含指定字符的数据
select * from emp where ename like '%S%' --包含s的
--select * from 表名 where 字段名 like '字符%' 查询以指定字符开头的数据
select * from emp where ename like 'S%'--以S开头
--select * from 表名 where 字段名 like '%字符' 查询以指定字符结尾的数据
select * from emp where ename like '%S'--以S结尾的
--select * from 表名 where 字段名 like '_字符%' 查询指定位置为指定字符的数据
--_表示一个任意字符
select * from emp where ename like '_A%'--第二个字符为A的
--select * from 表名 where 字段名 like '%字符2字符1%' escape'字符2'
--escape将指定的字符变为转义字符
--转义字符可以将特殊字符转为普通字符
select * from emp where ename like '%/_%' escape '/'

select * from emp for update
--查询有津贴的员工信息
-- select * from 表名 where 字段名 is null 字段值为null
-- select * from 表名 where 字段名 is not null 字段值不为null
--多个条件使用and关键进行连接,筛选的是符合所有条件的数据
--select * from 表名 where 筛选条件1 and 条件2 and ....
select * from emp where comm is not null and comm>0

使用group by分组
				在多行函数中不能直接使用普通字段,除非group by
				在多行函数中不能直接使用单行函数,除非group by
				group by学习:
						 ---1、使用group by进行数据分组 select 多行函数,分组字段 from 表名 group by 分组字段
						 ---2、多字段进行分组的时候,按照字段顺序进行分组,第一条件分组完成后,继续使用其他条件依次分组。
						 ---3、group by依然可以和order by 联合使用
						 ---4、可以和单行函数联合进行分组,注意使用了单行函数那么在查询语句中必须也要使用
				查询最高工资和员工数
					select max(sal),count(*) from emp
				查询不同部门的最高工资
					select * from emp order by deptno
					select  deptno,max(sal) from emp group by deptno--使用group进行分组查询,分组的字段可以出现在查询中,其他字段依然不可以
				查询不同工作岗位的员工数
					select * from emp for update
					select lower(job),count(*) from emp group by lower(job)--使用单行函数进行分组
				查询不同部门的不同工作岗位的人数
					select deptno,job ,count(*) from emp group by deptno,job--使用多字段组合进行分组
					select deptno,job ,count(*) from emp group by deptno,job order by deptno
				查询不同部门的不同工作岗位的并且人数大于1的信息
					select count(*) from emp where count(*)>3 group by deptno
					select deptno,job ,count(*) from emp where count(*)>1 group by deptno,job order by deptno
				查询部门号大于10的不同部门的不同工作岗位的人数
					select deptno,job ,count(*) from  emp where deptno>10 group by deptno,job order by deptno
				使用having进行分组后筛选
				having学习:
					   --1、使用group by分组后在进行数据筛选的时候,where中不能出现多行函数,所以使用新的关键字having进行条件筛选
					   --2、where条件筛选的执行顺序:from-->where--->group -->select
					   --3、having条件筛选的执行顺序:from-->group by -->having-->select
					   --4、where的执行效率比having要高,能使用where的情况下尽量不要使用having
				查询不同部门的不同工作岗位的并且人数大于1的信息
				使用where语句进行筛选
				where条件语句sql执行顺序:from-->where--->group -->select
					select count(*) from emp where count(*)>1 group by deptno,job
				使用having语句进行筛选
				having条件语句的执行顺序:from-->group by -->having-->select
					select deptno, count(*) from emp  group by deptno having count(*)>5
					select deptno,job ,count(*) from  emp  group by deptno,job  having deptno>10 order by deptno

  

原文地址:https://www.cnblogs.com/eadela/p/11435374.html