国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

python初學——mysql練習50題

張憲坤 / 2677人閱讀

創建表
請創建如下表,并創建相關約束。如圖1

操作表中的內容
1、自行創建測試數據;
2、查詢學生總人數;
3、查詢“生物”課程和“物理”課程成績都及格的學生id和姓名;
4、查詢每個年級的班級數,取出班級數最多的前三個年級;
5、查詢平均成績最高和最低的學生的id和姓名以及平均成績;
6、查詢每個年級的學生人數;
7、查詢每位學生的學號,姓名,選課數,平均成績;
8、查詢學生編號為“2”的學生的姓名、該學生成績最高的課程名、成績最低的課程名及分數;
9、查詢姓“李”的老師的個數和所帶班級數;
10、查詢班級數小于5的年級id和年級名;
11、查詢班級信息,包括班級id、班級名稱、年級、年級級別(12為低年級,34為中年級,56為高年級),示例結果如下;

12、查詢學過“張三”老師2門課以上的同學的學號、姓名;
13、查詢教授課程超過2門的老師的id和姓名;
14、查詢學過編號“1”課程和編號“2”課程的同學的學號、姓名;
15、查詢沒有帶過高年級的老師id和姓名;
16、查詢學過“張三”老師所教的所有課的同學的學號、姓名;
17、查詢帶過超過2個班級的老師的id和姓名;
18、查詢課程編號“2”的成績比課程編號“1”課程低的所有同學的學號、姓名;
19、查詢所帶班級數最多的老師id和姓名;
20、查詢有課程成績小于60分的同學的學號、姓名;
21、查詢沒有學全所有課的同學的學號、姓名;
22、查詢至少有一門課與學號為“1”的同學所學相同的同學的學號和姓名;
23、查詢至少學過學號為“1”同學所選課程中任意一門課的其他同學學號和姓名;
24、查詢和“2”號同學學習的課程完全相同的其他同學的學號和姓名;
25、刪除學習“張三”老師課的score表記錄;
26、向score表中插入一些記錄,這些記錄要求符合以下條件:①沒有上過編號“2”課程的同學學號;②插入“2”號課程的平均成績;
27、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生ID,語文,數學,英語,有效課程數,有效平均分;
28、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分;
29、按各科平均成績從低到高和及格率的百分數從高到低順序;
30、課程平均分從高到低顯示(現實任課老師);
31、查詢各科成績前三名的記錄(不考慮成績并列情況)
32、查詢每門課程被選修的學生數;
33、查詢選修了2門以上課程的全部學生的學號和姓名;
34、查詢男生、女生的人數,按倒序排列;
35、查詢姓“張”的學生名單;
36、查詢同名同姓學生名單,并統計同名人數;
37、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
38、查詢課程名稱為“數學”,且分數低于60的學生姓名和分數;
39、查詢課程鏈接描述編號為“3”且課程成績在80分以上的學生的學號和姓名;
40、求選修了課程的學生人數
41、查詢選修“王五”老師所授課程的學生中,成績最高和最低的學生姓名及其成績;
42、查詢各個課程及相應的選修人數;
43、查詢不同課程但成績相同的學生的學號、課程號、學生成績;
44、查詢每門課程成績最好的前兩名學生id和姓名;
45、檢索至少選修兩門課程的學生學號;
46、查詢沒有學生選修的課程的課程號和課程名;
47、查詢沒帶過任何班級的老師id和姓名;
48、查詢有兩門以上課程超過80分的學生id及其平均成績;
49、檢索“3”課程分數小于60,按分數降序排列的同學學號;
50、刪除編號為“2”的同學的“1”課程的成績;
51、查詢同時選修了物理課和生物課的學生id和姓名;

GITHUB

可以從github直接下載sql文件

創建表
# 自行創建測試數據
-- 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;

-- 2、查詢“生物”課程和“物理”課程成績都及格的學生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;
-- 3、查詢每個年級的班級數,取出班級數最多的前三個年級;

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;

-- 4、查詢平均成績最高和最低的學生的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));

-- 5、查詢每個年級的學生人數;
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;

-- 6、查詢每位學生的學號,姓名,選課數,平均成績;
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;

