T-SQL查询语句案例
实验一:单表查询 (实验素材:附加学生信息表(student))
1. 查询student表中所有学生的信息
select * from student
2. 查询student表中“姓名”“所在班级”和“成绩”列内容
select 姓名,所在班级,成绩 from student
3. 查询student表中7班的学生姓名
select 姓名 from student where 所在班级=7
4. 查询student表中成绩为90—100分的学生所有信息
select * from student where 成绩 between 90 and 100
5. 查询student表中成绩低于90分或者高于95分的学生所有信息
select * from student where 成绩<90 or 成绩>95
6. 查询student表中成绩为89分,90分的学生所有信息
select * from student where 成绩 in (89,90)
7. 查询student表中姓刘的学生所有信息
select * from student where 姓名 like‘刘%’
8. 查询student表中1班的名叫张红的学生信息
select * from student where 姓名=‘张红’and 所在班级=1
9. 查询student表中备注不为空的学生所有信息
select * from student where 备注 is not null
10. 查询student表中前3行的数据
select top 3 * from student
11. 查询student表中“姓名”和“身份证号”两列数据,查询结果“姓名”列名称显示为“name”,“身份证号”列名称显示为“idcard”
select 姓名 as name,身份证号 as idcard from student
12. 查询student表中所有学生的总成绩,列名称显示为“总成绩”、
select SUM(成绩) as 总成绩 from student
13. 查询student表中所有学生信息,并按照成绩从高到低显示查询结果
select * from student order by 成绩 desc
14. 查询student表中所有学生的平均成绩
select AVG(成绩) as 平均成绩 from student
15. 查询student表中所有学生中的最高分和最低分
select MAX(成绩) as 最高分,MIN(成绩) as 最低分 from student
16. 查询student表中所有行数
select COUNT(*) 总行数 from student
17. 查询student表中每个班级的总成绩
select 所在班级,SUM(成绩) as 总成绩 from student group by 所在班级
18. 查询student表中总成绩大于181分的班级
select 所在班级,SUM(成绩) as 总成绩 from student group by 所在班级 having SUM(成绩)>181
19. 将student表中1班的学生信息保存在表student_1中
select * into student_1 from student where 所在班级=1