SQL server训练题

select * from student;
--1、 查询Student表中的所有记录的Sname、Ssex和Class列。 
select sname,ssex,class from student ;
--2、 查询教师所在的单位即不重复的Depart列。 
select * from teacher;
select distinct depart from teacher;
--3、 查询Student表的所有记录。 
select * from student;
--4、 查询Score表中成绩在60到80之间的所有记录。 
select * from score;
select * from score where degree between 60 and 80;
--5、 查询Score表中成绩为85,86或88的记录。 
select * from score where degree in(85,86,88);
--6、 查询Student表中“95031”班或性别为“女”的同学记录。 
select * from student;
select * from student where class = '95031' or ssex = '女';
--7、 以Class降序查询Student表的所有记录。 
select * from student order by class desc;
--8、 以Cno升序、Degree降序查询Score表的所有记录。 
select * from score order by cno ,degree desc;
--9、 查询“95031”班的学生人数。
select count(*) from student where class = '95031' ;
--10、查询Score表中的最高分的学生学号和课程号。
select * from score where degree =(
select max(degree) from score );
--11、查询‘3-105’号课程的平均分。
select avg(degree) from score where cno = '3-105';
--12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。 
select avg(degree) from score where cno like '3%' group by cno having count(*)>5;
select avg(degree) from score group by cno having count(*)>5 and cno like '3%';              --(推荐)
--13、查询分数在70和90之间的Sno列。 
select sno from score where degree between 70 and 90;
--14、查询所有学生的Sname、Cno和Degree列。
select sname ,cno,degree from student,score where student.sno = score.sno;
--15、查询所有学生的Sno、Cname和Degree列。
select sno,cname,degree from score,course where course.cno = score.cno;
--16、查询所有学生的 Sname、Cname和Degree列。
select sname,cname,degree from student,score,course where student.sno = score.sno and course.cno = score.cno;
--17、查询“95033”班所选课程的平均分。
select cno,avg(degree) from score where sno in(
select sno from student where class = '95033') group by cno;                            --如果不写group by cno会报“不是单组分组函数”错误
--18、查询所有同学的Sno、Cno和rank列。
select sno,cno,(select rank from grade where degree between low and upp) rank from score;     -- 这个也比较重要,直接select()的东西变成字段
select * from grade;
--19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 
select * from student where sno in(
select sno from score where cno = '3-105' and degree >(
select degree from score where sno = '109' and cno='3-105'));                               
--20、查询score中选学一门以上课程的同学中分数为非本门课程最高分成绩的记录。 
select sno,cno,degree from score group by sno having count(*) >1;
select * from score;
select sno,cno from score group by sno;   select * from score group by sno;--均会报错,因为既没有sum,avg,min...也没有having等汇总的字眼不可用group by ,如果只有having 的话,group by后跟的必须和select后跟的一样                    
select sno from score group by sno having count(*)>1;
select sno from score group by sno having count(cno) > 1;                  --选学一门以上的同学
select max(degree) ,cno from score group by cno;

select * from (
select * from score where sno in (
select sno from score group by sno having count(cno) > 1)) a, 
(select max(degree) maxdegree,cno from score group by cno) b 
where a.cno = b.cno and degree < maxdegree
;

--21、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select * from student where extract(year from sbirthday)=(
select extract(year from sbirthday) from student where sno = '108');
--22、查询“张旭“教师任课的学生成绩。
select * from score where cno in(
select cno from course where tno = (select tno from teacher where tname = '张旭') );

--23、查询选修某课程的同学人数多于5人的教师姓名。
select tname from teacher where tno in (
select tno from course,score where score.cno in ( 
select cno from score group by cno having count(*)>5)                                        --选修人数多于5人的课程
);
--24、查询95033班和95031班全体学生的成绩记录。
select * from student where class in('95033','95031');
--25、查询存在有85分以上成绩的课程Cno.
select cno from(
select cno,max(degree) maxdegree from score group by cno) cno_degree 
where maxdegree > 85;
--26、查询出 “计算机系“教师所教课程的成绩表。 
select * from score where cno in (
select cno from course where tno in(
select tno from teacher where depart='计算机系'));
--27、查询所有教师和同学的 name、sex和birthday.
select tname,tsex,tbirthday,sname,ssex,sbirthday from teacher,student;
select * from student;
select * from teacher;
--28、查询所有“女”教师和“女”同学的name、sex和birthday.
select tname,tsex,tbirthday,sname,ssex,sbirthday from (select * from teacher where tsex='女') nv_teacher,
(select * from student where ssex = '女') nv_stu;

--29、查询成绩比该课程平均成绩低的同学的成绩表。
select * from score,(select cno,avg(degree) avgdegree from score group by cno) cno_avgdegree where degree < cno_avgdegree.avgdegree and score.cno = cno_avgdegree.cno;

--30、查询所有任课教师的Tname和Depart.
select tname,depart from teacher where tno in(
select tno from course );
--31、查询所有未讲课的教师的Tname和Depart.
select tname,depart from teacher where tno not in(
select tno from course );
--32、查询至少有2名男生的班号。
select class from (
select count(*) num,ssex,class from student group by ssex,class) a
where num>=2 and ssex='男';
--33、查询Student表中不姓“王”的同学记录。
select * from student where sname not like '王%';
--34、查询Student表中每个学生的姓名和年龄。
select sname,(sysdate-sbirthday)/365 age from student;                                       ----    年龄  ==>  (sysdate-sbirthday)/365 
--35、查询Student表中最大和最小的Sbirthday日期值。
select max(sbirthday) ,min(sbirthday) from student;
--36、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select student.*,(sysdate-sbirthday)/365 age from student order by class desc,age desc;
--37、查询“男”教师及其所上的课程.结果集有三列,分别是tname,tsex,cname。
select cname,tname,tsex from course,
(select tname,tno,tsex from teacher where tsex='男') a 
where course.tno = a.tno;
--38、查询各科目最高分同学的Sno、 Cno和Degree列。
select * from score,(select max(degree) max_degree,cno from score group by cno) b where score.degree = b.max_degree and score.cno = b.cno;
--39、查询和“李军”同性别的所有同学的Sname. 
select sname from student where ssex = (
select ssex from student where sname='李军'
);
--40、查询和“李军”同性别并同班的同学Sname.
select sname from student,(select ssex,class from student where sname='李军') a where student.ssex = a.ssex and student.class = a.class;
--41、查询所有选修“计算机导论”课程的“男”同学的成绩表
select * from score where cno = (
select cno from course where cname='计算机导论') 
and sno in (
select sno from student where ssex = '男');
 

关于group by ,order by,having 理解得还不够彻底,有待学习 

猜你喜欢

转载自blog.csdn.net/qq_38889342/article/details/84944397