关联子查询

 select a.deptno, a.* from emp a  
where a.sal = (select max(b.sal) from emp b where b.deptno =  a.deptno)  
 
 
 --关联性数据库的特点 
 --查询部门中高于平均工资的
 
 --例如10部门 首先是进行外查询
 --然后一条一条的去看满足条件的
 --例如此时e的deptno=20
 --后面的此时只会将20的筛选出来进行平均值计算
 --计算完之后根据where 条件来返回我们要的结果
 --最终还是有外面的一条一条的数据来进行计算的
 select  * from emp e
  where  e.sal>(select avg(sal)  from  emp  where  deptno=e.deptno)

你可能感兴趣的:(关联子查询)