題目來源於網絡,答案全爲博主手敲
一、表關係建立
表關係分析:
二、增刪查改
1.查詢“生物”課程比“物理”課程成績高的所有學生的學號
select * from
(select student_id,course_id,score from score left join course on score.course_id = course.cid where course.cname='生物') as A
left join
(select student_id,course_id,score from score left join course on score.course_id = course.cid where course.cname='物理') as B
on A.student_id = B.student_id
where A.score>B.score
2.查詢平均成績大於60分的同學的學號和平均成績
select student_id,avg(score)
from score
GROUP BY student_id
HAVING avg(score)>60
3.查詢所有同學的學號、姓名、選課數、總成績
select student_id,student.sname,count(1),sum(score)
from score left join student on score.student_id=student.sid
group by student_id
4.查詢姓“波”的老師的個數
select count(1)
from teacher
where teacher.teacher like '波%'
5.查詢沒學過“波多”老師課的同學的學號、姓名
方法1:
select student.sid,student.sname from student WHERE sid not in(
select score.student_id from course
left join score
on score.course_id=course.cid
left join teacher
on course.teacher_id = teacher.tid
where teacher.teacher='波多'
)
方法2:
select student.sid,student.sname from student where student.sid not in(
select student_id from score
WHERE course_id in
(select cid from course
left join teacher on course.teacher_id=teacher.tid
where teacher.teacher='波多')
)
6.查詢學過“1”並且也學過編號“2”課程的同學的學號、姓名
select A.student_id from
(select student_id,score from score where course_id=1) as A
left join
(select student_id,score from score where course_id=2) as B
on A.student_id = B.student_id
where A.score is not null and B.score is not null
7.查詢學過“波多”老師所教的所有課的同學的學號、姓名
select student_id,student.sname from score
left join student
on score.student_id=student.sid
where score.course_id in
(select cid from course left join teacher on course.teacher_id=teacher.tid
where teacher.teacher = '波多')
GROUP BY student_id
HAVING count(1)=(select count(cid) from course left join teacher on course.teacher_id=teacher.tid
where teacher.teacher = '波多')
8.查詢課程編號“2”的成績比課程編號“1”課程低的所有同學的學號、姓名
select P.student_id,student.sname from
(select M.student_id from
(select student_id,course_id,score from score where course_id=1) as M
left join
(select student_id,course_id,score from score where course_id=2) as N
on M.student_id = N.student_id
where M.score>N.score ) as P
left join student on student.sid=P.student_id
9.查詢有課程成績小於60分的同學的學號、姓名
select DISTINCT student_id,student.sname from score
left join student on score.student_id=student.sid
where score.score<60
10.查詢沒有學全所有課的同學的學號、姓名
select student_id,student.sname from score
left join student on score.student_id=student.sid
group BY student_id
having count(1)<(select count(1) from course)
11.查詢至少有一門課與學號爲“1”的同學所學相同的同學的學號和姓名
select DISTINCT student_id from score
where course_id in
(select course_id from score
where student_id = 1)
12.查詢至少學過學號爲“1”同學所選課程中任意一門課的其他同學學號和姓名
select DISTINCT student_id from score
where course_id in
(select course_id from score
where student_id = 1)
and student_id!=1
13.查詢和“2”號的同學學習的課程完全相同的其他同學學號和姓名
select student_id,student.sname from score
left join student on score.student_id=student.sid
where student_id in
(select student_id from score
group by student_id
having student_id !=2 and
count(course_id)=(select count(course_id) from score where student_id=2)
)
and
course_id in (select course_id from score where student_id=2)
group by student_id
having count(student_id) = (select count(course_id) from score where student_id=2)
分析:
由內向外:
1. 找到所有選課數與二號相同的學生的id
select student_id from score
group by student_id
having student_id !=2 and
count(course_id)=
(select count(course_id) from score where student_id=2)#二號學生選課數
)
2. 在score表篩選student_id,這個student_id要在第一步找到的id中,這就保證了篩選出來的學生選課數與2號相同。
3. 把course_id不在2號所選的course_id的記錄刪掉,那麼剩下的是所選course全在二號所選課中的記錄
4. 分組,統計student_id出現的次數,如果次數與2號選課的數量相同,則表示該學生與2號學生選課完全一樣
14.刪除學習“Faker”老師課的SC表記錄
delete from score where course_id in
(select cid from course left join
teacher on course.teacher_id=teacher.tid
where teacher.teacher='Faker')
15.向SC表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“2”課程的同學學號;②插入“2”號課程的平均成績
INSERT into score (student_id,course_id,score)
select sid,2,(select avg(score) from score where course_id=2 group by student_id)
from student where sid not in
(select student_id from score where course_id = 2)
16.按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,LOL,性教育,體育,有效課程數,有效平均分
select sc.student_id,
(select score from score left join course on score.course_id=course.cid where course.cname="LOL" and
score.student_id=sc.student_id) as lo,
(select score from score left join course on score.course_id=course.cid where course.cname="性教育"
and score.student_id=sc.student_id) as xing,
(select score from score left join course on score.course_id=course.cid where course.cname="體育"
and score.student_id=sc.student_id) as Ty,
count(sc.course_id),
AVG(sc.score)
from score as sc
group by student_id desc
17.查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分
select course_id,max(score),min(score) from score
group by course_id
18.按各科平均成績從低到高和及格率的百分數從高到低順序(case when score.score>60 then 1 else 0 end )相當於if else 判斷,如果score.score>60就返回1,否則返回0
select course_id,avg(score),
sum(case when score.score>60 then 1 else 0 END)/count(1)*100 as percent
from score
group BY course_id
19.課程平均分從高到低顯示(顯示任課老師)
select avg(if(isnull(score.score),0,score.score)),teacher.teacher from course
left join score on course.cid = score.course_id
left join teacher on course.teacher_id = teacher.tid
group by score.course_id
if( isnull(score.score) , 0 , score.score) 如果條件成立返回0,否則返回score.score,條件爲score.score是否爲空
20.查詢各科成績前三名的記錄:(不考慮成績並列情況),暫時沒做出來,貼一個網上的答案
select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join
(
select
sid,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 3,1) as second_num
from
score as s1
) as T
on score.sid =T.sid
where score.num <= T.first_num and score.num >= T.second_num
21.查詢每門課程被選修的學生數
select course_id,count(1)
from score
group by course_id
T23
select student_id
from score
GROUP BY student_id
having count(1)=1
22.查詢出只選修了一門課程的全部學生的學號
select student_id
from score
GROUP BY student_id
having count(1)=1
23.查詢男生、女生的人數
select * from
(select count(1) as man from student where sex='男') as A,
(select count(1) as woman from student where sex='女') as B
24.查詢姓“鋼”的學生名單
select student.sid,student.sname from student
where sname like '鋼%'
25.查詢同名同姓學生名單,並統計同名人數
select student.sname,count(1)
from student,(select sid,sname from student)as SC
where student.sid!=SC.sid and student.sname=SC.sname
26.查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列
select course_id,avg(score)
from score
group by course_id
ORDER BY score ASC,course_id desc
27.查詢平均成績大於85的所有學生的學號、姓名和平均成績
select student_id,student.sname,avg(score)
from score left JOIN student on score.student_id=student.sid
group by student_id
having avg(score)>85
28.查詢課程名稱爲“物理”,且分數低於60的學生姓名和分數
select student.sname,score from score left join student on score.student_id=student.sid
where course_id = (select cid from course where course.cname='物理')
and score < 60
29.查詢課程編號爲2且課程成績在80分以上的學生的學號和姓名
select student_id,student.sname from score left join student on score.student_id=student.sid
where course_id = 2 and score>80
30.求選了課程的學生人數
select count(1) from
(select student_id from score
group by student_id) as M
31.查詢選修“波多”老師所授課程的學生中,成績最高的學生姓名及其成績
select student.sname,max(score) from score left join
student on score.student_id=student.sid
where course_id in
(select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.teacher='波多')
32.查詢各個課程及相應的選修人數
select course_id,count(1),M.cname from score left join
(select cid,cname from course) as M
on score.course_id=M.cid
group by course_id
33.查詢不同課程但成績相同的學生的學號、課程號、學生成績
select score.student_id,M.student_id,score.course_id,score.score from score left join
(select student_id,score,course_id from score) as M
on score.score=M.score and score.student_id!=M.student_id
and score.course_id!=M.course_id
34.查詢每門課程成績最好的前兩名,同20
select score.sid,score.course_id,score.num,T.first_num,T.second_num from score left join
(
select
sid,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 0,1) as first_num,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 1,1) as second_num
from
score as s1
) as T
on score.sid =T.sid
where score.num <= T.first_num and score.num >= T.second_num
35.檢索至少選修兩門課程的學生學號
select student_id from score
group by student_id
having count(1)>2
36.查詢全部學生都選修的課程的課程號
select course_id from score group by course_id
having count(1)=(select count(1) from student)
37.查詢沒學過“波多”老師講授的任一門課程的學生姓名
select sid from student where sid not in(
select DISTINCT student_id from score
where course_id in
(select cid from course left join teacher on course.teacher_id=teacher.tid where teacher.teacher='波多')
)
38.查詢兩門以上不及格課程的同學的學號及其平均成績
select student_id from score
group by student_id
having count(case when score<60 then 1 ELSE null END)>2
注:0會被count計數,null不會
39.檢索“4”課程分數小於60,按分數降序排列的同學學號
select student_id,score
from score
where course_id = 4 and score<60
order by score DESC
40.刪除“2”同學的“1”課程的成績
delete from score
where student_id = 2
and course_id = 1