【mysql 練習題】查詢和“01”號同學所學課程完全相同的其他同學的學號

先說一件有點難過的事情,我還是放棄了想做數據分析師~投了好多好多好多的簡歷,只有幾個面試,可能真的和我長時間的空白期有關吧,哪怕很認真準備了很久也沒辦法得到青睞。把mysql的50題刷完~看一看剩下的投遞裏會不會有面試,這段很努力很努力的時光就暫時封存起來吧,可能未來我會在知識產權領域大有作爲呢!

如果有需要數據分析相關資源的小夥伴可以找我~python,mysql,power bi,商業數據分析我這裏都有學習的資料。

插入表: 

#學生表
CREATE TABLE `Student`(
`s_id` VARCHAR(20),
`s_name` VARCHAR(20) NOT NULL DEFAULT '',
`s_birth` VARCHAR(20) NOT NULL DEFAULT '',
`s_sex` VARCHAR(10) NOT NULL DEFAULT '',
PRIMARY KEY(`s_id`)
);
#課程表
CREATE TABLE `Course`(
`c_id` VARCHAR(20),
`c_name` VARCHAR(20) NOT NULL DEFAULT '',
`t_id` VARCHAR(20) NOT NULL,
PRIMARY KEY(`c_id`)
);
#教師表
CREATE TABLE `Teacher`(
`t_id` VARCHAR(20),
`t_name` VARCHAR(20) NOT NULL DEFAULT '',
PRIMARY KEY(`t_id`)
);
#成績表
CREATE TABLE `Score`(
`s_id` VARCHAR(20),
`c_id` VARCHAR(20),
`s_score` INT(3),
PRIMARY KEY(`s_id`,`c_id`)
);
#插入學生表測試數據
insert into Student values('01' , '趙雷' , '1990-01-01' , '男');
insert into Student values('02' , '錢電' , '1990-12-21' , '男');
insert into Student values('03' , '孫風' , '1990-05-20' , '男');
insert into Student values('04' , '李雲' , '1990-08-06' , '男');
insert into Student values('05' , '周梅' , '1991-12-01' , '女');
insert into Student values('06' , '吳蘭' , '1992-03-01' , '女');
insert into Student values('07' , '鄭竹' , '1989-07-01' , '女');
insert into Student values('08' , '王菊' , '1990-01-20' , '女');
#課程表測試數據
insert into Course values('01' , '語文' , '02');
insert into Course values('02' , '數學' , '01');
insert into Course values('03' , '英語' , '03');

#教師表測試數據
insert into Teacher values('01' , '張三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');

#成績表測試數據
insert into Score values('01' , '01' , 80);
insert into Score values('01' , '02' , 90);
insert into Score values('01' , '03' , 99);
insert into Score values('02' , '01' , 70);
insert into Score values('02' , '02' , 60);
insert into Score values('02' , '03' , 80);
insert into Score values('03' , '01' , 80);
insert into Score values('03' , '02' , 80);
insert into Score values('03' , '03' , 80);
insert into Score values('04' , '01' , 50);
insert into Score values('04' , '02' , 30);
insert into Score values('04' , '03' , 20);
insert into Score values('05' , '01' , 76);
insert into Score values('05' , '02' , 87);
insert into Score values('06' , '01' , 31);
insert into Score values('06' , '03' , 34);
insert into Score values('07' , '02' , 89);
insert into Score values('07' , '03' , 98);

這一個想了很久~還是借鑑了其他人的答案

查詢和“01”號同學所學課程完全相同的其他同學的學號

查詢‘01’同學所學的課程

select c_id from score where s_id='01'

查詢學習了‘01’同學沒學過課程的同學

select s_id from score
where c_id not in(select c_id from score where s_id='01')

用not in查詢和‘01’同學學過課程相同,且課程數量相等的人

select distinct s_id from score where s_id not in
(select s_id from score where c_id not in (select c_id from score where s_id='01'))
group by s_id
having count(c_id)=(select count(c_id) from score where s_id='01') and s_id<>'01'

用student表查學生信息

