select * from 表 where 表(子查询语句)-- 查询出students中身高最高的男生。显示名字和身高select s.name, s.high from students s where high=(select max(high) from students) andgender="男";-- 查询出高于平均身高的学生信息select * from students where high>(select avg(high) from students);-- 查询学生班级号cls_id能够对应的学生信息select * from students where cls_id in (select id from students);-- 查询最大年龄的女生的idselect * from students where id=(select max(id) from students where gender="女") andgender="女";
在这里插入图片描述
自关联
简单理解为自己与自己进行连接查询
1
-- 查询广东省下的所有广东市select * from cities c inner join provinces p on c.provinceid=p.provinceid having p.province="广东省";-- 查询广东省下的所有广东市-- 自关联select * from areas a inner join areas b on a.id=b.pid having a.name="广东";