1.基本查询
//查询所有列
select * from emp;
//依靠列名查询
select empno,ename,job,mgr,hiredate,sal,comm,deptno from emp;
//查询中带有表达式
select empno,ename,sal,sal*12 from emp;
select empno,ename,sal,sal*12,comm,sal*12+comm from emp;
//nvl(a,b)sql中存在null 若a为null则a=b,否则为a
select empno,ename,sal,sal*12,comm,sal*12+nvl(comm,0)from emp;
//distinct去除重复记录,作用于后面所有的列
select distinct deptno from emp;
select distinct deptno,job from emp
2.连接符||
select ename||'的薪水是:'|| sal from emp;
select concat('hello','world') from dual;
3.sql中的字符串
sql中字符串大小写敏感
日期格式敏感
//查询日期格式
select * from v$nls_parameters;
alter session/system set NLS_DATE_FORMAT='yyyy-mm-dd'
// 会话中/系统中 修改
4.模糊查询like
% _
//模糊查询,将姓名中带有_的员工找出来 _是sql中的特殊字符,需要转议,使用escape 关键字将\进行转议
select * from emp where ename like '%\_%' escape '\' ;
5.排序
- asc 默认 升序
- desc 降序
- order by 列/表达式/别名/序号(select语句中列的值1,2,3,4…..)
多个列排序
- order by 作用于后边所有的列
- 先按照第一个列排序,若第一个列相同,再按照第二个列排序
- desc只作用于最近的列
6.多表查询
(笛卡儿积)
连接条件
- 2张表:1个条件
- 3张表:2个条件
- n张表:n-1个条件
多表查询
- 等值连接
- 不等值连接
- 外连接
- 自连接
--等值连接
--查询员工信息:员工号,姓名,月薪,部门名称
select * from emp;
select * from dept;
select e.empno,e.ename,e.sal,d.dname
from emp e,dept d
where e.deptno = d.deptno;
--不等值链接
--查询员工信息:员工号,姓名,月薪,工资级别
select * from salgrade;--工资等级表
select e.empno,e.ename,e.sal,s.grade
from emp e,salgrade s
where e.sal between s.losal and s.hisal;
--外连接
--按照部门统计员工人数:部门号,部门名称,人数
select d.deptno 部门号,d.dname 部门名称,count(e.deptno) 人数
from emp e,dept d
where e.deptno=d.deptno
group by d.deptno,d.dname;
--左外:当where e.deptno=d.deptno不成立的时候,等号左边的表依然被包含在内
--写法:where e.deptno=d.deptno(+)
--右外:当where e.deptno=d.deptno不成立的时候,等号右边的表依然被包含在内
--写法:where e.deptno(+)=d.deptno
select d.deptno 部门号,d.dname 部门名称,count(e.deptno) 人数
from emp e,dept d
where e.deptno(+)=d.deptno
group by d.deptno,d.dname;
--自连接:通过表的别名,将同一张表视为多张表
--查询员工信息: 员工的姓名 老板的姓名
select e.ename 员工姓名,b.ename 老板姓名
from emp e,emp b
where e.mgr=b.empno
order by b.ename;
--自连接不适合操作大表
--层次查询-树状图结构
select level,empno,ename,mgr
from emp
connect by prior empno=mgr
start with mgr is null
order by level;
7.子查询
注意问题:
- 括号
- 合理的书写风格
- 可以在住查询的where select having from 后面放置子查询
- 不可以在group by 后面放置子查询
- 强调from 后面的子查询
- 主查询和子查询可以不是同一张表,只要子查询返回的结果主查询可以使用即可
- 一般不在子查询中排序,但在TOP-N分析问题中,必须对子查询排序
- 一般先执行子查询,再执行主查询,但相关子查询例外
- 单行子查询只能使用单行操作符,多行子查询只能使用多行操作符
- 子查询中的null
--3.可以在主查询的where select having from后面放置子查询
select empno,ename,sal,(select job from emp where empno=7839) 第四列
from emp;
--向主查询中的having子句返回结果
select deptno,min(sal)
from emp
group by deptno
having min(sal)> (
select min(sal)
from emp
where deptno=10
);
--5.强调from后面的子查询
--查询员工信息:员工号,姓名,月薪
select *
from (
select empno,ename,sal
from emp
);
--查询员工信息:员工号,姓名,月薪,年薪
select * from (
select empno,ename,sal,sal*12 annlsal
from emp
);
--6.主查询和子查询可以不是同一张表;只要子查询返回的结果主查询可以使用即可
--查询名称是SALES部门的员工信息
select *
from emp
where deptno=(
select deptno
from dept
where dname='SALES');
--多行子查询中的null
--查询不是老板的员工信息(不是老板即员工号不在mar列的员工)
select *
from emp
where empno not in (select mgr from emp);
--查询是老板的员工信息
select *
from emp
where empno in (select mgr from emp);
--查询不是老板的员工信息(不是老板即员工号不在mar列的员工)
select *
from emp
where empno not in (
select mgr
from emp
where mgr is not null
);
单行子查询
单行操作符
- =
- >
- >=
- <
- <=
- <>
多行子查询
多行操作符
- IN 等于列表中的任何一个
- ANY 和子查询返回的任意一个值比较
- ALL和子查询返回的所有值比较
--单行子查询
select ename,job,sal
from emp
where job = (select job from emp where empno =7566)
and sal > (select sal from emp where empno=7782);
--在子查询中使用组函数
select ename,job,sal
from emp
where sal=(select min(sal) from emp);
--多表查询 查询名称是SALES部门的员工信息
select e.*
from emp e, dept d
where e.deptno=d.deptno and d.dname='SALES';
--in 在集合中
--查询部门名称是SALES和ACCOUNTING的员工
select *
from emp
where deptno in (
select deptno
from dept
where dname='SALES' or dname='ACCOUNTING'
);
select e.*
from emp e,dept d
where e.deptno=d.deptno and(d.dname='SALES' OR d.dname='ACCOUNTING');
--any 和集合中的任意一个值比较
--查询工资比30号部门任意一个员工高的员工信息
select *
from emp
where sal>any(
select sal
from emp
where deptno=30
);
--all: 和集合中的所有值比较
--查询工资比30号部门所有员工高的员工信息
select *
from emp
where sal>all(
select sal
from emp
where deptno=30
);