SQL查询
-
1 select * from scores
- 使用TOP限制结果集,WITH TIES作用-包括最后一行取值并列的结果
1 --查询english最高的前3名 2 select top 3 with ties id,chinese,math,english 3 from scores 4 order by english desc
1 --查询出现次数最多的chinese分数值 2 select top 1 with ties chinese, count(*) as 次数 --COUNT()需要和group by一起使用 3 from scores 4 group by chinese 5 order by count(*) desc
1 --查询总成绩前20%学生成绩 2 select top 20 percent with ties id, chinese, math, english, chinese+math+english as total 3 from scores 4 order by chinese+math+english desc
-
1 select id, chinese, math, english, chinese+math+english as total, level = 2 case 3 when chinese+math+english > 270 then 'A' 4 when chinese+math+english > 230 then 'B' 5 else 'C' 6 end 7 from scores 8 order by chinese+math+english desc
-
1 --将查询结果保存到新表中 2 --使用'#'将新表标识临时表,生存期与创建此局部临时表的用户的连接生存期相同,只在当前连接中使用 3 --使用'##'将新表标识全局临时表,生存期与创建此局部临时表的用户的连接生存期相同,可被所有的连接使用 4 --将上一步查询的结果存入新表 5 select id, chinese, math, english, chinese+math+english as total, level = 6 case 7 when chinese+math+english > 270 then 'A' 8 when chinese+math+english > 230 then 'B' 9 else 'C' 10 end 11 into global_scores --全局新表 12 from scores 13 order by chinese+math+english desc 14 go 15 select * from global_scores
1 --在永久表中查询level为B的学生 2 select * from global_scores 3 where level = 'B'
1 --将查询结果保存到新表中 2 --使用'#'将新表标识临时表,生存期与创建此局部临时表的用户的连接生存期相同,只在当前连接中使用 3 --使用'##'将新表标识全局临时表,生存期与创建此局部临时表的用户的连接生存期相同,可被所有的连接使用 4 --将上一步查询的结果存入新表 5 select id, chinese, math, english, chinese+math+english as total, level = 6 case 7 when chinese+math+english > 270 then 'A' 8 when chinese+math+english > 230 then 'B' 9 else 'C' 10 end 11 into #temp_scores --临时表 12 from scores 13 order by chinese+math+english desc 14 go 15 select * from #temp_scores --可像永久表一样查询 16 where level = 'B'
- 并运算UNION
1 select * from global_scores 2 where level = 'B' 3 union 4 select * from global_scores 5 where level = '
- 交运算INTERSECT
1 select * from scores where chinese = 95 2 intersect 3 select * from scores where math = 64
- 差运算EXCEPT
select * from scores where chinese = 95
1 select * from scores where chinese = 95 2 intersect 3 select * from scores where math = 64
- 使用子查询进行基于集合的测试
1 --子查询,查询与id为2的学生chinese成绩相同的学生 2 select * from scores 3 where chinese in 4 ( 5 select chinese from scores where id = 2 6 ) 7 and id != 2
- 使用子查询进行比较测试
-
1 --查询成绩最高的 2 select *, chinese+math+english as total 3 from scores where chinese+math+english = 4 ( 5 select MAX(chinese+math+english) from scores 6 )
1 --查询成绩高与平均分的学生 2 select *, chinese +math + english as total from scores 3 where chinese + math + english > 4 ( 5 select AVG(chinese +math + english) from scores 6 )
-
使用子查询进行存在性测试
1 select * from info
1 --查询名字为'd'的学生成绩 2 select * from scores 3 where exists 4 ( 5 select * from info where scores.id = info.id and info.name = 'd' 6 )
以下情况起不到选择作用,会返回所有结果1 select * from scores 2 where exists 3 ( 4 select * from scores where chinese > 80 5 )
改成这样则可以,可能因为子查询只查一个表的时候,会导致exists返回的值总是true或总是false的情况(对于每一层外查询,exists内执行的子查询是一样的),而与外查询中的表连接后,因为每一层外查询对应的值不同,所以每一次执行的子查询结果都不同,exists会有true或false,达到选择的目的
如上面一个查询语句中的select * from scores where chinese > 80,该语句总是true,因为一直存在>80的数据,而对于下面这一查询语句,因为外查询是一行一行查的,对于每一行外查询,它的id值都是不同的(依次为1,2,3,4……),所以子查询中的select * from scores b where a.id = b.id and chinese > 80,条件:a.id = b.id并没有一直成立,所以可能返回true或false1 select * from scores a 2 where exists 3 ( 4 select * from scores b where a.id = b.id and chinese > 80 5 )
-
to be continued