select * from student 
where s_id in(
select distinct s_id from score where s_id not in
(select s_id from score where c_id not in (select c_id from score where s_id='01'))
group by s_id
having count(c_id)=(select count(c_id) from score where s_id='01') and s_id<>'01')

其他的練習題:

#1.查詢課程編號爲“01”的課程比“02”的課程成績高的所有學生的學號(重點)
select Student.*,Score.*
from Student join score on student.s_id=score.s_id 
where student.s_id in
(select s1.s_id from
(select s_id,s_score from Score where c_id='01') s1,(select s_id,s_score from Score where c_id='02') s2
where s1.s_id=s2.s_id and s1.s_score>s2.s_score);

#2.查詢平均成績大於60分的學生的學號和平均成績
select student.s_id,avg(s_score)
from student,score
where student.s_id=score.s_id
group by student.s_id
having avg(s_score)>60;
#只需要學生表的話 可以用in
select student.*
from student
where student.s_id in
(select s_id from score group by s_id having avg(s_score)>60);

#查詢沒學過“張三”老師課的學生的學號、姓名(重點)
select student.s_id, student.s_name
from student where s_id not in
(select s_id from course,teacher,score where course.t_id=teacher.t_id and course.c_id=score.c_id and teacher.t_name='張三');

#查詢學過“張三”老師所教的所有課的同學的學號、姓名(重點)
select student.s_id,student.s_name
from student,course,teacher,score 
where course.t_id=teacher.t_id and course.c_id=score.c_id and teacher.t_name='張三' and student.s_id=score.s_id
group by student.s_id
having count(s_score)=(select count(c.c_id) from course c, teacher t where c.t_id=t.t_id and t.t_name='張三')

#查詢學過編號爲“01”的課程並且也學過編號爲“02”的課程的學生的學號、姓名(重點)
select a.id, a.name from
(select student.s_id id, student.s_name name
from student,score
where student.s_id=score.s_id and score.c_id='01') a
inner join 
(select student.s_id id, student.s_name name
from student,score
where student.s_id=score.s_id and score.c_id='02') b
on a.id=b.id;
#可以用student作爲主表 where student.s_id in 
select student.s_id,student.s_name
from student
where student.s_id in
(select a.id from (select score.s_id id from score where score.c_id='01') a inner join (select score.s_id id from score where score.c_id='02') b
where a.id=b.id );

#查詢所有課程成績小於60分的學生的學號、姓名
#這個暫時不對!!!
select student.s_id from student where s_id in
(select score.s_id from score, (select s_id,count(c_id) num from score group by s_id) cc
where s_score<60 and score.s_id=cc.s_id
group by s_id
having count(score.c_id)=cc.num);

#查詢沒有學全所有課的學生的學號、姓名(
select student.s_id,student.s_name
from student,score
where student.s_id=score.s_id
group by student.s_id
having count(score.c_id)<
(select count(distinct c_id) from course);

#查詢至少有一門課與學號爲“01”的學生所學課程相同的學生的學號和姓名
select distinct student.s_id,s_name from student,score
where student.s_id=score.s_id
and score.c_id in
(select c_id from score where s_id='01')
and  student.s_id!='01';

#查詢和“01”號同學所學課程完全相同的其他同學的學號
select * from student 
where s_id in(
select distinct s_id from score where s_id not in
(select s_id from score where c_id not in (select c_id from score where s_id='01'))
group by s_id
having count(c_id)=(select count(c_id) from score where s_id='01') and s_id<>'01');

#查詢沒學過"張三"老師講授的任一門課程的學生姓名
select s_name from student 
where s_id not in(
select s_id from score where c_id in(
select c_id from course,teacher
where course.t_id=teacher.t_id and t_name='張三'));

#查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select student.s_id,student.s_name,avg(s_score)
from student, score
where student.s_id=score.s_id and s_score<60
group by student.s_id
having count(c_id)>=2;
# case when then else end
select student.s_id,student.s_name,avg(s_score)
from student, score
where student.s_id=score.s_id 
group by student.s_id
having sum(case when s_score<60 then 1 else 0 end)>=2;

