python初學——mysql練習50題

建立表
請建立以下表,並建立相關約束。如圖1
圖片描述
操做表中的內容
一、自行建立測試數據;
二、查詢學生總人數;
三、查詢「生物」課程和「物理」課程成績都及格的學生id和姓名;
四、查詢每一個年級的班級數,取出班級數最多的前三個年級;
五、查詢平均成績最高和最低的學生的id和姓名以及平均成績;
六、查詢每一個年級的學生人數;
七、查詢每位學生的學號,姓名,選課數,平均成績;
八、查詢學生編號爲「2」的學生的姓名、該學生成績最高的課程名、成績最低的課程名及分數;
九、查詢姓「李」的老師的個數和所帶班級數;
十、查詢班級數小於5的年級id和年級名;
十一、查詢班級信息,包括班級id、班級名稱、年級、年級級別(12爲低年級,34爲中年級,56爲高年級),示例結果以下;
圖片描述
十二、查詢學過「張三」老師2門課以上的同窗的學號、姓名;
1三、查詢教授課程超過2門的老師的id和姓名;
1四、查詢學過編號「1」課程和編號「2」課程的同窗的學號、姓名;
1五、查詢沒有帶太高年級的老師id和姓名;
1六、查詢學過「張三」老師所教的全部課的同窗的學號、姓名;
1七、查詢帶過超過2個班級的老師的id和姓名;
1八、查詢課程編號「2」的成績比課程編號「1」課程低的全部同窗的學號、姓名;
1九、查詢所帶班級數最多的老師id和姓名;
20、查詢有課程成績小於60分的同窗的學號、姓名;
2一、查詢沒有學全全部課的同窗的學號、姓名;
2二、查詢至少有一門課與學號爲「1」的同窗所學相同的同窗的學號和姓名;
2三、查詢至少學過學號爲「1」同窗所選課程中任意一門課的其餘同窗學號和姓名;
2四、查詢和「2」號同窗學習的課程徹底相同的其餘同窗的學號和姓名;
2五、刪除學習「張三」老師課的score表記錄;
2六、向score表中插入一些記錄,這些記錄要求符合如下條件:①沒有上過編號「2」課程的同窗學號;②插入「2」號課程的平均成績;
2七、按平均成績從低到高顯示全部學生的「語文」、「數學」、「英語」三門的課程成績,按以下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;
2八、查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分;
2九、按各科平均成績從低到高和及格率的百分數從高到低順序;
30、課程平均分從高到低顯示(現實任課老師);
3一、查詢各科成績前三名的記錄(不考慮成績並列狀況)
3二、查詢每門課程被選修的學生數;
3三、查詢選修了2門以上課程的所有學生的學號和姓名;
3四、查詢男生、女生的人數,按倒序排列;
3五、查詢姓「張」的學生名單;
3六、查詢同名同姓學生名單,並統計同名人數;
3七、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
3八、查詢課程名稱爲「數學」,且分數低於60的學生姓名和分數;
3九、查詢課程連接描述編號爲「3」且課程成績在80分以上的學生的學號和姓名;
40、求選修了課程的學生人數
4一、查詢選修「王五」老師所授課程的學生中,成績最高和最低的學生姓名及其成績;
4二、查詢各個課程及相應的選修人數;
4三、查詢不一樣課程但成績相同的學生的學號、課程號、學生成績;
4四、查詢每門課程成績最好的前兩名學生id和姓名;
4五、檢索至少選修兩門課程的學生學號;
4六、查詢沒有學生選修的課程的課程號和課程名;
4七、查詢沒帶過任何班級的老師id和姓名;
4八、查詢有兩門以上課程超過80分的學生id及其平均成績;
4九、檢索「3」課程分數小於60,按分數降序排列的同窗學號;
50、刪除編號爲「2」的同窗的「1」課程的成績;
5一、查詢同時選修了物理課和生物課的學生id和姓名;mysql

GITHUB

能夠從github直接下載sql文件git

建立表

