1.where型子查询:
select cat_id,good_id,good_name from goods where good_id in (selctmax(good_id)from goodsgroupby cat_id);
2. from 型子查询:
select * from (select cat_id,good_id,good_namefrom goodsorderby cat_idasc, good_iddesc) as temp group by cat_id;
3.from和where型综合练习:
查出挂科2门及以上同学的平均分:
思路讲解:
select c.name ,avg(c.score) from cenjibiao c,(select name ,count(*) from cejibiao where score < 60 group by name having count(*)>=2) t where c.name = t.name group by c.name ; ;
4.in子查询:查询年龄为20岁的员工部门
select * from department where did in(SELECT did from employee where age=20);
5.exists子查询:查询是否存在年龄大于21岁的员工
select * from department where EXISTS (SELECT did from employee where age>21);
6. all子查询:查询满足条件的部门
select * from department where did> all(SELECT did from employee );
7比较运算符子查询:查询赵四是哪个部门的
select * from department where did= all(SELECT did from employee where name='赵四');
总结:
where型子查询:指把内部查询的结果作为外层查询的比较条件。
from型子查询:把内层的查询结果当成临时表,供外层sql再次查询。
in子查询:内层查询语句仅返回一个数据列,这个数据列的值将供外层查询语句进行比较。
exists子查询:把外层的查询结果,拿到内层,看内层是否成立,简单来说后面的返回true,外层(也就是前面的语句)才会执行,否则不执行。
any子查询:只要满足内层子查询中的任意一个比较条件,就返回一个结果作为外层查询条件。
all子查询:内层子查询返回的结果需同时满足所有内层查询条件。
比较运算符子查询:子查询中可以使用的比较运算符如 “>” “
来自:https://blog.csdn.net/qq_39380737/article/details/81127497