mysql練習答案(后25題)


1、查詢沒有學全所有課的同學的學號、姓名;

# 先統計一共有多少門課程

select count(cid) from course;

# 查看每個學生選擇的課程書

select count(course_id) from score group by student_id;

# 查詢所學課程數小於總課程數的學生學號

select student_id

from (select count(course_id) c_course_id,student_id from score group by student_id) t1 

where t1.c_course_id <  (select count(cid) from course) ;

# 查詢沒有學全所有課的同學的學號、姓名;

select sid,sname from student where sid in (

select student_id from (select count(course_id) c_course_id,student_id from score group by student_id

) t1 where t1.c_course_id <  (select count(cid) from course)

) ;

 

2、查詢和“002”號的同學學習的課程完全相同的其他同學學號和姓名;

# 先查詢2號同學學了哪些課程

select * from score where student_id =2;

# 找到學習了2號同學沒學習課程的所有同學(找到所有和2號同學學習的課程不一樣的同學)

select student_id from score where course_id not in (select course_id from score where student_id=2)

# 找到score表中所有的學生並且把 2號同學 以及(和2號同學學習的課程不一樣的同學)排除出去

select student_id from score where student_id not in (select student_id from score where course_id not in (select course_id from score where student_id=2)) and student_id !=2

# 對剩余的和2號同學所選課程沒有不同的同學所選課程數進行統計,如果和2號同學的課程數相同,就是選擇了相同的課程

select student_id from score where student_id not in (

select student_id from score where course_id not in (select course_id from score where student_id=2)

) and student_id !=2

group by student_id 

having count(course_id)= (select count(course_id) from score where student_id=2);

 

3、刪除學習“葉平”老師課的SC(score)表記錄;

# 先查出李平老師的id

select tid from teacher where tname = '李平老師';

# 查看李平老師所教授的課程

