链接数据库:sqlplus tiger/scott
查询emp表中的数据:select * from emp
设置每行显示的数据长度:set linesize 300
设置每页显示的数据行数:set pagesize 30
模式名称几乎等同于表名称
Host调用本机命令
查询所有的表:select * from tab;
查询一个表的结构:desc 表名称
col ename for A10 设置ename显示的字节
col job gor A10 设置job显示的字节
简单查询:
②select
①from
查询emp表中的全部记录:select * from emp
查询每个雇员的编号、姓名、职位、工资:select empno,ename,job,sal from emp;
查询所有的职位信息:select job from emp
消除掉重复的内容:select distinct from emp
查询每个雇员的编号、姓名、基本年薪:select empno,ename,sal*12 sumsal from emp col sal for A10
要求显示出每个雇员的编号、姓名、基本年薪(15个月工资,200元饭补/月,100元交通/月,100元电话/月,5个月没有200元高温补贴):select empno,ename,(sal*15+(200+100+100)*12+200*5) sumsal from emp
常量:①:字符串使用单引号声明②:数字直接编写③:日期按照xx日-xx月-xx年,例如:'17-12月-80'
直接查询常量:select '雇员',empno,ename from emp
||连接:select empno || ename from emp
转换显示格式:效果:雇员编号:7369,姓名:smith,收入:800
select '雇员编号:' || empno || ',姓名:' || ename || ',收入:' || sal info from emp
限定查询:
③select
①from
②where
关系运算符:
查询所有基本工资高于1500的雇员信息
select *
from emp
where sal > 1500
查询smith的完整信息
select *
from emp
where ename = 'SMITH'
查询职位不是销售人员的雇员编号、姓名、职位
select empno,ename,job
from emp
where job != 'SALESMAN'
逻辑运算符:and or not
查询出工资范围在1500~3000之间的雇员信息
select *
from emp
where sal >= 1500 and sal <= 3000
查询工资大于2000或者职位是办事员的所有雇员信息
select *
from emp
where sal > 2000 or job = 'CLERK'
查询所有工资小于2000的雇员信息
select *
from emp
where not sal >= 2000
范围运算符:between...and
查询工资在1500~2000之间的雇员(包含1500,,2000)
select *
from emp
where sal between 1500 and 2000
查询所有在1981年雇佣的雇员
select *
from emp
where hiredate between '01-1月-1981' and '31-12月-81'
谓词范围:in , not in
要求查询出雇员编号是7369,7566,7788,9999的雇员信息
select *
from emp
where empno not in(7369,7566,7788,9999)
关于not in与null的问题:
在使用not in进行范围判断的时候,如果范围里面包含有null,那么不会有任何的结果返回
空判断:is null,is not null
查询所有领取佣金的雇员信息(佣金存在,不为空)
select *
from emp
where comm is not null
模糊查询:like
"_":匹配任意的一位字符
"%":匹配任意的零位、一位或多位字符
LIKE可以应用在各种数据类型上,不一定非要是字符串。
在使用Like模糊查询时,如果不设置关键字,表示查询全部
查询排序:order by是唯一一个可以使用select子句定义别名的子句
③select
①from
②where
④order by ASC (默认,按照升序的方式排列) DESC(按照降序的方式排列)
综合练习:
1:选择部门30中的所有员工
select *
from emp
where deptno = 30
2:列出所有办事员的姓名,编号和部门编号
select ename,emptno,deptno
from emp
where job = 'CLEAK'
3:找出佣金高于薪金的60%的员工
select *
from emp
where comm > sal*0.6
4:找出部门10中的所有经理和部门20中所有办事员的详细资料
select *
from emp
where (job = 'MANAGER' and deptno = 10) or (job = "CLEAK" and deptno = 20)
5:找出部门10中所有经理,部门20中所有办事员,既不是经理又不是办事员但其薪金大于或等于2000的所有员工的详细资料
select *
from emp
where (deptno = 10 and job = 'MANAGER') or (deptno = 20 and job = 'CLEAK') or (job != 'MANAGER' or job != 'CLEAK' and sal >= 2000)
select *
from emp
where (deptno = 10 and job = 'MANAGER') or (deptno = 20 and job = 'CLEAK') or (job not in('MANAGER','ClERK') and sal >= 2000)
6:找出收取佣金的员工的不同工作
select distinct job
from emp
where comm is not null
7:找出不收取佣金或收取的佣金低于100的员工
select *
from emp
where comm is null or comm < 100
8:显示不带有R的员工的姓名
select *
from emp
where ename not like "%R%"
9:显示姓名字段的任何位置包含"A"的所有员工的姓名,显示的结果按照基本工资由高到低排序,如果基本工资相同,则按照雇佣年限由早到晚排序,如果雇佣日期相同,则按照职位排序
select *
from emp
where ename like "%A%" order by sal desc,hiredate asc,job
简单查询遇到问题要多想,一步一步写出其过程,这样对解决问题有很大的好处