11 子查询

时间:2020-02-16 10:37:41   收藏:0   阅读:45
select name from department where  id in
(select dep_id from employee 
group by dep_id
having avg(age)>25)
;

#查询技术部门员工的姓名

select name from employee where dep_id=(
select id from department where name=技术
)
;

#查询不足1人的部门名

select name from department where id not in (
select distinct dep_id from employee
);

 

#查询大于所有人平均年龄的员工名与年龄

select name, age from employee where age> (
select avg(age) from employee);

 

#带EXISTS关键字的子查询

select * from employee
where EXISTS
(select id from department where name=‘IT‘);

 

#查询每个部门最新入职的员工

select * from employee as t1
inner join
(select post, max(hire_date) as max_hire_date from employee 
group by post) as t2
on t1.post = t2.post
where t1.hire_date =t2.max_hire_date
;

  

 

原文:https://www.cnblogs.com/zhujing666/p/12315606.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!