一 題目 二 答案 1、查詢所有的課程的名稱以及對應的任課老師姓名 SELECT course.cname, teacher.tname FROM course INNER JOIN teacher ON course.teacher_id = teacher.tid; 2、查詢學生表中男女生各有多 ...
一 題目
1、查詢所有的課程的名稱以及對應的任課老師姓名
2、查詢學生表中男女生各有多少人
3、查詢物理成績等於100的學生的姓名
4、查詢平均成績大於八十分的同學的姓名和平均成績
5、查詢所有學生的學號,姓名,選課數,總成績
6、 查詢姓李老師的個數
7、 查詢沒有報李平老師課的學生姓名
8、 查詢物理課程比生物課程高的學生的學號
9、 查詢沒有同時選修物理課程和體育課程的學生姓名
10、查詢掛科超過兩門(包括兩門)的學生姓名和班級
、查詢選修了所有課程的學生姓名
12、查詢李平老師教的課程的所有成績記錄
13、查詢全部學生都選修了的課程號和課程名
14、查詢每門課程被選修的次數
15、查詢之選修了一門課程的學生姓名和學號
16、查詢所有學生考出的成績並按從高到低排序(成績去重)
17、查詢平均成績大於85的學生姓名和平均成績
18、查詢生物成績不及格的學生姓名和對應生物分數
19、查詢在所有選修了李平老師課程的學生中,這些課程(李平老師的課程,不是所有課程)平均成績最高的學生姓名
20、查詢每門課程成績最好的前兩名學生姓名
21、查詢不同課程但成績相同的學號,課程號,成績
22、查詢沒學過“葉平”老師課程的學生姓名以及選修的課程名稱;
23、查詢所有選修了學號為1的同學選修過的一門或者多門課程的同學學號和姓名;
24、任課最多的老師中學生單科成績最高的學生姓名
二 答案
#1、查詢所有的課程的名稱以及對應的任課老師姓名
SELECT
course.cname,
teacher.tname
FROM
course
INNER JOIN teacher ON course.teacher_id = teacher.tid;
#2、查詢學生表中男女生各有多少人
SELECT
gender 性別,
count(1) 人數
FROM
student
GROUP BY
gender;
#3、查詢物理成績等於100的學生的姓名
SELECT
student.sname
FROM
student
WHERE
sid IN (
SELECT
student_id
FROM
score
INNER JOIN course ON score.course_id = course.cid
WHERE
course.cname = '物理'
AND score.num = 100
);
#4、查詢平均成績大於八十分的同學的姓名和平均成績
SELECT
student.sname,
t1.avg_num
FROM
student
INNER JOIN (
SELECT
student_id,
avg(num) AS avg_num
FROM
score
GROUP BY
student_id
HAVING
avg(num) > 80
) AS t1 ON student.sid = t1.student_id;
#5、查詢所有學生的學號,姓名,選課數,總成績(註意:對於那些沒有選修任何課程的學生也算在內)
SELECT
student.sid,
student.sname,
t1.course_num,
t1.total_num
FROM
student
LEFT JOIN (
SELECT
student_id,
COUNT(course_id) course_num,
sum(num) total_num
FROM
score
GROUP BY
student_id
) AS t1 ON student.sid = t1.student_id;
#6、 查詢姓李老師的個數
SELECT
count(tid)
FROM
teacher
WHERE
tname LIKE '李%';
#7、 查詢沒有報李平老師課的學生姓名(找出報名李平老師課程的學生,然後取反就可以)
SELECT
student.sname
FROM
student
WHERE
sid NOT IN (
SELECT DISTINCT
student_id
FROM
score
WHERE
course_id IN (
SELECT
course.cid
FROM
course
INNER JOIN teacher ON course.teacher_id = teacher.tid
WHERE
teacher.tname = '李平老師'
)
);
#8、 查詢物理課程比生物課程高的學生的學號(分別得到物理成績表與生物成績表,然後連表即可)
SELECT
t1.student_id
FROM
(
SELECT
student_id,
num
FROM
score
WHERE
course_id = (
SELECT
cid
FROM
course
WHERE
cname = '物理'
)
) AS t1
INNER JOIN (
SELECT
student_id,
num
FROM
score
WHERE
course_id = (
SELECT
cid
FROM
course
WHERE
cname = '生物'
)
) AS t2 ON t1.student_id = t2.student_id
WHERE
t1.num > t2.num;
#9、 查詢沒有同時選修物理課程和體育課程的學生姓名(沒有同時選修指的是選修了一門的,思路是得到物理+體育課程的學生信息表,然後基於學生分組,統計count(課程)=1)
SELECT
student.sname
FROM
student
WHERE
sid IN (
SELECT
student_id
FROM
score
WHERE
course_id IN (
SELECT
cid
FROM
course
WHERE
cname = '物理'
OR cname = '體育'
)
GROUP BY
student_id
HAVING
COUNT(course_id) = 1
);
#10、查詢掛科超過兩門(包括兩門)的學生姓名和班級(求出<60的表,然後對學生進行分組,統計課程數目>=2)
SELECT
student.sname,
class.caption
FROM
student
INNER JOIN (
SELECT
student_id
FROM
score
WHERE
num < 60
GROUP BY
student_id
HAVING
count(course_id) >= 2
) AS t1
INNER JOIN class ON student.sid = t1.student_id
AND student.class_id = class.cid;
#11、查詢選修了所有課程的學生姓名(先從course表統計課程的總數,然後基於score表按照student_id分組,統計課程數據等於課程總數即可)
SELECT
student.sname
FROM
student
WHERE
sid IN (
SELECT
student_id
FROM
score
GROUP BY
student_id
HAVING
COUNT(course_id) = (SELECT count(cid) FROM course)
);
#12、查詢李平老師教的課程的所有成績記錄
SELECT
*
FROM
score
WHERE
course_id IN (
SELECT
cid
FROM
course
INNER JOIN teacher ON course.teacher_id = teacher.tid
WHERE
teacher.tname = '李平老師'
);
#13、查詢全部學生都選修了的課程號和課程名(取所有學生數,然後基於score表的課程分組,找出count(student_id)等於學生數即可)
SELECT
cid,
cname
FROM
course
WHERE
cid IN (
SELECT
course_id
FROM
score
GROUP BY
course_id
HAVING
COUNT(student_id) = (
SELECT
COUNT(sid)
FROM
student
)
);
#14、查詢每門課程被選修的次數
SELECT
course_id,
COUNT(student_id)
FROM
score
GROUP BY
course_id;
#15、查詢之選修了一門課程的學生姓名和學號
SELECT
sid,
sname
FROM
student
WHERE
sid IN (
SELECT
student_id
FROM
score
GROUP BY
student_id
HAVING
COUNT(course_id) = 1
);
#16、查詢所有學生考出的成績並按從高到低排序(成績去重)
SELECT DISTINCT
num
FROM
score
ORDER BY
num DESC;
#17、查詢平均成績大於85的學生姓名和平均成績
SELECT
sname,
t1.avg_num
FROM
student
INNER JOIN (
SELECT
student_id,
avg(num) avg_num
FROM
score
GROUP BY
student_id
HAVING
AVG(num) > 85
) t1 ON student.sid = t1.student_id;
#18、查詢生物成績不及格的學生姓名和對應生物分數
SELECT
sname 姓名,
num 生物成績
FROM
score
LEFT JOIN course ON score.course_id = course.cid
LEFT JOIN student ON score.student_id = student.sid
WHERE
course.cname = '生物'
AND score.num < 60;
#19、查詢在所有選修了李平老師課程的學生中,這些課程(李平老師的課程,不是所有課程)平均成績最高的學生姓名
SELECT
sname
FROM
student
WHERE
sid = (
SELECT
student_id
FROM
score
WHERE
course_id IN (
SELECT
course.cid
FROM
course
INNER JOIN teacher ON course.teacher_id = teacher.tid
WHERE
teacher.tname = '李平老師'
)
GROUP BY
student_id
ORDER BY
AVG(num) DESC
LIMIT 1
);
#20、查詢每門課程成績最好的前兩名學生姓名
#查看每門課程按照分數排序的信息,為下列查找正確與否提供依據
SELECT
*
FROM
score
ORDER BY
course_id,
num DESC;
#表1:求出每門課程的課程course_id,與最高分數first_num
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id;
#表2:去掉最高分,再按照課程分組,取得的最高分,就是第二高的分數second_num
SELECT
score.course_id,
max(num) second_num
FROM
score
INNER JOIN (
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id
) AS t ON score.course_id = t.course_id
WHERE
score.num < t.first_num
GROUP BY
course_id;
#將表1和表2聯合到一起,得到一張表t3,包含課程course_id與該們課程的first_num與second_num
SELECT
t1.course_id,
t1.first_num,
t2.second_num
FROM
(
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id
) AS t1
INNER JOIN (
SELECT
score.course_id,
max(num) second_num
FROM
score
INNER JOIN (
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id
) AS t ON score.course_id = t.course_id
WHERE
score.num < t.first_num
GROUP BY
course_id
) AS t2 ON t1.course_id = t2.course_id;
#查詢前兩名的學生(有可能出現併列第一或者併列第二的情況)
SELECT
score.student_id,
t3.course_id,
t3.first_num,
t3.second_num
FROM
score
INNER JOIN (
SELECT
t1.course_id,
t1.first_num,
t2.second_num
FROM
(
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id
) AS t1
INNER JOIN (
SELECT
score.course_id,
max(num) second_num
FROM
score
INNER JOIN (
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id
) AS t ON score.course_id = t.course_id
WHERE
score.num < t.first_num
GROUP BY
course_id
) AS t2 ON t1.course_id = t2.course_id
) AS t3 ON score.course_id = t3.course_id
WHERE
score.num >= t3.second_num
AND score.num <= t3.first_num;
#排序後可以看的明顯點
SELECT
score.student_id,
t3.course_id,
t3.first_num,
t3.second_num
FROM
score
INNER JOIN (
SELECT
t1.course_id,
t1.first_num,
t2.second_num
FROM
(
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id
) AS t1
INNER JOIN (
SELECT
score.course_id,
max(num) second_num
FROM
score
INNER JOIN (
SELECT
course_id,
max(num) first_num
FROM
score
GROUP BY
course_id
) AS t ON score.course_id = t.course_id
WHERE
score.num < t.first_num
GROUP BY
course_id
) AS t2 ON t1.course_id = t2.course_id
) AS t3 ON score.course_id = t3.course_id
WHERE
score.num >= t3.second_num
AND score.num <= t3.first_num
ORDER BY
course_id;
#可以用以下命令驗證上述查詢的正確性
SELECT
*
FROM
score
ORDER BY
course_id,
num DESC;
-- 21、查詢不同課程但成績相同的學號,課程號,成績
-- 22、查詢沒學過“葉平”老師課程的學生姓名以及選修的課程名稱;
-- 23、查詢所有選修了學號為1的同學選修過的一門或者多門課程的同學學號和姓名;
-- 24、任課最多的老師中學生單科成績最高的學生姓名
更多練習以及參考答案:https://www.cnblogs.com/clschao/articles/9995768.html