使用in 和exists
- in
- exists
in:多个条件筛选
select * from score where score in (60,70,90)
exists:判断是否(如果括号里返回true就执行,返回flase就不执行)
select * from score where exists (select * from score where id= 99)
使用修改过的比较运算符
运算符 | 描述 |
---|---|
>ALL | 表示大于列表中的最大值 表达式 | 列名 >ALL (10,20,30) 大于30的 |
>ANY | 表示大于列表中的最小值 表达式 | 列名 >ANY (10,20,30) 大于10的 |
=ANY | 表示列表中的值,和in的作用相同 表达式 | 列名 =ANY (10,20,30) 表示 等于10 等于20 等于30的 |
<>ANY | 表示不等于列表中的任何值 表达式 | 列名 <>ANY (10,20,30) 表示不等于10,20,30任意一个 |
<>ALL | 表示不等于列表中的所有值,和not in一样的 表达式 | 列名 >ANY (10,20,30) 大于10的 表示不等于10,20,30 |
select * from score where score >ALL(select score from score where score > 40)
使用聚合函数
查询大于平均分的学生
select * from score where score >(select avg(score) from score )
select * from score where score >ALL(select avg(score) from score )
使用嵌套子查询
当一个查询的条件依赖与另一个查询的结果
比如查询成绩大于60分的学生名称 需要用到student和socre表
select * from student where id in (select s_id from score where score>60)