1.查询输出“高等数学”课程成绩前三名(不考虑成绩有重复值的情况)的学生的学号,姓名,课程名,系名(sdept),成绩。
SELECT s.sno,sname,cname,sdept,grade
FROM student s, course c, sc
WHERE s.sno = sc.sno AND c.cno = sc.cno AND cname = '高等数学'
ORDER BY grade DESC LIMIT 3;
2.统计各门课程选修人数,要求输出课程代号,课程名,有成绩人数(grade不为NULL),最高分,最低分,平均分(取整round函数),按课程号排序。
[注意:SQL表名请用小写]
学生数据库db_student包括三个数据表student(学生表)、course(课程表)和sc(选课表)。
SELECT sc.CNO,CNAME,COUNT(grade),MAX(grade),MIN(grade),ROUND(AVG(grade))
FROM course c , sc
WHERE sc.CNO = c.CNO
GROUP BY sc.CNO
ORDER BY sc.CNO;
3.有课程表,学生表,成绩表如下,查询计算机系平均成绩高于70分的学号,姓名、平均成绩,并按平均成绩降序排列。
SELECT s.sno,sname,AVG(grade)
FROM student s, sc
WHERE s.sno = sc.sno AND sdept ='计算机系'
GROUP BY s.sno
HAVING AVG(grade) > 70
ORDER BY AVG(grade) DESC;
4.查询同时选修了“数据库基础”和“计算机网络”两门课的学生的学号,姓名。
SELECT s.sno,sname
FROM student s,course c,sc
WHERE s.sno=sc.sno AND sc.cno =c.cno
AND cname IN('数据库基础','计算机网络')
GROUP BY s.sno
HAVING COUNT(*)=2;
5.查询选修了课程的学生的学号和姓名,按学号排序。
[注意:SQL表名请用小写]
学生数据库db_student包括三个数据表student(学生表)、course(课程表)和sc(选课表)。
SELECT DISTINCT s.SNO,SNAME
FROM student s,course c,sc
WHERE s.SNO = sc.SNO AND sc.CNO = c.CNO
ORDER BY s.SNO;
6.查询学生的选课情况,要求输出学号,姓名,课程门数,课程名列表(按课程名升序排列,用下划线_分隔),按照学号升序排序。
[注意:SQL表名请用小写]
学生数据库db_student包括三个数据表student(学生表)、course(课程表)和sc(选课表)。
SELECT s.sno,sname,COUNT(*),
GROUP_CONCAT(DISTINCT cname ORDER BY cname SEPARATOR '_')'课程名列表'
FROM student s,sc,course cs
WHERE s.sno=sc.sno AND sc.cno=cs.cno
GROUP BY s.sno
ORDER BY s.sno;
7.查询每个男生的选课门数(要求用嵌套查询实现)要求输出学号、选课门数,并按序号升序排序。
SELECT sc.sno,COUNT(*)
FROM sc
WHERE sno IN(SELECT sno FROM student WHERE ssex='男')
GROUP BY sno;
8.查询没有同时选修“计算机导论”和“计算机网络”两门课的学生的学号,姓名
SELECT s.SNO,SNAME
FROM student s
WHERE s.SNO NOT IN(
SELECT s.SNO
FROM student s,sc,course c
WHERE s.SNO = sc.SNO AND sc.CNO =c.CNO
AND CNAME IN ('计算机导论','计算机网络')
GROUP BY s.SNO
HAVING COUNT(*) >=2
);
9.有课程表,学生表,成绩表如下,查询选修了学号为9521102同学选修的全部课程的学生学号和姓名。
SELECT DISTINCT s.sno, s.sname
FROM student s
WHERE NOT EXISTS (
SELECT *
FROM sc b
WHERE b.sno = '9521102' AND NOT EXISTS (
SELECT *
FROM sc c
WHERE s.sno = c.sno AND c.cno = b.cno
)
);
或者
SELECT DISTINCT s.sno, s.sname
FROM student s, sc
WHERE s.sno = sc.sno AND cno IN
(
SELECT cno
FROM sc
WHERE sno = '9521102'
)
GROUP BY s.sno
HAVING COUNT(DISTINCT cno) =
(
SELECT COUNT(cno)
FROM sc
WHERE sno = '9521102'
);
10.查询95级学生(学号前两位)的选课情况,输出学号、姓名、课程号、成绩(包括未选课的学生信息)。
SELECT s.sno,sname,sc.cno,grade
FROM student s
LEFT JOIN sc ON s.sno = sc.sno
WHERE s.sno LIKE '95%';
11.有课程表,学生表,成绩表如下,查询选课门数最多的学生的学号和姓名,结果可能不只一行。
SELECT s.sno,sname
FROM student s,sc
WHERE s.sno = sc.sno
GROUP BY s.sno
HAVING COUNT(*) = (
SELECT COUNT(*) FROM sc GROUP BY sno ORDER BY COUNT(*) DESC LIMIT 1
);
12.查询每个学生成绩最高的成绩信息,有课程表,学生表,成绩表如下,请完成查询,输出成绩信息学号、课程号及成绩,最高分可能有多门成绩。
SELECT sc.sno,cno,grade
FROM sc,(
SELECT sc.sno sno,MAX(grade) max_grade
FROM sc
GROUP BY sc.sno
) AS s
WHERE sc.sno = s.sno AND grade = s.max_grade;
13.查询同时选修了c01,c02,c03课程学生,输出学号,姓名
SELECT s.sno,sname
FROM student s,sc
WHERE s.sno=sc.sno
AND cno IN('c01','c02','c03')
GROUP BY s.sno
HAVING COUNT(*)=3 ;
14.查询每门课程被选修的情况,输出课程号,课程名,被选修次数,结果按选修次数降序课程号降序排列。
SELECT course.cno, course.cname, COUNT(*)
FROM course, sc
WHERE course.cno = sc.cno
GROUP BY sc.cno
ORDER BY COUNT(*) DESC, sc.cno DESC;
15.查询每门课程被选修的情况(包括从未被选修过的课程),输出课程号,课程名,被选修次数,结果按选修次数升序课程号升序排列。
SELECT course.cno, cname, COUNT(sno)
FROM course LEFT JOIN sc
ON course.cno = sc.cno
GROUP BY course.cno
ORDER BY COUNT(sno) ASC, course.cno ASC;
16.有课程表,学生表,成绩表如下,查询选修了c03课程的学生学号和姓名。
SELECT s.sno,sname
FROM sc,student s
WHERE s.sno =sc.sno AND cno ='C03';
17.查询没有参加选课的学生,输出系名,学号,姓名,性别,按系名(升序),学号(升序)排序。
SELECT s.sdept, s.sno, s.sname, s.ssex
FROM student s
WHERE s.sno NOT IN (SELECT DISTINCT sno FROM sc)
ORDER BY s.sdept ASC, s.sno ASC;
18.有课程表,学生表,成绩表如下,查询所有人都选修了的课程号与课程名。
SELECT c.cno,cname
FROM course c, sc
WHERE c.cno = sc.cno
GROUP BY c.cno
HAVING COUNT(DISTINCT sno) = (
SELECT COUNT(sno)
FROM student
);
19.查询每个学生高于他自己选修平均分的那门课程的成绩,输出学号,课程号,课程成绩,他所有课程的平均分(取整),并按学号升序排列。
SELECT sc.sno,cno,grade,avggrade
FROM sc,(SELECT sno,ROUND(AVG(grade)) AS avggrade FROM sc GROUP BY sno) AS avg_sc
WHERE sc.sno=avg_sc.sno AND sc.grade>avg_sc.avggrade
ORDER BY sc.sno;
20.统计各门课程的重修人数(包括grade为NULL),要求输出课程代号,课程名及重修人数。
SELECT c.cno, c.cname, COUNT(*)
FROM course c, sc
WHERE c.cno = sc.cno AND (grade < 60 OR grade IS NULL)
GROUP BY c.cno;
21.查询输出平均成绩在2-5名的学生,输出学号、姓名和平均成绩(取整),平均成绩降序。【不用考虑空值】
SELECT s.sno, s.sname, ROUND(AVG(sc.grade))
FROM student s, sc
WHERE sc.sno = s.sno
GROUP BY sc.sno
ORDER BY ROUND(AVG(grade)) DESC
LIMIT 1,4;
LIMIT 1,4 表示从查询结果中的第2行开始,取出4行记录。其中,第1个参数1表示查询结果的偏移量(即从第几行开始取),第2个参数4表示取出的记录数。
LIMIT 1 表示从查询结果中取出1行记录。其中,1表示取出的记录数,查询结果中的第1行就是这唯一的记录。
22.有课程表,学生表,成绩表如下,查询超过该课程平均分的成绩信息,输出学号,课程号及成绩。
SELECT sc.sno, sc.cno, sc.grade
FROM sc,(
SELECT cno, AVG(grade) AS avg_grade
FROM sc
GROUP BY cno
) AS t
WHERE sc.cno = t.cno AND sc.grade > t.avg_grade;
23.查询选修平均分为60分(包括60分)以上的学生的各门课成绩,要求输出学号,姓名,课程名和成绩,并按学号升序排序。
SELECT s.sno, s.sname, c.cname, sc.grade
FROM student s,sc,course c
WHERE s.sno = sc.sno AND sc.cno = c.cno
AND s.sno IN (
SELECT sno
FROM sc
GROUP BY sno
HAVING AVG(grade) >= 60
)
ORDER BY s.sno ASC;
24.查询与“王大力”同一个系的学生的基本信息。
SELECT *
FROM student
WHERE sname !='王大力' AND sdept IN (
SELECT DISTINCT sdept
FROM student
WHERE sname='王大力');
25.查询每门课的先修课,输出课程号、课程名和先修课程名。
SELECT c1.cno, c1.cname, c2.cname AS pre_course_name
FROM course c1 ,course c2
WHERE c1.cpno = c2.cno;
26.查询选修“高等数学”课程的成绩不为空的学生学号、姓名和成绩。
SELECT s.sno, s.sname, sc.grade
FROM student s,sc,course c
WHERE s.sno = sc.sno AND sc.cno = c.cno AND
c.cname = "高等数学" AND grade IS NOT NULL;
27.有课程表,学生表,成绩表如下,请完成查询,查询学生成绩单,要求输出有有效成绩的学号,姓名,课程名,成绩,按学号升序课程名降序排列。
SELECT s.sno, s.sname, c.cname, sc.grade
FROM student s,sc,course c
WHERE s.sno = sc.sno AND sc.cno = c.cno AND
sc.grade IS NOT NULL
ORDER BY s.sno ASC, c.cname DESC;
28.查询学生成绩单,要求输出学号,姓名,平均分,选修门数,按平均分降序排序。
SELECT s.sno,s.sname,AVG(sc.grade),COUNT(*)
FROM student s,sc
WHERE s.sno = sc.sno
GROUP BY s.sno
ORDER BY AVG(sc.grade) DESC;
29.查询选修通过2门(包括2门)以上的学生的信息,输出学号、选修通过门数、平均成绩(取整),按门数降序排序,若门数相同,按照成绩降序。
SELECT sno, COUNT(*), ROUND(AVG(grade))
FROM sc
WHERE grade >= 60
GROUP BY sno
HAVING COUNT(*) >= 2
ORDER BY 2 DESC, 3 DESC;
30.统计各门课程的未通过人数(包括grade为NULL),要求输出课程代号,未通过人数,结果按课程号升序排列。
SELECT cno, COUNT(*)
FROM sc
WHERE grade < 60
OR grade IS NULL
GROUP BY cno
ORDER BY cno;
31.查询统计学生不及格(低于60分)门数大于等于2门的信息,输出系名,学号,姓名,不及格门数,按照系(升序)排序,不及格门数(降序)排序。
SELECT s.sdept, s.sno, s.sname, COUNT(*)
FROM student s, sc
WHERE s.sno = sc.sno AND sc.grade < 60
GROUP BY s.sno
HAVING COUNT(*) >= 2
ORDER BY s.sdept, COUNT(*) DESC;
32.查询选修了“计算机网络”或者“数据库基础”课程的学生的学号,姓名。按学号排序。
SELECT DISTINCT s.sno, s.sname
FROM student s,sc,course c
WHERE s.sno = sc.sno AND sc.cno = c.cno AND
c.cname IN ('计算机网络', '数据库基础')
ORDER BY s.sno ASC;
33.查询选修了全部课程的学生的学号,姓名,系名
SELECT s.sno,sname,sdept
FROM student s,sc
WHERE s.sno=sc.sno
GROUP BY s.sno
HAVING COUNT(DISTINCT sc.cno) =(SELECT COUNT(cno) FROM course);
34.查询不姓张的学生的基本信息文章来源:https://www.toymoban.com/news/detail-714188.html
SELECT *
FROM student
WHERE sname NOT LIKE '张%';
35.查询考试不及格的学生的学号(包括没有成绩的)文章来源地址https://www.toymoban.com/news/detail-714188.html
SELECT DISTINCT sno
FROM sc
WHERE (grade IS NULL) OR( grade < 60);
到了这里,关于数据查询sql习题综合二的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!