数据库所有练习以及答案
1、 查询Student表中的所有记录的Sname、Ssex和Class列。
select sname,ssex,class from student
2、 查询教师所有的单位即不重复的Depart列。
select distinct depart from teacher
3、 查询Student表的所有记录。
select *from student
4、 查询Score表中成绩在60到80之间的所有记录。
select *from score where degree between 60 and 80
select *from score where degree>=60 and degree<=80
5、 查询Score表中成绩为85,86或88的记录。
select *from score where degree=85 or degree=86 or degree=88
select *from score where degree in(85,86,88)
6、 查询Student表中“95031”班或性别为“女”的同学记录。
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 asc,degree desc
9、 查询“95031”班的学生人数。
select COUNT(*) from student where class='95031'
10、 查询Score表中的最高分的学生学号和课程号。
select sno,cno from score where degree=(select MAX(degree)from score)
11、 查询每门课的平均成绩。
select AVG(degree), cno from score group by cno
12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
select AVG(degree) from score where cno like '3%' group by cno having count(*)>=5
13、查询分数大于70,小于90的Sno列。
select sno from score where degree>70 and degree<90
14、查询所有学生的Sname、Cno和Degree列。
select s.sname, c.cno , c.DEGREE from student as s join score as c on s.sno=c.sno
15、查询所有学生的Sno、Cname和Degree列。
select sno, cname, DEGREE from course join score on course.cno=score.cno
16、查询所有学生的Sname、Cname和Degree列。
select sname,cname,DEGREE from score join course on score.cno=course.cno join student on student.sno = score.sno
17、查询“95033”班学生的平均分。
select AVG(degree) from score where sno in (select sno from student where class=95033)
select avg(degree) from score as c join student as s on c.sno = s.sno where s.class = '95033';
18、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
select t.* from score as c join student as t on c.sno=t.sno where c.cno='3-105' and c.degree>(select degree from score where sno = '109' and cno = '3-105');
19、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
select *from score where cno='3-105' and degree >(select degree from score where cno='3-105'and sno='109')
20、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select sno,sname,sbirthday from student where YEAR(sbirthday)=(select YEAR(sbirthday) from student where sno='108')
21、查询“张旭“教师任课的学生成绩。
select *from score where cno=(select cno from course where tno =(select tno from teacher where tname='张旭'))
select s.*,t.tname from score as s join course as c on s.cno = c.cno join teacher as t on t.tno = c.tno where t.tname = '张旭';
22、查询考计算机导论的学生成绩
select s.* from score as s join course as c on c.cno=s.cno where c.cname = '计算机导论';
23、查询李诚老师教的课程名称
select c.* from course as c join teacher as t on t.tno = c.tno where t.tname = '李诚';
24、教高等数学的老师是哪个系的
select t.* from course as c join teacher as t on t.tno = c.tno where c.cname = '高等数学';
25、查询选修某课程的同学人数多于5人的教师姓名。
select t.* from teacher as t join course as c on t.tno = c.tno where c.cno in (select cno from score group by cno having COUNT(*)>5);
26、查询95033班和95031班全体学生的记录。
select *from student where class='95033' or class='95031'
27、查询成绩表中存在有85分以上成绩的课程Cno.
select distinct cno from score where degree >= 85;
28、查询出“计算机系“教师所教课程的成绩表。
select *from score where cno in(select cno from course where tno in(select tno from teacher where depart='计算机系'))
select s.* from score as s join course as c on s.cno = c.cno join teacher as t on t.tno = c.tno where t.depart = '计算机系';
29、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的 Cno、Sno和Degree.
select a.* from score as a join score as b on a.sno = b.sno where a.cno = '3-105' and b.cno = '3-245' and a.degree>b.degree;
30、查询所有教师和同学的name、sex和birthday.
select sname as name,ssex as sex,sbirthday as birthday from student
union
select tname as name,tsex as sex,tbirthday as birthday from teacher
31、查询所有“女”教师和“女”同学的name、sex和birthday.
select sname as name,ssex as sex,sbirthday as birthday from student where ssex='女' union select tname as name,tsex as sex,tbirthday as birthday from teacher where tsex='女'
32、查询所有成绩比3-105课程平均成绩低的同学的成绩表.
select * from score where degree <(select avg(degree) from score where cno = '3-105');
33、查询所有任课教师的Tname和Depart.
select tname,depart from teacher where tno in (select tno from course)
select t.tname,t.depart from teacher as t join course as c on c.tno=t.tno;
34、查询所有未讲课的教师的Tname和Depart.
select tname,depart from teacher where tno not in(select t.tno from teacher as t join course as c on c.tno=t.tno)
35、查询至少有2名男生的班号。
select class from student where ssex='男' group by class having COUNT(*)>=2
36、查询Student表中不姓“王”的同学记录。
select * from student where sno not in(select sno from student where sname like '王%');
37、查询Student表中每个学生的姓名和年龄。
select sname as 姓名,(2020-YEAR(sbirthday))as 年龄 from student
38、查询Student表中最大和最小的Sbirthday日期值。
select MAX(sbirthday) as 最大,MIN(sbirthday) as 最小 from student
39、以班号和年龄从大到小的顺序查询Student表中的全部记录。
select *from student order by class desc,(2020-YEAR(sbirthday))desc,(13-month(sbirthday))desc
40、查询“男”教师及其所上的课程。
select tname ,cname from teacher join course on teacher.tno=course.tno where tsex='男'
41、查询最高分同学的Sno、Cno和Degree列。
select * from score where degree = (select max(degree) from score);
42、查询和“李军”同性别的所有同学的Sname.
select sname from student where ssex=(select ssex from student where sname='李军')
select a.* from student as a join student as b on a.ssex = b.ssex where b.sname = '李军';
43、查询和“李军”同性别并同班的同学Sname.
select *from student select sname from student where ssex=(select ssex from student where sname='李军')and class=(select class from student where sname ='李军')
select a.* from student as a join student as b on a.ssex = b.ssex and a.class = b.class where b.sname = '李军';
44、查询所有选修“计算机导论”课程的“男”同学的成绩表。
select degree from score where cno=(select cno from course where cname='计算机导论')
and sno in(select sno from student where ssex='男')
select o.* from score as o join student as s on s.sno= o.sno join course as c on c.cno = o.cno where c.cname='计算机导论' and s.ssex = '男';
浙公网安备 33010602011771号