1. 程式人生 > 實用技巧 >C語言基數排序(浙江大學資料結構)

C語言基數排序(浙江大學資料結構)

暫時沒學過檢視

1.a表外鏈b表 想成生成一個臨時表,在這個表裡面可以進行條件判斷,篩選操作

2.不管是外鏈查詢還是同資料對比都需要明確外鏈主鍵或者同表主鍵,通過主鍵關聯表格可以更快組合獲取想要的資料

3.()內可以想成一個表,可以作為where n in ()的條件,也可以作為臨時表,如需命名可以用()as 名稱 使用

4.avg()等分組後的值的平均數、求和最好進行命名as name 否則會被錯誤認成函式

5.第五、八題內容相識,都是對同一表內資料篩選後根據以表內同一學生(非sid、這裡同學同時滿足兩個條件)外鏈在一起,由此進行篩選

6、SELECT count(1) from course 這裡count(1)表示計數可以用在where條件中

7.計算滿足條件的某列資料個數:select count(1) from score where student_id=2; count 不分組中的使用

8.select 可以的物件可以是固定值,也可以是固定動態查詢語句 :select id,name,1,(select count(1) from tb) 動態篩選 from tb2

9.group by 在對(例:select * from table1) 表中運用時,不需要在對該表進行select from(例)的操作,並且此時 * 失效,需要根據group by 的物件進行重新篩選

10、判斷avg(if(isnull(num),0,num)) 當需要對資料進行操作時,可能存在未null的情況需對資料進行判斷

11.distinct 去重 select count(distinct student_id) from score

歸納總結:

1.建表:

(1)class

主鍵為cid,非空自增,自增初始值為5(這裡可以不設定)

CREATE TABLE `class` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `caption` varchar(32) NOT NULL,
  PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO `class` VALUES ('1', '三年二班'), ('2', '三年三班'), ('3', '一年二班'), ('4', '二年九班');

(2)course

cid主鍵非空自增,外來鍵teacher表tid  

CREATE TABLE `course` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `cname` varchar(32) NOT NULL,
  `teacher_id` int(11) NOT NULL,
  PRIMARY KEY (`cid`),
  KEY `fk_course_teacher` (`teacher_id`),
  CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '物理', '2'), ('3', '體育', '3'), ('4', '美術', '2');

(3)student

CREATE TABLE `student` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `gender` char(1) NOT NULL,
  `class_id` int(11) NOT NULL,
  `sname` varchar(32) NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `fk_class` (`class_id`),
  CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8;
