使用 T-SQL 语句完成数据综合检索

内容:

1. select 语句的基本语法格式和执行方法;
2. 连接查询的表示及使用;
3. 嵌套查询的表示及使用;
4. 集合查询的表示及使用;

实验三数据为基础,用 T-SQL 语句实现下列数据查询操作。

  1. 查询选修了“数据库”的学生的基本信息,结果列显示学号,姓名,性别,院系。
select snum,sname,ssex,sdept from student
where snum in (select sno from cj
			   where cno=(select cnum from course 
		                  where cname='数据库'))
  1. 查询年龄比“李勇”小的学生的学号、课程号、成绩。
select sno,cno,score from cj
where sno in (select snum from student
			  where sage<(select sage from student 
			              where sname='李咏'))
  1. 查询其他系中比院系为“信息系”的学生中年龄最小者要大的学生的信息。
select * from student
where sdept not like '信息系' and sage>(select min(sage) from student
										where sdept='信息系')
  1. 查询其他系中比院系为“商务系”的学生年龄都大的学生的姓名。
select sname from student
where sdept not like '商务系' and sage>(select max(sage) from student
										where sdept='商务系')
  1. 查询“c001”课程的成绩高于 70 的学生姓名。
select  distinct student.sname from cj,student
where student.snum=cj.sno and cj.cno='c001' and cj.score>70
  1. 查询没有选修的学生姓名。
select sname from student 
where snum not in (select sno from cj)
  1. 查询学校开设的课程总数。
select COUNT(cnum) from course
  1. 查询选修两门及两门以上课程的学生姓名。
select student.sname from student,cj
where student.snum=cj.sno
group by student.sname,cj.sno having count(cj.cno)>1
  1. 查询以“数据”开头的课程的详细情况;
select * from course 
where cname like '数据%'
  1. .查询名字中第 2 个字为“向”的学生姓名和学号及选修的课程号、课程名;
select cnum,cname from course
where exists(select * from cj 
			 where exists(select * from student 
						  where course.cnum=cj.cno and sname like '_向%'))
  1. 列出选修了“数据库”或者“数学”的学生学号、姓名、所在院系、选修课程号及成绩;
select student.snum,student.sname,student.sdept from student,cj
where student.snum=cj.sno and cno in (select cnum from course 
		       where cname='数据库' or cname='数学')
  1. 查询缺少成绩的所有学生的详细情况;
select distinct * from student
where snum not in(select distinct sno from cj)
  1. 查询与‘张力’(假设姓名唯一)年龄不同的所有学生的信息;
select * from student
where sage<>(select sage from student
			 where sname like '张力')
  1. 查询所选课程的平均成绩大于张力的平均成绩的学生学号、姓名及平均成绩;
select snum,sname,AVG(score)as '平均成绩' from student,cj
where student.snum=cj.sno
group by student.snum,sname having AVG(score)>(select AVG(score) from cj,student
where student.snum=cj.sno and student.sname= '张力')
  1. 使用嵌套查询列出选修了“数据结构”课程的学生学号和姓名;
select snum,sname from student
where snum in(select sno from cj where cno in (select cnum from course where cname='数据结构'))
  1. 使用嵌套查询查询其它系中年龄小于“计算机系”的某个学生的学生姓名、年龄和院系;
select sname,sage,sdept from student
where sdept not like '计算机系' and sage<(select MIN(sage) from student)
  1. .列出与‘张力’在一个院系的学生的信息;
select * from student
where sdept=(select sdept from student where sname='张力') and sname<>'张力'

猜你喜欢

转载自blog.csdn.net/weixin_45104240/article/details/106334360