-- 7、查詢學生編號為“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));

-- 8、查詢姓“李”的老師的個數和所帶班級數;
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 "李%";

-- 9、查詢班級數小于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;

-- 10、查詢班級信息,包括班級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;


-- 11、查詢學過“張三”老師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);

-- 12、查詢教授課程超過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;

-- 13、查詢學過編號“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;

-- 14、查詢沒有帶過高年級的老師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 != "高年級")
    );
-- 15、查詢學過“張三”老師所教的所有課的同學的學號、姓名;
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 = "張三")
    );

-- 16、查詢帶過超過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;

-- 17、查詢課程編號“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);


-- 18、查詢所帶班級數最多的老師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));


-- 19、查詢有課程成績小于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);

-- 21、查詢至少有一門課與學號為“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

-- 22、查詢至少學過學號為“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的同學

-- 23、查詢和“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)
-- 24、刪除學習“張三”老師課的score表記錄;
DELETE
FROM score
WHERE course_id IN
(SELECT cid
FROM teacher INNER JOIN course ON teacher.tid = course.teacher_id
WHERE teacher.tname = "張三");

-- 25、向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

-- 26、按平均成績從低到高顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示: 學生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;

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

-- 28、按各科平均成績從低到高和及格率的百分數從高到低順序;
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;


-- 29、課程平均分從高到低顯示(現實任課老師);
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;


-- 32、查詢每門課程被選修的學生數;
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;

-- 33、查詢選修了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;

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

-- 35、查詢姓“張”的學生名單;
SELECT student.sid, student.sname, student.gender, class.caption
FROM student LEFT JOIN class ON student.class_id = class.cid
WHERE sname LIKE "張%";

-- 36、查詢同名同姓學生名單,并統計同名人數;
SELECT sname, count(sname) AS "same_name_c"
FROM student
GROUP BY sname
HAVING COUNT(sname) > 1;
-- 37、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列;
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;

-- 38、查詢課程名稱為“數學”,且分數低于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;

-- 39、查詢課程編號為“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;
-- 41、查詢選修“王五”老師所授課程的學生中,成績最高和最低的學生姓名及其成績;
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;

-- 42、查詢各個課程及相應的選修人數;
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;

-- 43、查詢不同課時但成績相同的學生的學號、課程號、學生成績;
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;

-- 44、查詢每門課程成績最好的前兩名學生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;

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

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

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

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

-- 49、檢索“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
    );
-- 51、查詢同時選修了物理課和生物課的學生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
    );

文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。

轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/41704.html

相關文章

  • 第7期 Datawhale 組隊學習計劃

    馬上就要開始啦這次共組織15個組隊學習 涵蓋了AI領域從理論知識到動手實踐的內容 按照下面給出的最完備學習路線分類 難度系數分為低、中、高三檔 可以按照需要參加 - 學習路線 - showImg(https://segmentfault.com/img/remote/1460000019082128); showImg(https://segmentfault.com/img/remote/...

    dinfer 評論0 收藏0
  • Python

    摘要:最近看前端都展開了幾場而我大知乎最熱語言還沒有相關。有關書籍的介紹,大部分截取自是官方介紹。但從開始,標準庫為我們提供了模塊,它提供了和兩個類,實現了對和的進一步抽象,對編寫線程池進程池提供了直接的支持。 《流暢的python》閱讀筆記 《流暢的python》是一本適合python進階的書, 里面介紹的基本都是高級的python用法. 對于初學python的人來說, 基礎大概也就夠用了...

    dailybird 評論0 收藏0
  • Python基礎練習100 ( 41~ 50

    摘要:刷題繼續大家好,我又回來了,昨天和大家分享了題,今天繼續來看題解法一解法二解法一解法二解法一解法二解法一解法二解法一解法一解法一解法一解法一解法一源代碼下載這十道題的代碼在我的上,如果大家想看一下每道題的輸出結果,可以點擊以下鏈接下載題 刷題繼續 大家好,我又回來了,昨天和大家分享了31-40題,今天繼續來看41~50題 Question 41: Write a program whi...

    mochixuan 評論0 收藏0

發表評論

0條評論

最新活動
閱讀需要支付1元查看
<