INSERT INTO `student` VALUES ('1', '男', '1', '理解'), ('2', '女', '1', '鋼蛋'), ('3', '男', '1', '張三'), ('4', '男', '1', '張一'), ('5', '女', '1', '張二'), ('6', '男', '1', '張四'), ('7', '女', '2', '鐵錘'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四'), ('12', '女', '3', '如花'), ('13', '男', '3', '劉三'), ('14', '男', '3', '劉一'), ('15', '女', '3', '劉二'), ('16', '男', '3', '劉四');

(4)score  

CREATE TABLE `score` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `student_id` int(11) NOT NULL,
  `course_id` int(11) NOT NULL,
  `num` int(11) NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `fk_score_student` (`student_id`), --這裡是命名
  KEY `fk_score_course` (`course_id`),
  CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
  CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8;
INSERT INTO `score` VALUES ('1', '1', '1', '10'), ('2', '1', '2', '9'), ('5', '1', '4', '66'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '99'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'), ('26', '7', '1', '9'), ('27', '7', '2', '100'), ('28', '7', '3', '67'), ('29', '7', '4', '88'), ('30', '8', '1', '9'), ('31', '8', '2', '100'), ('32', '8', '3', '67'), ('33', '8', '4', '88'), ('34', '9', '1', '91'), ('35', '9', '2', '88'), ('36', '9', '3', '67'), ('37', '9', '4', '22'), ('38', '10', '1', '90'), ('39', '10', '2', '77'), ('40', '10', '3', '43'), ('41', '10', '4', '87'), ('42', '11', '1', '90'), ('43', '11', '2', '77'), ('44', '11', '3', '43'), ('45', '11', '4', '87'), ('46', '12', '1', '90'), ('47', '12', '2', '77'), ('48', '12', '3', '43'), ('49', '12', '4', '87'), ('52', '13', '3', '87');

(5)teacher

CREATE TABLE `teacher` (
  `tid` int(11) NOT NULL AUTO_INCREMENT,
  `tname` varchar(32) NOT NULL,
  PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
INSERT INTO `teacher` VALUES ('1', '張磊老師'), ('2', '李平老師'), ('3', '劉海燕老師'), ('4', '朱雲海老師'), ('5', '李傑老師');

2.操作查詢

簡單查詢:

1.找出num大於60的資料

select * from score where num>60

2.找出course表中老師任了幾門課(分組操作)

select teacher_id,count(cname)  from course GROUP BY teacher_id 

3.課程表顯示老師中文名(連表操作)

SELECT * from course
LEFT JOIN teacher on course.teacher_id =teacher.tid;

4.列出學生的資訊和班級資訊(連表操作)

SELECT * FROM student  
LEFT JOIN class on student.class_id=class.cid;

5.student表中男生女生的個數(分組)

根據gender進行group by ,通過count()進行取數

select gender,count(gender) from student GROUP BY gender;
select gender,count(sid) from student GROUP BY gender;
select gender,count(sname) from student GROUP BY gender;
select gender,count(1) from student GROUP BY gender;

6.臨時表/子查詢

select num,course_id  from (SELECT num,course_id  from score where num>60) as B; 

7.同表中 表名.行名 與 表名.行名對比滿足條件保留

如假設取出score表中 student_id 比oure_id 大的 sid和num

SELECT sid,num FROM score WHERE score.student_id > score.course_id

操作表查詢方法組合

1、查詢平均成績大於60分的同學的學號和平均成績;

a、查詢score表

b、平均成績大於60 avg、每個學生應該group by

SELECT student_id,avg(num) from score GROUP BY student_id having avg(num)>60;

c、在新增條件avg(num)>60

having avg(num)>60

SELECT student_id,avg(num) from score GROUP BY student_id HAVING avg(num)>60;

d、 附加:查詢滿足上面條件的學生的姓名-連表操作

SELECT * from (SELECT student_id,avg(num) from score GROUP BY student_id HAVING avg(num)>60)as B 
LEFT JOIN   student on  B.student_id=student.sid;

精確獲取student_id sname,以及avg(num)

注意這裡avg(num)會被誤認為函式報錯,需要最avg(num)進行命名

SELECT B.student_id,student.sname,B.ccc  from (SELECT student_id,avg(num) as ccc from score GROUP BY student_id HAVING avg(num)>60)as B  
LEFT JOIN   student on  B.student_id=student.sid;

2、查詢所有同學的學號、姓名、選課數、總成績;

連表查詢+分組查詢

連表不用as做臨時表 select 是需要的,連表可以理解成一張整表,不過select內部元素時需要帶上表名

select score.student_id,student.sname,count(student_id),sum(num) from score left JOIN student on score.student_id =student.sid GROUP BY score.sid 

附加:以sum(num)進行排序

select score.student_id,student.sname,count(student_id),sum(num) from 
score left JOIN student on score.student_id =student.sid GROUP BY score.sid  ORDER BY sum(num) asc; 

3.查詢姓李的老師個數

like的用法,count計數count(1)

select  *  from teacher where teacher.tname like '李%' 
SELECT count(1) from (select  *  from teacher where teacher.tname like '李%' )as B 

4.查詢沒學過“李平”老師課的同學的學號、姓名

思考:這裡存在有一個學生選擇多門課程的情況,當篩到這麼名學生coures_id 不在時依舊會取出student_id,但是實際上這個人是學過的

所以一定要先考慮是否有student_id重合的情況

先篩出( 學過的學生id)再獲得沒學過的學生id,之後獲得滿足的學號姓名

a、先查詢老師的任課情況,連表查詢李平老師任教的課(cid)

SELECT course.cid from  course left JOIN  teacher on course.teacher_id= teacher.tid where teacher.tname='李平老師'

 

b、查詢學過的學生score.student_id

SELECT student_id from score where course_id in(
SELECT 
course.cid 
from 
course 
left JOIN teacher on course.teacher_id= teacher.tid 
where 
teacher.tname='李平老師'
)GROUP BY student_id

c、再查詢student表裡student.sid不在已查的sid裡的

select student.sid,student.sname  from student WHERE sid not in (
SELECT  student_id  from score  where course_id in(
SELECT 
 course.cid 
from  
 course 
left JOIN  teacher on course.teacher_id= teacher.tid 
where 
 teacher.tname='李平老師'
)GROUP BY student_id
)

5.查詢“生物”課程比“物理”課程成績高的所有學生的學號

考察 先連結串列 分別查詢出滿足條件的資料,在通過二者間存在唯一主鍵學生,進行外鏈,組合後,通過表名.行名的對比返回滿足條件的學生學號

解析:

a、查詢student表中課程是生物的及分數 ,查詢student 表中課程是物理的及分數

SELECT A.student_id FROM (SELECT  score.student_id,score.num,course.cname  from score left JOIN course 
on course.cid=score.course_id WHERE course.cname='物理')as A LEFT JOIN (SELECT score.student_id,score.num,course.cname from score left JOIN course on course.cid=score.
course_id WHERE course.cname='生物') as B on A.student_id=B.student_id WHERE A.num<B.num

  

深化 顯示所有學生姓名 

SELECT C.student_id,student.sname  
FROM(SELECT A.student_id
FROM (SELECT score.student_id,score.num,course.cname from score left JOIN course on course.cid=score.
course_id WHERE course.cname='物理')
as A LEFT JOIN (SELECT score.student_id,score.num,course.cname from score left JOIN course on course.cid=score.
course_id WHERE course.cname='生物')
as B on A.student_id=B.student_id WHERE A.num<B.num) as C LEFT JOIN student on student.sid=C.student_id

6.查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名;

錯誤思路:篩選出學過001 and 002的同學,這裡and錯誤,因為表內001與002資料皆為單獨存在,不存在單獨情況

正確思路:可以篩出course_id為1或者2的資料,對student_id進行分組,若學生數大於1則篩出學生的學號,並連結串列查詢姓名

SELECT B.student_id,student.sname from (select * from score where course_id=1 or course_id=2)as B 
LEFT JOIN student on B.student_id=student.sid GROUP BY B.student_id HAVING count(B.student_id)>1

 

7.查詢學過“李平”老師所教的所有課的同學的學號、姓名;

a、明確查詢同學的學號、姓名所在的表,

b、明確查詢條件,查詢的老師和對應課程

c、連結串列查詢

A.student_id,student.sname from (SELECT cid  FROM score where course_id in(select course.cid  from  course LEFT JOIN teacher on course.teacher_id=teacher.tid  where teacher.tname='李平老師'))as A  
LEFT JOIN student on A.student_id=student.sid  GROUP BY A.student_id HAVING count(A.student_id)=(select count(course.cid)  from  course LEFT JOIN teacher on course.teacher_id=teacher.tid  where teacher.tname='李平老師')

8、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名;

同第五題

SELECT C.student_id,student.sname  FROM

(select A.sid,A.student_id  from (select sid,student_id,num from score where  course_id=2) as A 
left JOIN
(select sid,student_id,num from score where  course_id=1)as B 
on A.student_id =B.student_id where A.num<B.num) as C  LEFT JOIN  student on student.sid=C.student_id

  

9、查詢有課程成績小於60分的同學的學號、姓名;

a、第一種使用篩選出student表中sid在(篩選score表的student_in)中的資料

這裡可以 用distinct去重,但是效率不高

  select sid,sname from student where sid in (
        select distinct student_id from score where num < 60
    )

b、第二種先篩選score表中的student—id 再連結串列查詢姓名,group by 去重

select A.student_id ,student.sname from 
(SELECT student_id ,num from score where num<60) as A
LEFT JOIN student
on student.sid =A.student_id GROUP BY A.student_id  

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

使用count(1) 或者count(主鍵) 進行計數

思路

a、在分數表中根據學生進行分組,獲取每一個學生選課數量

b、如果數量= 總課程數量,表示 已經選擇了所有課程

SELECT A.student_id,student.sname from
(SELECT student_id,count(student_id) from score GROUP BY student_id  
HAVING count(student_id)  < (SELECT count(1)  from course ))as A
left JOIN student
on student.sid=A.student_id

  

11、查詢至少有一門課與學號為“001”的同學所學相同的同學的學號和姓名;

思路:獲取001同學選擇的所有課程

   獲取課程在其中的所有人以及所有課程

   根據學生篩選,獲取所有學生資訊

   再與學生表連線,獲取姓名 

select 學生id from 分數表 where 學生id!=1 and 課程id in (學號為1的課程id ) group by 學生id

SELECT  A.student_id,student.sname FROM
(select student_id from score WHERE student_id !=1 and course_id  
in (select course_id from score where student_id=1) GROUP BY student_id
)as A LEFT JOIN student on student.sid=A.student_id

12、查詢至少學過學號為“001”同學所有課的其他同學學號和姓名;

a、先篩選出至少有一門課與學號為“001”的同學所學相同的同學的學號

SELECT student_id,count(1) from score WHERE student_id !=1 and course_id in (SELECT course_id FROM score 
WHERE student_id =1) GROUP BY student_id;

 

b、由於a裡面判斷了course_id in 001同學的courser_id的情況,並且最多隻能是3,所以判斷count(1)與1同學相等,相等的情況不止有001同學的所有課的情況可能還有別的選課

SELECT student_id,count(1) from score WHERE student_id !=1 and course_id in (SELECT course_id FROM score 
WHERE student_id =1) GROUP BY student_id HAVING count(1) =(SELECT count(course_id) FROM score 
WHERE student_id =1)

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

獲取選課個數相同的學生,

002學得課程也學過

select student_id  FROM score WHERE student_id in (
select student_id from score 
WHERE student_id !=2
GROUP BY student_id  HAVING count(1)=(select  count(1) 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(1)=(select  count(1) from score  where student_id=2)

 

14、刪除學習“李平”老師課的score表記錄 

 找到李平老師對應的課,找出courser_id in 李平的課 ,刪除

DELETE  from  score  where  course_id in (select course.cid  from course left JOIN teacher on teacher.tid=course.teacher_id where teacher.tname='李平老師')

 

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

insert into table() select * from table1 的格式

select 

  id,

  name,

  1,

  (select count(1) from tb) 動態篩選

from tb2

INSERT INTO score(student_id,course_id,num) 
select student_id ,2, (SELECT AVG(NUM)  from score WHERE course_id =2)  FROM score where course_id !=2

  

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

a、篩出課程表

select * 
from score ; 

b、在原表基礎上進行遍歷篩選(測試)

select   
student_id,
(select num  from score as s2 where s2.student_id = s1.student_id and course_id =1) as '語文',
(select num  from score as s2 where s2.student_id = s1.student_id and course_id =2)as '數學',
(select num  from score as s2 where s2.student_id = s1.student_id and course_id =3) as '英語'

from score  as s1;

針對s1表student_id 的條件再對s2每一級的滿條件(s2的student_id對應s1的student_id 並滿足course_id =某值)的num 進行篩選

c、針對滿足的條件的內容進行篩選

select sc.student_id,
(select num from score left join course on score.course_id = course.cid where course.cname = "生物" and score.student_id=sc.student_id) as sy,
(select num from score left join course on score.course_id = course.cid where course.cname = "物理" and score.student_id=sc.student_id) as wl,
(select num 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.num)
from score as sc
group by student_id        

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

group by ,max,min的用法

select  course_id,max(num) as max_num,min(num) as min_num

from  score GROUP BY course_id

附加:讓最低分小於10的顯示為0

case when 條件 then 滿足的結果 els 不滿足的結果 end

select  course_id,max(num) as max_num,min(num),case when min(num)<10 then 0 else min(num) end as min_num
from  score GROUP BY course_id;

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

case when then else end

select student_id,course_id,num  from score;

a、 通過case對資料進行清洗,使得小於60的為0 大於60的為1,通過對0,1的計數來獲取及格率

select student_id ,course_id ,num ,case when num <60 then 0 else 1 end as count_num from score

b、 對course_id 進行分組,分組後對num進行avg 求出平均值,對為0為1的數計數求和,以及通過sum(1)對所有數計數求和,為0的數量/sum(1)的數即為及格率

select course_id ,avg(num) as avg_num ,sum(case when num <60 then 0 else 1 end),sum(1),
sum(case when num <60 then 0 else 1 end)/sum(1) as pas

from score GROUP BY course_id;

c、按平均分和及格率分別排序

select course_id ,avg(num) as avg_num ,sum(case when num <60 then 0 else 1 end),sum(1),
sum(case when num <60 then 0 else 1 end)/sum(1) as p

from score GROUP BY course_id order by avg_num asc,p desc;

19、課程平均分從高到低顯示(現實任課老師)

三表連結串列操作,分組後排序

select avg(if(isnull(score.num),0,score.num)),teacher.tname 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

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

#order by num desc limit 0,1 表示倒敘排名第一 1.1表示第二

select

sid,

 條件一,

 條件二

from score

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(student_id) from score GROUP BY course_id;
SELECT  course_id ,count(1) from score GROUP BY course_id

22、查詢出只選修了一門課程的全部學生的學號和姓名;  

select * from score;
select student_id,student.sname from score
LEFT JOIN student on score.student_id = student.sid
group by student_id  HAVING count(course_id) =1  

23、查詢男生、女生的人數; 

group by 

-- 第一種
SELECT  gender,count(gender) FROM student GROUP BY gender ;

-- 第二種
select * from
    (select count(1) as man from student where gender='男') as A ,
    (select count(1) as feman from student where gender='女') as B

24、查詢姓“張”的學生名單;

where like 的用法

select  *  from student where  student.sname like'張%'

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

select  sname,count(1) from  student GROUP BY sname 

 

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

group by ;order by * asc, * desc

select  course_id,avg(num) from  score GROUP BY course_id order by avg(num) asc ,course_id desc

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

可以先連結串列後進行group by ,having判斷

注意avg(Num)在連結串列後A.avg(num)會被誤認為函式,請命名

注意加上判斷 avg(if(isnull(num),0,num))

select A.student_id,student.sname,A.nn from
(select student_id,avg(if(isnull(num),0,num))as nn from score group by student_id HAVING nn>85) as A 
left join student 
on student.sid = A.student_id

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

兩次連結串列,where 條件判斷 

或者 一次查詢的臨時表作為資料二次篩選

select student.sname, score.num from score
  left join course on course.cid = score.course_id
  left JOIN studenton student.sid= score.student_id
where course.cname = '生物' and score.num<60;select * from score where num<60 and course_id =(select cid from course where cname='生物');

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

一次連結串列,where a條件 and b條件

select student_id,student.sname 
from score 
left JOIN student on  score.student_id = student.sid
where score.num>80 and score.course_id=3

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

select count(distinct student_id) from score;

select student_id,count(student_id) from score GROUP BY student_id;
select count(c) from ( select count(student_id) as c from score group by student_id) as A

31、查詢選修“張磊”老師所授課程的學生中,成績最高的學生姓名及其成績

提取極值資訊:order by 後limit取數

select  *  from score;

select sname,num from score
left join student on score.student_id = student.sid;

-- 找到張磊老師課程id
select course.cid from course left join teacher on course.teacher_id = teacher.tid where tname='張磊老師';

-- 找到選了張磊老師課程的學生 select sname,num from score left join student on score.student_id = student.sid where score.course_id in (select course.cid from course left join teacher on course.teacher_id = teacher.tid where tname='張磊老師') order by num desc limit 1;

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

SELECT  score.course_id, count(1) 
from  score left JOIN course on course.cid = score.course_id 
group by  score.course_id

 

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

笛卡爾積概念

SELECT * from score as s1,score as s2;

SELECT * from score as s1,score as s2 where s1.sid !=s2.sid;



SELECT * from score as s1,score as s2 where s1.sid !=s2.sid  and s1.sid != s2.sid ;


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

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;

 

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

表示前幾名,最好的,需要二級篩選,第一是什麼,第二是什麼

select *  from score;


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;

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(student_id) > 1  

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

select course_id , count(1)  from  score   GROUP BY course_id having COUNT(1)= (SELECT count(1) from student)

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

select sid from student where 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 tname = '張磊老師'))

  

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

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

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

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

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

delete from score where course_id = 1 and student_id = 2