sql复杂查询

内连接

左外连接 Left Outer Join On  ,无论右边是否匹配到,左边的数据都在

右外连接 Right Outer Join On ,无论左边是否匹配到,右边的数据都在

子查询: 将一个查询作为另一个查询的一部分

查询student表中年龄最大学员的信息 select * from student where age = (select max(age) from student);

IN/EXISTS 存在

查询所有成绩小于60分的同学名称

select name from student where id in (select student_id from studentcource where score < 60);

select name from student where exists (select * from studentcource where score < 60 and student.id = studentcource.student_id);

exists的查询效率一般高于in ,实际开发中,建议多使用exists

ANY、SOME、ALL

ANY 部分数据 >any(1,2,3)  大于最小的
ALL 所有数据 > all(1,2,3)   大于最大的

groupby 按照某个条件分组,形成一个新的结果集,多和having联用。having根据条件过滤分组的数据。

比如 搜索学生总成绩大于等于500分的学生id

   SELECT student_id, total_score
   FROM student_score
   GROUP BY student_id
   HAVING total_score>=500

UNION [ALL] 取结果集并集 有all的时候包含所有不去重,没有all,去重。

原文地址:https://www.cnblogs.com/javabigdata/p/5625603.html