带有比较运算符的子查询是指父查询与子查询之间用比较运算符进行连接。当用户确切知道内层查询返回的值是单值时,可以用>,<,=,>=,<=,!=,或<>等比较运算符。例如在例37中,由于一个学生只可能在一个系学习,也就是说内查询的结果是一个值,因此可以用 = 代替 in ,其SQL语句如下:
select sno,sname,sdept
from student
where sdept =
(select sdept
from student
where sname ='刘晨')
需要注意
的是,子查询一定要跟在比较符之后,下列写法是错误的
select sno,sname,sdept
from student
where (select sdept
from student
where sname ='刘晨')=sdept