select cid from course where teacher_id = (select tid from teacher where tname = '李平老師’);

# 查看李平老師所教課程的成績數據

select * from score where course_id in (select cid from course where teacher_id = (select tid from teacher where tname = '李平老師'));

# 執行刪除命令

delete from score where course_id in (select cid from course where teacher_id = (select tid from teacher where tname = '李平老師'));

 

4、向SC表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“002”課程的同學學號;②插入“002”號課程的平均成績; 

#  先找尋上過2號課程的同學

select student_id from score where course_id = 2;

# 再找到沒上過2號課程的所有同學

select * from student where sid not in (select student_id from score where course_id = 2);

#  計算出學習2號課程的同學的平均成績

select avg(num) from score where course_id = 2 group by course_id;

# 用笛卡爾積將上述兩個表拼起來

select * from (select sid from student where sid not in (select student_id from score where course_id = 2)) t1,(select avg(num) from score where course_id = 2 group by course_id) t2;

#  向SC表中插入記錄

insert into score (course_id,student_id,num)   select 2,t1.sid,t2.avg_num from (select sid from student where sid not in (select student_id from score where course_id = 2)) t1,(select avg(num) avg_num from score where course_id = 2 group by course_id) t2;

 

5、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;

# 查看每個學生的數學成績

select student_id,num from score where course_id = (select cid from course where cname = '數學');

#  查看每個學生的語文成績

select student_id,num from score where course_id = (select cid from course where cname = '語文');

#  查看每個學生的英語成績

select student_id,num from score where course_id = (select cid from course where cname = '英語');

# 查看每個學生的平均成績

select student_id,avg(num),count(num) from score group by student_id;

# 將上面的幾張表拼接起來,為了生成所有學生的信息,用student表作為左連接的第一張表

select sid 學生ID,t2.num 語文,t1.num 數學, t3.num 英語,t4.count_course 有效課程數,t4.avg_num 有效平均分 from student 

left join (select student_id,num from score where course_id = (select cid from course where cname = '數學')) t1

on student.sid = t1.student_id

left join (select student_id,num from score where course_id = (select cid from course where cname = '語文')) t2

on student.sid = t2.student_id

left join (select student_id,num from score where course_id = (select cid from course where cname = '英語')) t3

on student.sid = t3.student_id

left join (select student_id,avg(num) avg_num,count(num) count_course from score group by student_id)  t4

on student.sid = t4.student_id

 

6、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;

select course_id 課程ID,max(num) 最高分,min(num) 最低分 from score group by course_id;

 

7、按各科平均成績從低到高和及格率的百分數從高到低順序;

# 方法1:

# 先求平均成績

select course_id,avg(num) from score group by course_id;

# 解決計算各科及格率的問題

所有及格的人/所有人數

select t1.course_id,t1.count1/t2.count2 from 

(select course_id,count(course_id) count1 from score where num>60 group by course_id) t1 

left join

(select course_id,count(course_id) count2 from score group by course_id) t2

on t1.course_id = t2.course_id;

# 根據上述內容進行表的拼接

select  t_out1.course_id,t_out1.avgnum, t_out2.pass_per from 

(select course_id,avg(num) avgnum from score group by course_id ) t_out1

left join 

(select t1.course_id,t1.count1/t2.count2 pass_per from 

(select course_id,count(course_id) count1 from score where num>60 group by course_id) t1 

left join

(select course_id,count(course_id) count2 from score group by course_id) t2

on t1.course_id = t2.course_id) t_out2

on  t_out1.course_id = t_out2.course_id

# 加上排序

select  t_out1.course_id,t_out1.avgnum, t_out2.pass_per from  (select course_id,avg(num) avgnum from score group by course_id ) t_out1 left join  (select t1.course_id,t1.count1/t2.count2 pass_per from  (select course_id,count(course_id) count1 from score where num>60 group by course_id) t1  left join (select course_id,count(course_id) count2 from score group by course_id) t2 on t1.course_id = t2.course_id) t_out2 on  t_out1.course_id = t_out2.course_id order by avgnum ,pass_per desc;

 

 

# 方法2 

# 使用case when直接計算合格率

select 

sum(case when num>60 then 1 else 0 end)/count(course_id)

from score group by course_id

# 加上課程id和平均值

select  course_id,avg(num),

sum(case when num>60 then 1 else 0 end)/count(course_id)

from score group by course_id

# 加上排序

select  course_id,avg(num) avgnum,

sum(case when num>60 then 1 else 0 end)/count(course_id) pass_per 

from score group by course_id

 order by avgnum ,pass_per desc;

 

 

8、查詢各科成績前三名的記錄:(不考慮成績並列情況) 

select
t1.sid,t1.student_id,t1.course_id,t1.num from score t1
left join
(
select sid,course_id,
(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,
(select num from score as s2 where s2.course_id = s1.course_id order by num desc limit 2, 1) as third_num
from score as s1
) t2
on t1.sid = t2.sid
where t1.num = t2.first_num or t1.num = t2.second_num or t1.num = t2.third_num;

 

 

 

9、查詢每門課程被選修的學生數;

select course_id,count(course_id) from score group by course_id;

 

10、查詢同名同姓學生名單,並統計同名人數;

select sname,count(1) as count from student group by sname;

 

11、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;

select course_id,avg(if(isnull(num), 0 ,num)) as avg from score group by course_id order by avg  asc,course_id desc;

 

12、查詢平均成績大於85的所有學生的學號、姓名和平均成績;

select student_id,sname, avg(if(isnull(num), 0 ,num)) from score left join student on score.student_id = student.sid group by student_id;

 

13、查詢課程名稱為“數學”,且分數低於60的學生姓名和分數;

select student.sname,score.num from score

left join course on score.course_id = course.cid

left join student on score.student_id = student.sid

where score.num < 60 and course.cname = '數學'

 

 

14、查詢課程編號為003且課程成績在80分以上的學生的學號和姓名; 

select * from score where score.student_id = 3 and score.num > 80

 

15、求選了課程的學生人數

select sid,sname from student where sid not in (select student_id from score group by student_id);

 

16、查詢選修“楊艷”老師所授課程的學生中,成績最高的學生姓名及其成績;

# 先找到“楊艷”老師的教師id

select tid from teacher where tname = '楊艷';

# 再找到楊艷老師教的所有課程

select cid from course where teacher_id in (select tid from teacher where tname = '楊艷');

# 再找到楊艷老師教的所有課程的最高分

select max(num) from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'));

# 再找到楊艷老師教的所有課程的最高分對應的學生

select distinct student_id,num from score 

where num = (select max(num) from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'))) 

and course_id in   (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'));

# 找到學生的姓名

select student.sname,t1.num from(

select distinct student_id,num from score 

where num = (select max(num) from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'))) 

and course_id in   (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'))

) t1

left join

student

on 

t1.student_id = student.sid;

 

17、查詢各個課程及相應的選修人數;

select course.cname,count(1) from score

left join course on score.course_id = course.cid

group by course_id;

 

18、查詢不同課程但成績相同的學生的學號、課程號、學生成績;

select DISTINCT s1.course_id,s2.course_id,s1.num,s2.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id;

 

19、查詢每門課程成績最好的前兩名;

   先查詢每條數據對應學科成績的第一名和第二名,這里必須要保留所有的s1,以便后續進行連表查詢

select sid,course_id,
(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

按照sid連表,把學生的成績和對應的第一名、第二名成績連起來
select
* from score t1
left join
(
select sid,course_id,
(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
) t2
on t1.sid = t2.sid

判斷如果學生的成績是第一名、第二名的成績,那么就符合條件,顯示學生的id、學科和成績
select
t1.sid,t1.student_id,t1.course_id,t1.num from score t1
left join
(
select sid,course_id,
(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
) t2
on t1.sid = t2.sid
where t1.num = t2.first_num or t1.num = t2.second_num;

20、檢索至少選修兩門課程的學生學號;

select student_id from score group by student_id having count(student_id) > 1;

 

21、查詢全部學生都選修的課程的課程號和課程名;

# 先查看一共有多少學生

select count(sid) from student;

#  查看哪一門課選秀的學生個數和學生的總個數相等

select course_id from score group by course_id having count(student_id) = (select count(sid) from student);

 

22、查詢沒學過“葉平”老師講授的任一門課程的學生姓名;

# 先查看要查找老師的id

select tid from teacher where tname = '李平老師';

# 查看該老師交了哪些課程

select cid from course where teacher_id in (select tid from teacher where tname = '李平老師')

# 看看有多少學生學習了該老師的課程

select distinct student_id from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師'));

# 把不在上表中的學生姓名查出來

select sname from student where sid not in (select distinct student_id from score where course_id in (select cid from course where teacher_id in (select tid from teacher where tname = '李平老師')));

 

23、查詢兩門以上不及格課程的同學的學號及其平均成績;

select student_id,avg(num) from score where num<60 group by student_id having count(num)>=2;

 

 

24、檢索“004”課程分數小於60,按分數降序排列的同學學號;

select student_id from score where num< 60 and course_id = 4 order by num desc;

 

25、刪除“002”同學的“001”課程的成績;

delete from score where course_id = 1 and student_id = 2


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM