一、等值连接
1.查询员工的名字、部门编号、部门名字
select emp.last_name,dept.id,dept.name
from s_emp emp,s_dept dept
where emp.dept_id =dept.id;
2.查询部门的 id,名称以及所在区域的名称
select dept.id,dept.name,re.name
from s_region re,s_dept dept
where dept.region_id =re.id;
3.查询每个员工所在的区域
select emp.last_name,re.name
from s_emp emp,s_region re,s_dept dept
where emp.dept_id=dept.id
and dept.region_id=re.id;
4.查询Ngao所在的部门名称以及相对应区域名称
select emp.last_name,dept.name dept_name,re.name region_name
from s_emp emp,s_dept dept,s_region re
where emp.dept_id =dept.id
and
dept.region_id = re.id
and
emp.last_name ='Ngao';
5.查询工资大于1200的员工所在部门区域
select emp.last_name,emp.salary,re.name
from s_emp emp,s_dept dept,s_region re
where emp.dept_id =dept.id
and
dept.region_id = re.id
and
emp.salary >1200;
二、不等值连接
1.查询员工的工资等级名称
select emp.last_name,grade.gradeName
from s_emp emp,s_grade grade
where emp.salary>=grade.losal
and
emp.salary<=grade.hisal;
或者
select emp.last_name,grade.gradename
from s_emp emp,s_grade grade
where emp.salary between grade.losal andgrade.hisal;
三、外连接
1.左外连接
查询员工所在部门,没有部门的员工也要查询出来
select emp.last_name,dept.id
from s_emp emp, s_dept dept
where emp.dept_id =dept.id(+);
或者
select emp.last_name,dept.id
from s_emp emp left joins_dept dept
on emp.dept_id =dept.id(+);
2.右外连接
查询每个部门所对应的员工,没有员工的部门也要查询出来
select emp.last_name,dept.id
from s_emp emp,s_dept dept
where emp.dept_id(+) =dept.id;
或者
select emp.last_name,dept.id
from s_emp emp right joins_dept dept
on emp.dept_id(+) =dept.id;
3.全连接
查询所有的员工,以及对应的部门的名字,没有任何员工的部门也要显示出来,没有部门的员工也要显示出来
select emp.last_name,dept.id,dept.name
from s_emp emp full joins_dept dept
on emp.dept_id =dept.id;
1. 自连接
select emp1.last_name,emp2.last_name
from s_emp emp1,s_emp emp2
where emp1.id =emp2.manager_id;
四、结果集
◆union 获得俩个结果集的【并集】
select emp.last_name,dept.id
from s_emp emp, s_dept dept
where emp.dept_id =dept.id(+)
union
select emp.last_name,dept.id
from s_emp emp,s_dept dept
where emp.dept_id(+) =dept.id;
◆union all 把俩个结果集合在一起显示出来
select emp.last_name,dept.id
from s_emp emp, s_dept dept
where emp.dept_id =dept.id(+)
union all
select emp.last_name,dept.id
from s_emp emp,s_dept dept
where emp.dept_id(+) =dept.id;
◆minus 第一个结果集除去第二个结果集和它相同的部分【差集】
select emp.last_name,dept.id
from s_emp emp, s_dept dept
where emp.dept_id =dept.id(+)
minus
select emp.last_name,dept.id
from s_emp emp,s_dept dept
where emp.dept_id(+) =dept.id;
◆intersect 求俩个结果集的【交集】(公共部分)
select emp.last_name,dept.id
from s_emp emp, s_dept dept
where emp.dept_id =dept.id(+)
intersect
select emp.last_name,dept.id
from s_emp emp,s_dept dept
where emp.dept_id(+) =dept.id;
五、伪列rownum
(1)rowmun 能等于1
查询第一列数据
select*
from s_emp
where rownum = 1
(2)rownum 能大于0
select*
from s_emp
where rownum >0
rownum >=1 也是可以的
select*
from s_emp
where rownum >=1
(3)rownum小于任何数
select*
from s_emp
where rownum <=5
六、分页
(1) 查询第4条到第6条的数据
select*
from s_emp
where rownum <=6
minus
select*
from s_emp
where rownum <=3
(2)查询第2条数据
select emp.*
from s_emp emp
where rownum <= 2
minus
select emp.*
from s_emp emp
where rownum <= 1;
七、组函数
1.不使用groupby
select max(last_name)
from s_emp;
select min(last_name)
from s_emp;
select avg(last_name)
from s_emp;
select sum(last_name)
from s_emp;
计算有多少条数据
select count(last_name)
from s_emp;
标准差
select stddev(salary)
from s_emp;
方差
select variance(salary)
from s_emp;
2.使用group by
(1)查询员工表中每个部门的平均工资
select dept_id,avg(salary)
from s_emp
group by dept_id;
(2)查询员工表中每个部门员工的人数
select dept_id,count(*)
from s_emp
group by dept_id;
3.Having
(1)查询区域id以及名字,同时显示该区域所有员工工资总和
select r.id,r.name,sum(salary)
from s_region r,s_emp e,s_dept p
where r.id=p.region_id ande.dept_id = p.id
group by r.id,r.name;
(2)查询s_emp表中部门的平均工资大于等于1400的部门
select dept_id,avg(salary)
from s_emp
group by dept_id
having avg(salary) >=1400;
(3)求部门平均工资大于1000的信息,并按照部门|平均工资排序输出
select e.dept_id,avg(salary)
froms_dept d,s_emp e
where d.id = e.dept_id
group bye.dept_id
having avg(salary)>1000
order by avg(salary);
having/where的区别
where:
不能够单独使用
where紧跟在from
where在分组之前去执行,不能够出现组函数
having:
不能够单独使用
紧跟在group by
可以出现组函数
where为什么不能够出现组函数:
组函数是在分组以后执行
分组:
默认分组:
将整列认为是一个大组
group by:
where 在这个语句执行之前执行
select执行顺序
select
from
where
group by
having
order by
1.from
2.where
3.group by
4.having
5.select
6.order by
组函数可以出现的位置:
group by执行的语句都可以出现
having:
对分组后的数据进一步筛选