# 自行建立測試數據
-- create database school charset utf8;
use `school`;
# 建立班級表
DROP TABLE IF EXISTS `class`;
create table `class`(
  `cid` int unsigned not null auto_increment ,
  `caption` varchar(50) not null unique ,
  `grade_id` int unsigned not null ,
  PRIMARY KEY(`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `class`;
# 建立學生表
DROP TABLE IF EXISTS `student`;
CREATE TABLE student(
  `sid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
  `sname` VARCHAR(50) NOT NULL ,
  `gender` ENUM('男','女') NOT NULL DEFAULT '男',
  `class_id` INT UNSIGNED NOT NULL,
  PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `student`;
# 建立老師表
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher`(
    tid INT UNSIGNED NOT NULL AUTO_INCREMENT ,
    tname VARCHAR(50) NOT NULL ,
    PRIMARY KEY (tid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `teacher`;
# 建立課程表
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course`(
    `cid` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
    `cname` VARCHAR(50) NOT NULL ,
    `teacher_id` INT UNSIGNED NOT NULL,
    PRIMARY KEY (`cid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC course;
# 建立成績表
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score`(
    `sid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `student_id` INT UNSIGNED NOT NULL,
    `course_id` INT UNSIGNED NOT NULL,
    `score` INT UNSIGNED NOT NULL DEFAULT 0,
    PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `score`;
# 建立年紀表
DROP TABLE IF EXISTS `class_grade`;
CREATE TABLE `class_grade`(
    `gid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `gname` VARCHAR(50) NOT NULL,
    PRIMARY KEY (`gid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `class_grade`;
# 班級班級任職表
DROP TABLE IF EXISTS `teach2cls`;
CREATE TABLE `teach2cls`(
    `tcid` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `tid` INT UNSIGNED NOT NULL,
    `cid` INT UNSIGNED NOT NULL,
    PRIMARY KEY (`tcid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
DESC `class_grade`;

-- 插入數據
-- 插入班級表
INSERT INTO `class`(`caption`, `grade_id`) VALUES
('一年一班', 1),
('二年一班', 2),
('三年二班', 3),
('一年二班', 1),
('二年二班', 2),
('三年一班', 3),
('四年一班', 4),
('五年一班', 5),
('六年一班', 6);


-- 插入學生表
INSERT INTO `student`(`sname`, `gender`, `class_id`) VALUES
('喬丹', '女', 1),
('艾費森','女',1),
('科比','男',2),
('亨利', '男', 2),
('alex', '女', 3),
('jenry', '女', 4),
('tom', '男', 3),
('mike', '男', 5),
('sam', '男', 8),
('den', '男', 9),
('張三', '男', 2),
('張五', '男', 4),
('jenry', '男', 1);
-- 插入老師表
INSERT INTO `teacher`(`tname`) VALUES
('張三'),
('李四'),
('王五'),
('賈六'),
('孫七');
-- 插入課程表
INSERT INTO `course`(`cname`, `teacher_id`) VALUES
('生物', 1),
('體育', 1),
('物理', 2),
('語文', 5),
('數學', 3),
('英語', 4);
-- 插入成績表
INSERT INTO `score`(`student_id`, `course_id`, `score`) VALUES
(1, 1, 60),
(1, 2, 59),
(2, 2, 99),
(2, 3, 80),
(2, 4, 50),
(3, 1, 100),
(3, 6, 89),
(4, 5, 40),
(6, 3, 70),
(10, 1, 80),
(7, 4, 20),
(11, 6, 71),
(11, 5, 100),
(13, 5, 80),
(1, 3, 70),
(2, 1, 90);
-- 插入年級表
INSERT INTO `class_grade`(`gname`) VALUES
('一年級'),
('二年級'),
('三年級'),
('四年級'),
('五年級'),
('六年級');

-- 插入班級任職表
INSERT INTO `teach2cls`(`tid`, `cid`) VALUES
(1, 1),
(1, 2),
(2, 1),
(3, 2),
(5, 1),
(4, 3),
(1, 3),
(2, 4),
(3, 6),
(5, 7),
(5, 8),
(4, 5);

50題

-- 1.查找學生總人數
SELECT COUNT(sid) as 'student_c' FROM student;

-- 二、查詢「生物」課程和「物理」課程成績都及格的學生id和姓名;
SELECT student.sid, student.sname, course.cname 
FROM (student INNER JOIN score on student.sid = score.student_id) 
INNER JOIN course on score.course_id = course.cid 
WHERE cname = '生物' or cname = '物理' and score >= 60
ORDER BY sid;
-- 三、查詢每一個年級的班級數,取出班級數最多的前三個年級;

SELECT class_grade.gname, COUNT(class.cid) AS 'class_c' 
FROM class LEFT JOIN class_grade ON class.grade_id = class_grade.gid 
GROUP BY class_grade.gid 
ORDER BY class_c DESC 
LIMIT 3;

-- 四、查詢平均成績最高和最低的學生的id和姓名以及平均成績;
SELECT student.sid, student.sname, AVG(score.score)
FROM score INNER JOIN student ON score.student_id = student.sid
GROUP BY score.student_id
HAVING AVG(score.score) IN (
    (SELECT AVG(score.score) AS 'score_a' 
    FROM student INNER JOIN score ON student.sid = score.student_id 
    GROUP BY student.sid
    ORDER BY score_a DESC
    LIMIT 1),
    (SELECT AVG(score.score) AS 'score_a' 
    FROM student INNER JOIN score ON student.sid = score.student_id 
    GROUP BY student.sid
    ORDER BY score_a
    LIMIT 1));

-- 五、查詢每一個年級的學生人數;
SELECT class_grade.gname, COUNT(student.sid) AS 'student_c'
FROM (class RIGHT JOIN class_grade ON class.grade_id = class_grade.gid) 
LEFT JOIN student ON class.cid = student.class_id
GROUP BY gname;

-- 六、查詢每位學生的學號,姓名,選課數,平均成績;
SELECT student.sid, sname, COUNT(course_id) AS 'course_c', IFNULL(AVG(score),0) AS 'score_a'
FROM student LEFT JOIN score ON student.sid = score.student_id
GROUP BY sname
ORDER BY sid;

-- 七、查詢學生編號爲「2」的學生的姓名、該學生成績最高的課程名、成績最低的課程名及分數;
SELECT course.cname, score.score
FROM course INNER JOIN score ON course.cid = score.course_id
WHERE score IN (
    (SELECT MAX(score)
    FROM score
    WHERE student_id = 2),
    (SELECT MIN(score)
    FROM score
    WHERE student_id = 2));

-- 八、查詢姓「李」的老師的個數和所帶班級數;
SELECT COUNT(DISTINCT teacher.tname) AS 'teacher_c',COUNT(teach2cls.cid) AS 'class_c'
FROM teacher LEFT JOIN teach2cls ON teacher.tid = teach2cls.tid
WHERE teacher.tname LIKE '李%';

-- 九、查詢班級數小於5的年級id和年級名;
SELECT gid, class_grade.gname
FROM class RIGHT JOIN class_grade ON class.grade_id = class_grade.gid
GROUP BY class_grade.gname
HAVING COUNT(caption) < 5
ORDER BY gid;

-- 十、查詢班級信息,包括班級id、班級名稱、年級、年級級別(12爲低年級,34爲中年級,56爲高年級),示例結果以下;
SELECT class.cid, class.caption, class_grade.gname,
    CASE 
        WHEN class_grade.gid BETWEEN 1 AND 2 THEN '低年級'
        WHEN class_grade.gid BETWEEN 3 AND 4 THEN '中年級'
        WHEN class_grade.gid BETWEEN 5 AND 6 THEN '高年級'
    END AS 'level'
    FROM class INNER JOIN class_grade ON class.grade_id = class_grade.gid;


-- 十一、查詢學過「張三」老師2門課以上的同窗的學號、姓名;
SELECT sid, sname
    FROM student
    WHERE sid IN
    (SELECT student_id
        FROM score
        WHERE course_id IN
            (SELECT cid
                FROM course
                WHERE teacher_id IN
                (SELECT tid
                    FROM teacher
                    WHERE tname = '張三'
                )
            )
        GROUP BY student_id
        HAVING COUNT(course_id) > 2);

-- 十二、查詢教授課程超過2門的老師的id和姓名;
SELECT teacher.tid, teacher.tname
    FROM teacher INNER JOIN course ON teacher.tid = course.teacher_id
    GROUP BY teacher.tid
    HAVING COUNT(course.cid) > 2;

-- 1三、查詢學過編號「1」課程和編號「2」課程的同窗的學號、姓名;
SELECT student.sid, student.sname
    FROM student INNER JOIN score ON student.sid = score.student_id
    WHERE score.course_id IN (1,2)
    GROUP BY sname
    HAVING COUNT(student.sid) = 2;

-- 1四、查詢沒有帶太高年級的老師id和姓名;
SELECT tid, tname
    FROM teacher
    WHERE tid IN
    (SELECT tid
        FROM teach2cls
        WHERE cid IN
        (SELECT cid
            FROM
            (SELECT class.cid, class.caption, class_grade.gname,
                CASE 
                    WHEN class_grade.gid BETWEEN 1 AND 2 THEN '低年級'
                    WHEN class_grade.gid BETWEEN 3 AND 4 THEN '中年級'
                    WHEN class_grade.gid BETWEEN 5 AND 6 THEN '高年級'
                END AS 'class_level'
                FROM class INNER JOIN class_grade ON class.grade_id = class_grade.gid
            ) AS t1
            WHERE t1.class_level != '高年級')
    );
-- 1五、查詢學過「張三」老師所教的全部課的同窗的學號、姓名;
SELECT sid, sname
FROM student
WHERE sid IN
    (SELECT student_id
        FROM score
        WHERE course_id IN
        (SELECT cid
            FROM teacher INNER JOIN course ON teacher.tid = course.teacher_id
            WHERE teacher.tname = '張三')
    );

-- 1六、查詢帶過超過2個班級的老師的id和姓名;
SELECT teacher.tid, teacher.tname
    FROM teacher INNER JOIN teach2cls ON teacher.tid = teach2cls.tid
    GROUP BY teacher.tid
    HAVING COUNT(cid) > 2;

-- 1七、查詢課程編號「2」的成績比課程編號「1」課程低的全部同窗的學號、姓名;
SELECT sid, sname
FROM student
WHERE sid IN    
    (SELECT t1.student_id
        FROM
        (SELECT student_id,score
                FROM score
                WHERE course_id = 2
        ) AS t1,
        (SELECT student_id,score
                FROM score
                WHERE course_id = 1
        ) AS t2
        WHERE
            t1.score < t2.score);


-- 1八、查詢所帶班級數最多的老師id和姓名;
SELECT tid, tname
FROM teacher
WHERE tid IN
(SELECT tid
    FROM teach2cls
    GROUP BY tid
    HAVING COUNT(cid) =
    (SELECT MAX(t1.count)
        FROM
        (SELECT tid, COUNT(cid) AS 'count'
            FROM teach2cls
            GROUP BY tid) AS t1));


-- 1九、查詢有課程成績小於60分的同窗的學號、姓名;
SELECT sid, sname
FROM student INNER JOIN score ON student.sid = score.student_id
WHERE score.score < 60;

-- 20、查詢沒有學全全部課的同窗的學號、姓名;
SELECT student.sid, student.sname
FROM score INNER JOIN student ON score.student_id = student.sid
GROUP BY student_id
HAVING COUNT(course_id) >=
(SELECT COUNT(cid)
FROM course);

-- 2一、查詢至少有一門課與學號爲「1」的同窗所學相同的同窗的學號和姓名;
SELECT student.sid, student.sname
FROM score INNER JOIN student ON score.student_id = student.sid
WHERE course_id IN
(SELECT course_id
FROM score
WHERE score.student_id = 1)
GROUP BY student_id

-- 2二、查詢至少學過學號爲「1」同窗所選課程中任意一門課的其餘同窗學號和姓名;
SELECT student.sid, student.sname
FROM score INNER JOIN student ON score.student_id = student.sid
WHERE course_id IN
(SELECT course_id
FROM score
WHERE score.student_id = 1)
GROUP BY student_id
HAVING student_id != 1; # 排除學號爲1的同窗

-- 2三、查詢和「2」號同窗學習的課程徹底相同的其餘同窗的學號和姓名;
SELECT student.sid, student.sname
FROM score INNER JOIN student ON score.student_id = student.sid
WHERE student_id != 2
GROUP BY student_id 
HAVING COUNT(course_id) = 
    (SELECT COUNT(course_id)
    FROM score
    WHERE student_id = 2)
-- 2四、刪除學習「張三」老師課的score表記錄;
DELETE
FROM score
WHERE course_id IN
(SELECT cid
FROM teacher INNER JOIN course ON teacher.tid = course.teacher_id
WHERE teacher.tname = '張三');

-- 2五、向score表中插入一些記錄,這些記錄要求符合如下條件:①沒有上過編號「2」課程的同窗學號;②插入「2」號課程的平均成績;
INSERT INTO score(student_id, course_id, score)
SELECT t1.sid, t1.cid, t2.avg_score
FROM 
    (SELECT student.sid, '2' AS cid
    FROM student LEFT JOIN score ON student.sid = score.student_id
    WHERE course_id != 2 OR ISNULL(course_id)) AS t1,
    (SELECT IFNULL(AVG(score), 0) as 'avg_score'
    FROM score
    WHERE course_id = '2') AS t2
ORDER BY sid

-- 2六、按平均成績從低到高顯示全部學生的「語文」、「數學」、「英語」三門的課程成績,按以下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;
SELECT
        sc.student_id,
        (select score.score from score left join course on score.course_id = course.cid where course.cname = '語文' and score.student_id = sc.student_id) as yw,
        (select score.score from score left join course on score.course_id = course.cid where course.cname = '數學' and score.student_id = sc.student_id) as sx,
        (select score.score from score left join course on score.course_id = course.cid where course.cname = '英語' and score.student_id = sc.student_id) as yy,
        COUNT(sc.course_id),
        AVG(sc.score)
FROM score AS sc
GROUP BY sc.student_id
ORDER BY avg(sc.score) ASC;

-- 2七、查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分;
SELECT course.cid, MAX(score), MIN(score)
FROM score RIGHT JOIN course ON score.course_id = course.cid
GROUP BY course_id;

-- 2八、按各科平均成績從低到高和及格率的百分數從高到低順序;
SELECT AVG(score) AS 'score_avg', CONCAT((CASE WHEN score.score > 60 THEN 1 ELSE 0 END) * 100, '%') AS 'percent'
FROM score
GROUP BY score.course_id
ORDER BY score_avg ASC, percent DESC;


-- 2九、課程平均分從高到低顯示(現實任課老師);
SELECT course.cname,teacher.tname, AVG(score.score) AS 'score_avg'
FROM (score RIGHT JOIN course ON score.course_id = course.cid)
INNER JOIN teacher ON course.teacher_id = teacher.tid
GROUP BY course_id
ORDER BY score_avg DESC;

-- 30、查詢各科成績前三名的記錄(不考慮成績並列狀況)
SELECT course.cid, course.cname, s2.first_s, s2.second_s, s2.third_s
FROM
(SELECT course_id,
(SELECT score FROM score AS s2 WHERE s1.course_id = s2.course_id ORDER BY score DESC LIMIT 0,1) AS 'first_s',
(SELECT score FROM score AS s2 WHERE s1.course_id = s2.course_id ORDER BY score DESC LIMIT 1,1) AS 'second_s',
(SELECT score FROM score AS s2 WHERE s1.course_id = s2.course_id ORDER BY score DESC LIMIT 2,1) AS 'third_s'
FROM score AS s1
GROUP BY s1.course_id) AS s2 RIGHT JOIN course ON s2.course_id = course.cid;


-- 3二、查詢每門課程被選修的學生數;
SELECT course.cid, course.cname, COUNT(score.student_id) AS 'student_c'
FROM score RIGHT JOIN course ON score.course_id = course.cid
GROUP BY course.cid;

-- 3三、查詢選修了2門以上課程的所有學生的學號和姓名;
SELECT student.sid, student.sname
FROM score RIGHT JOIN student ON score.student_id = student.sid
GROUP BY student.sid
HAVING COUNT(score.course_id) > 2;

-- 3四、查詢男生、女生的人數,按倒序排列;
SELECT gender ,COUNT(gender) AS 'gender_c'
FROM student
GROUP BY gender
ORDER BY gender_c DESC;

-- 3五、查詢姓「張」的學生名單;
SELECT student.sid, student.sname, student.gender, class.caption
FROM student LEFT JOIN class ON student.class_id = class.cid
WHERE sname LIKE '張%';

-- 3六、查詢同名同姓學生名單,並統計同名人數;
SELECT sname, count(sname) AS 'same_name_c'
FROM student
GROUP BY sname
HAVING COUNT(sname) > 1;
-- 3七、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
SELECT course.cid, course.cname, AVG(score) AS 'score_a'
FROM score RIGHT JOIN course ON score.course_id = course.cid
GROUP BY course_id
ORDER BY score_a, cid DESC;

-- 3八、查詢課程名稱爲「數學」,且分數低於60的學生姓名和分數;
SELECT student.sname, score.score
FROM (score RIGHT JOIN course ON score.course_id = course.cid)
INNER JOIN student ON score.student_id = student.sid
WHERE course.cname = '數學' AND score.score < 60;

-- 3九、查詢課程編號爲「3」且課程成績在80分以上的學生的學號和姓名;
SELECT student.sid, student.sname
FROM score INNER JOIN student ON score.student_id = student.sid
WHERE course_id = 3 AND score > 80;
-- 40、求選修了課程的學生人數
SELECT course.cid, course.cname, COUNT(student_id) AS 'student_c'
FROM score INNER JOIN course ON score.course_id = course.cid
GROUP BY course_id;
-- 4一、查詢選修「王五」老師所授課程的學生中,成績最高和最低的學生姓名及其成績;
SELECT student.sname, t2.course_id, t2.score, t2.max_score, t2.min_score
FROM student
INNER JOIN 
    (SELECT score.student_id, score.course_id, score.score, t1.max_score, t1.min_score
    FROM score,
            (SELECT course_id, MAX(score) AS 'max_score' ,MIN(score) AS 'min_score'
            FROM score
            WHERE course_id IN 
            ( SELECT cid                    
                FROM    course
                INNER JOIN teacher ON course.teacher_id = teacher.tid
                WHERE teacher.tname = '王五')
            GROUP BY course_id) as t1
            WHERE score.course_id = t1.course_id AND score.score IN (max_score, min_score)
    ) AS t2 ON student.sid = t2.student_id;

-- 4二、查詢各個課程及相應的選修人數;
SELECT course.cid, course.cname, IFNULL(COUNT(score.student_id), 0) AS 'student_c'
FROM score RIGHT JOIN course ON score.course_id = course.cid
GROUP BY course.cid;

-- 4三、查詢不一樣課時但成績相同的學生的學號、課程號、學生成績;
SELECT DISTINCT s1.student_id, s1.course_id, s1.score
FROM score as s1, score as s2
WHERE s1.student_id != s2.student_id AND s1.course_id != s2.course_id AND s1.score = s2.score;

-- 4四、查詢每門課程成績最好的前兩名學生id和姓名;
SELECT student.sid, student.sname, t2.score,t2.course_id
FROM
(SELECT score.student_id, score.course_id, score.score
FROM 
(select
        s1.course_id,
        (select s2.score from score as s2 where s1.course_id = s2.course_id order by s2.score desc limit 0,1) as first_score,
        (select s3.score from score as s3 where s1.course_id = s3.course_id order by s3.score desc limit 1,1) as second_score
from
        score as s1
GROUP BY s1.course_id) AS t1 INNER JOIN score ON t1.course_id = score.course_id
WHERE score.score IN (t1.first_score, t1.second_score)
) AS t2 INNER JOIN student ON student.sid = t2.student_id
ORDER BY t2.course_id , t2.score DESC;

-- 4五、檢索至少選修兩門課程的學生學號;
SELECT student_id
FROM score
GROUP BY student_id
HAVING COUNT(course_id) >= 2;

-- 4六、查詢沒有學生選修的課程的課程號和課程名;
SELECT course.cid, course.cname
FROM score RIGHT JOIN course ON score.course_id = course.cid
WHERE score.student_id IS NULL;

-- 4七、查詢沒帶過任何班級的老師id和姓名;
SELECT teacher.tid, teacher.tname
FROM teach2cls RIGHT JOIN teacher ON teach2cls.tid = teacher.tid
WHERE teacher.tid IS NULL;

-- 4八、查詢有兩門以上課程超過80分的學生id及其平均成績;
SELECT student_id, AVG(score)
FROM score
WHERE score.score > 80
GROUP BY student_id
HAVING COUNT(score.course_id) >= 2;

-- 4九、檢索「3」課程分數小於60,按分數降序排列的同窗學號;
SELECT student_id, score
FROM score
WHERE score.course_id = 3 AND score.score < 60
ORDER BY student_id DESC;
-- 50、刪除編號爲「2」的同窗的「1」課程的成績;
DELETE FROM score
WHERE score.sid =
    (SELECT t1.sid
    FROM 
        (SELECT sid
        FROM score AS s1
        WHERE student_id = 2 AND course_id = 1) AS t1
    );
-- 5一、查詢同時選修了物理課和生物課的學生id和姓名;
SELECT sid, sname
FROM student
WHERE sid IN 
    (SELECT student_id
    FROM score
    WHERE course_id IN 
        (SELECT cid
        FROM course
        WHERE course.cname IN ('物理','生物'))
    GROUP BY student_id
    HAVING count(course_id) = 2
    );
相關文章
相關標籤/搜索