子查询
将一个查询语句做为一个结果集供其他SQL语句使用,就像使用普通的表一样,被当作结果集的查询语句被称为子查询。所有可以使用表的地方几乎都可以使用子查询来代替。
select from (select from student where sAge<30) as t --被查询的子表必须有别名
where t.sSex =’男’ —对子表中的列筛选
转换为两位小数:CONVERT(numeric(10,2), AVG(english))
只有返回且仅返回一行、一列数据的子查询才能当成单值子查询。
select ‘平均成绩’, (select AVG(english) from Score) —可以成功执行
select ‘姓名’, (select sName from student) —错误,因为‘姓名’只有一行,而子表中姓名有多行
select * from student where sClassId in(select cid from Class where cName IN(‘高一一班’,’高二一班’)) —子查询有多值时使用in
