sql子查询用法

在数据库的查询过程中,可能经常要用到子查询的情况,用得较多的可能是嵌套子查询,最近查资料时发现还有另外一种方法,就是在from子句中使用子查询,也可以实现相同的效果,下面通过一个实例来讲讲这两种方法的使用。
问题:查询员工工资高于其所在部门平均工资的员工的信息
1.使用嵌套子查询的方法。
select * from Employee e1
where e1.salary>
(select AVG(salary) from Employee e2
where e1.depId=e2.depId
group by e2.depId
)
 2.在from子句中使用子查询。在from子句中使用子查询时,必须给子查询指定别名。
select e1.* from
Employee e1,(select depId,avg(salary) avgsalary from Employee group by depName) e2
where e1.depId=e2.depId and e1.salary>e2.avgsalary
原文地址:https://www.cnblogs.com/moss_tan_jun/p/2025945.html