登录
首页 >  数据库 >  MySQL

python初学——mysql练习50题

来源:SegmentFault

时间:2023-01-12 16:11:18 483浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个数据库开发实战,手把手教大家学习《python初学——mysql练习50题》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

创建表
请创建如下表,并创建相关约束。如图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)  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 =
(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  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 

到这里,我们也就讲完了《python初学——mysql练习50题》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于mysql的知识点!

声明:本文转载于:SegmentFault 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表