-- 查询所有表
select * from tab;
select * from user_tables;
-- 查看表中的字段信息
-- 命令行方式中执行:desc emp;
-- 给表添加注释
comment on table emp is '雇员信息表';
-- 给字段添加注释
comment on column emp.ename is '员工姓名';
-- 查询emp表中所有数据
select * from emp;
-- 查询部门编号为10的员工的信息
select * from emp where deptno = 10;
-- 去重查询 关键字distinct
select distinct * from emp;
-- 给表和字段起别名
select e.empno as 员工ID, e.ename as 员工姓名
from emp e
where e.ename = 'KING';
-- as可以省略
select e.empno 员工ID, e.ename 员工姓名
from emp e
where e.ename = 'KING';
pass
-- any:代表其中的任何一个满足即是满足
select * from emp where sal = any(1000, 2000, 3000);
-- some:和any差不多
select * from emp where sal = some(1000, 2000, 3000);
-- all:满足全部即满足
select * from emp where sal = all(1000, 2000, 3000);
pass
-- between and
select * from emp where sal between 1000 and 3000;
select * from emp where sal in (1000, 3000);
select * from emp where sal not in (1000, 3000);
and的优先级要高于or,所以or操作的时候要使用括号.
比如查询部门编号为10或者20的员工信息
select *
from emp
where exists (select deptno
from dept
where (deptno = 10 or deptno = 20)
and dept.deptno = emp.deptno);
可以当成外循环与条件判断的关系,exists子查询就是条件,外部每次遍历的结果如果满足子查询结果即满足.
-- 模糊查询
select * from emp where ename like 'S%';
-- escape转义指定的符号和字符,比如查询姓名中带有%号的雇员信息
select * from emp where ename like '%/%%' escape('/');
like的效率问题:低,尽量别用
-- 练习
-- 查询部门编号为10的员工信息
select * from emp where deptno = 10;
-- 查询年薪大于3w的员工信息
select * from emp where sal * 12 + nvl(comm, 0) > 30000;
-- 查询comm为null的员工信息
select * from emp where comm is null;
-- 查询sal大于1500并且有comm的员工信息
select *
from emp
where sal > 1500
and comm is not null;
-- 查询sal大于1500或者有comm的员工信息
select *
from emp
where sal > 1500
or comm is not null;
-- 查询姓名中包含S的员工信息
select * from emp where ename like '%S%';
-- 查询姓名以J开头,第二个字母为O的员工信息
select * from emp where ename like 'JO%';
-- 查询姓名中包含%的员工信息
select * from emp where ename like '%/%%' escape('/');
-- 查询部门名称为SALES和RESEARCH的雇员信息
-- 方式1:子查询
select *
from emp
where deptno in (select deptno
from dept
where dname = 'SALES'
or dname = 'RESEARCH');
-- 方式2:关联查询
select *
from emp e, dept d
where e.deptno = d.deptno
and (d.dname = 'SALES' or d.dname = 'RESEARCH');
-- 方式3:left join
select *
from emp
left join dept
on emp.deptno = dept.deptno
where dname in ('SALES', 'RESEARCH');
-- 方式4:exists()
select *
from emp
where exists (select *
from dept
where dname in ('SALES', 'RESEARCH')
and emp.deptno = dept.deptno);
1,数字顺序
2,字段顺序
-- order by 效率问题
select * from emp order by sal asc;
select * from emp order by sal desc;
order by存在效率问题,当需要执行这些业务操作时,尽量在业务低峰时期执行.
union all:包含两个集合的所有(不去重)
union:包含两个集合的所有(去重)
intersect:包含两个集合都有的元素
minus:仅包含A集合中有的元素
-- 全集
select *
from emp
where sal is null
union all
select *
from emp
where sal is not null;
-- 并集
select *
from emp
where sal is null
union
select *
from emp
where sal is not null;
-- 交集
select *
from emp
where sal is null
intersect
select *
from emp
where sal is not null;
-- 差集
select *
from emp
where sal is null
minus
select *
from emp
where sal is not null;
--练习
-- 查询dept表中所有部门的名称
select dname 部门名称 from dept;
-- 查询emp表显示所有员工姓名以及其全年收入,并指定列名称为年收入
select ename 姓名, (sal + nvl(comm, 0)) * 12 年收入 from emp e;
-- 查询不存在雇员的所有部门编号
select dept.deptno
from dept
where not exists (select * from emp where emp.deptno = dept.deptno);
-- 查询emp表,显示工资超过2850的员工姓名和工资
select e.ename, e.sal from emp e where e.sal > 2850;
-- 查询工资不在1500~2850的员工信息
select * from emp where sal not between 1500 and 2850;
-- 查询empno为7566雇员姓名以及部门代码
select ename, deptno from emp where empno = 7566;
-- 查询部门编号为10和30中工资超过1500的雇员信息
select * from emp where sal >1500 and deptno = any(10,30);
-- 查询员工姓名第二个字符为A的员工信息
select * from emp where ename like '_A%';
-- 查询comm不为null的所有员工信息
select ename,comm from emp where comm is not null;
-- 获取所有姓名,工资,入职时间,并以雇员名称升序排列
select ename,sal,hiredate from emp order by ename asc;
-- 获取指定日期入职的雇员信息
select ename, job, hiredate
from emp
where hiredate between to_date('1981/2/1', 'yyyy-mm-dd') and
to_date('1981/5/1', 'yyyy-mm-dd')
order by hiredate;
-- 查询所有有补助的员工姓名,工资,补助,并按照工资升序,补助降序排列
select ename, sal, comm
from emp
where comm is not null
order by sal asc, comm desc;
-- 字符函数
-- 字符串拼接
select concat('ename is ', ename) from emp;
-- 首字母大写
select initcap(ename) from emp;
-- 全部大写
select upper(ename) from emp;
-- 全部小写
select lower(ename) from emp;
-- 左/右填充
select lpad(ename,10,'*') from emp;
select rpad(ename,10,'*') from emp;
-- 去除空格(两边,左边,右边)
select trim(ename) from emp;
select ltrim(ename) from emp;
select rtrim(ename) from emp;
-- 获取字符出现的位置(从1开始)
select instr('weiwenlong','w') from dual;
select instr(ename,'A') from emp;
-- 查看字符串的长度
select ename, length(ename) from emp;
-- 子串获取
select substr(ename,0,3) from emp;
-- 替换操作
select replace('weiwenlong','wei','666') from dual;
-- 四舍五入 round(num,保留小数点后几位)
select round(3.1415926,2) from dual;
select round(3.1415926,3) from dual;
-- 截断数据 trunc(num,保留小数点后几位)
select trunc(3.1415926,2) from dual;
select trunc(3.1415926,3) from dual;
-- 取模
select mod(9,2) from dual;
-- 向上取整/向下取整
select ceil(3.14) from dual;
select floor(3.14) from dual;