数据库查询语言(DML)练习:课程表与学生信息分析

需积分: 37 5 下载量 88 浏览量 更新于2024-07-12 收藏 261KB PPT 举报
本资源是一个关于数据库查询语言(DML)的练习题目,主要涉及SQL语句的使用。课程表(course)包含课程编号(cno)、课程名称(cname)和授课教师编号(tno)。练习提供了四张表格的数据,包括学生信息表(student)、老师信息表(teacher)、课程表(course)和成绩表(score),用于编写和执行SQL查询。 1. 查询student表中所有记录的sname、sex和class列: ```sql SELECT sname, sex, class FROM student; ``` 2. 显示教师所有的单位,即不重复的depart列: ```sql SELECT DISTINCT depart FROM teacher; ``` 3. 显示student表的所有记录: ```sql SELECT * FROM student; ``` 4. 显示score表中成绩在60到80之间的所有记录: ```sql SELECT * FROM score WHERE degree BETWEEN 60 AND 80; ``` 5. 显示score表中成绩为85,86或88的记录: ```sql SELECT * FROM score WHERE degree IN (85, 86, 88); ``` 6. 显示student表中“95031”班或性别为“女”的同学记录: ```sql SELECT * FROM student WHERE class = '95031' OR sex = '女'; ``` 7. 以class降序显示student表的所有记录: ```sql SELECT * FROM student ORDER BY class DESC; ``` 8. 以cno升序、degree降序显示score表的所有记录: ```sql SELECT * FROM score ORDER BY cno ASC, degree DESC; ``` 9. 显示“98031”班的学生人数: ```sql SELECT COUNT(*) FROM student WHERE class = '98031'; ``` 10. 显示score表中的最高分的学生学号和课程号: ```sql SELECT sno, cno FROM score WHERE degree = (SELECT MAX(degree) FROM score); ``` 11. 显示“3-105”号课程的平均分: ```sql SELECT AVG(degree) FROM score WHERE cno = '3-105'; ``` 12. 显示score表中至少有5名学生选修的并以3开头的课程号的平均分数: ```sql WITH subquery AS ( SELECT cno, COUNT(*) AS student_count FROM score GROUP BY cno HAVING COUNT(*) >= 5 AND cno LIKE '3%' ) SELECT AVG(score.degree) FROM score JOIN subquery ON score.cno = subquery.cno; ``` 13. 显示最低分大于70,最高分小于90的sno列: ```sql SELECT sno FROM ( SELECT sno, MIN(degree) AS min_degree, MAX(degree) AS max_degree FROM score GROUP BY sno ) subquery WHERE min_degree > 70 AND max_degree < 90; ``` 14. 显示所有学生的sname、cno和degree列: ```sql SELECT sname, cno, degree FROM student JOIN score ON student.sno = score.sno; ``` 15. 显示所有学生的sname、cname和degree: ```sql SELECT sname, cname, degree FROM student JOIN score ON student.sno = score.sno JOIN course ON score.cno = course.cno; ``` 以上是针对给定练习题目的SQL查询语句及其解释,涵盖了基本的查询、聚合函数、条件过滤、排序以及子查询等多种SQL操作。通过这些练习,可以巩固和提升对SQL语言的理解和应用能力。