#按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
select student.s_id,
sum(case when c_id='01' then s_score else NULL end ) cid1,
sum(case when c_id='02' then s_score else NULL end ) cid2,
sum(case when c_id='03' then s_score else NULL end ) cid3,
avg(s_score) average
from student,score
where student.s_id=score.s_id
group by student.s_id
order by average desc;


#查詢各科成績最高分、最低分和平均分:
#以如下形式顯示:課程ID,課程name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率
#注意如果case when用於count則else必須是NULL不能是0
select score.c_id,course.c_name,max(s_score),min(s_score),avg(s_score),
sum(case when s_score>=60 then 1 else 0 end)/count(s_score) '及格率',
sum(case when s_score>=90 then 1 else 0 end)/count(s_score) '優秀率'
from score,course where score.c_id=course.c_id
group by score.c_id;

#按各科成績進行排序,並顯示排名
#row_number 不重複123, rank 存在相同排名重複113, dense_rank 相同排名重複 112 
select s_id, s_score, row_number() over(partition by c_id order by s_score desc) rank_cid
from score;

#查詢所有課程的成績第2名到第3名的學生信息及該課程成績
#注意where中不能用select中定義的列 因爲順序是 from-where-select
select s_id,s_score from
(select s_id, s_score, row_number() over(partition by c_id order by s_score desc) rank_cid
from score) cc
where rank_cid in (2,3) ;

#查詢各科成績前三名的記錄(不考慮成績並列情況)
select c_id,
sum(case when rank_cid=1 then s_score else null end) 'first',
sum(case when rank_cid=2 then s_score else null end) '2nd',
sum(case when rank_cid=3 then s_score else null end) '3rd'
from (select c_id,s_id,s_score, row_number() over (partition by c_id order by s_score desc) rank_cid from score) cc
group by c_id;

#查詢不同老師所教不同課程平均分從高到低顯示
select course.t_id,course.c_id,avg(s_score) average
from course,teacher,score
where course.t_id=teacher.t_id and course.c_id=score.c_id
group by course.t_id,course.c_id
order by average desc;

#查詢出只有兩門課程的全部學生的學號和姓名
select student.s_id,student.s_name
from student
where student.s_id in
(select score.s_id from score group by score.s_id
having count(c_id)=2)

#查詢任何一門課程成績在70分以上的姓名、課程名稱和分數
select distinct student.s_id,student.s_name,c_id,score.s_score
from student inner join score on student.s_id=score.s_id
where student.s_id not in
(select s_id from score where s_score<70)

#查詢選修“張三”老師所授課程的學生中成績最高的學生姓名及其成績
select student.s_id, s_name, score.s_score
from student inner join score on student.s_id=score.s_id
where c_id in(select course.c_id from course inner join teacher
              on course.t_id=teacher.t_id where t_name='張三')
order by s_score desc
limit 0,1

#查詢不同課程成績相同的學生的學生編號、課程編號、學生成績 (重點)
select distinct s1.s_id,s1.c_id,s1.s_score
from score s1 join score s2
on s1.s_id=s2.s_id 
where s1.c_id<>s2.c_id  and s1.s_score=s2.s_score
order by c_id

#查詢每門課程成績最好的前兩名
select s_id, c_id from(
select s_id,c_id,s_score,row_number() over(partition by c_id order by s_score desc) rn
from score) cc
where cc.rn in (1,2);

# 查詢選修了全部課程的學生信息
select student.* from student
where s_id in
(select s_id from score group by s_id having count(c_id)=(select count(distinct c_id) from course));

#查詢各學生的年齡
#按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一(這個我抄的!!!)
SELECT s_name,s_birth,
(DATE_FORMAT(NOW(),'%Y') - DATE_FORMAT(s_birth,'%Y') -
(CASE WHEN DATE_FORMAT(NOW(),'%m%d') > DATE_FORMAT(s_birth,'%m%d') 
THEN 0 ELSE 1 END)
) AS age
FROM student;

#本月過生日
SELECT * FROM student
where MONTH(s_birth)=MONTH(DATE(now()))
 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章