LuffyCity-MySQL综合练习50实例

1、请创建如下表,并添加相应约束;

2、自行构造测试数据;

  • 新建数据库
  • 创建表
  • 构造测试数据
#Step1-创建数据库LuffyCity_MySQL;
#CREATE DATABASE LuffyCity_MySQL CHARSET utf8;
#Step2-创建数据表;
#1、创建年级表class_grade(因为class表的外键要参考class_grade表的gid字段);
CREATE TABLE class_grade (
    gid INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    gname VARCHAR (12) NOT NULL UNIQUE #年级的名称也是唯一值;指定存储引擎为InnoDB,字符编码为utf8后续不再赘述;
) ENGINE = INNODB CHARSET = utf8;

#2、创建班级表class(因为学生表的外键class_id要参考班级表的cid字段);
CREATE TABLE class (
    cid INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    caption VARCHAR (16) NOT NULL UNIQUE,
    #班级名称也为唯一值;
    grade_id INT NOT NULL,
    CONSTRAINT fk_class_grade FOREIGN KEY (grade_id) REFERENCES class_grade (gid) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = INNODB CHARSET = utf8;

#3、学生表student(与class表多对一关系);
CREATE TABLE student (
    sid INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    sname VARCHAR (12) NOT NULL,
    gender ENUM ('', '') DEFAULT '' NOT NULL,
    class_id INT NOT NULL,
    CONSTRAINT fk_class FOREIGN KEY (class_id) REFERENCES class (cid) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = INNODB CHARSET = utf8;

#4、老师表teacher(因为课程表的外键teacher_id要参考老师表的tid字段);
CREATE TABLE teacher (
    tid INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    tname VARCHAR (8) NOT NULL
) ENGINE = INNODB CHARSET = utf8;

#5、课程表course(因为成绩表的外键course_id要参考课程表的cid字段);
CREATE TABLE course (
    cid INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    cname VARCHAR (8) NOT NULL UNIQUE,
    #课程名唯一值;
    teacher_id INT NOT NULL,
    CONSTRAINT fk_teacher FOREIGN KEY (teacher_id) REFERENCES teacher (tid) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = INNODB CHARSET = utf8;

#6、成绩表(因为成绩表的外键course_id要参考课程表的cid字段);
CREATE TABLE score (
    sid INT NOT NULL UNIQUE AUTO_INCREMENT,
    student_id INT NOT NULL,
    course_id INT NOT NULL,
    score INT NOT NULL,
    CONSTRAINT fk_student FOREIGN KEY (student_id) REFERENCES student (sid) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT fk_course FOREIGN KEY (course_id) REFERENCES course (cid) ON DELETE CASCADE ON UPDATE CASCADE,
    PRIMARY KEY (student_id, course_id) #多字段联合主键;
) ENGINE = INNODB CHARSET = utf8;

#7、班级任职表teach2cls;
CREATE TABLE teach2cls (
    tcid INT NOT NULL UNIQUE AUTO_INCREMENT,
    tid INT NOT NULL,
    cid INT NOT NULL,
    CONSTRAINT fk_teacher1 FOREIGN KEY (tid) REFERENCES teacher (tid) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT fk_class1 FOREIGN KEY (cid) REFERENCES class (cid) ON DELETE CASCADE ON UPDATE CASCADE,
    PRIMARY KEY (tid, cid)
) ENGINE = INNODB CHARSET = utf8;

#Step3-插入测试数据;
#年级表;创建6个年级;
INSERT INTO class_grade (gname)
VALUES
    ('一年级'),
    ('二年级'),
    ('三年级'),
    ('四年级'),
    ('五年级'),
    ('六年级');

#班级表;每个年级指定n个班级;
INSERT INTO class (caption, grade_id)
VALUES
    ('一年级1班', 1),
    ('一年级2班', 1),
    ('一年级3班', 1),
    ('二年级1班', 2),
    ('二年级2班', 2),
    ('三年级1班', 3),
    ('四年级1班', 4),
    ('四年级2班', 4),
    ('四年级3班', 4),
    ('四年级4班', 4),
    ('五年级1班', 5),
    ('六年级1班', 6),
    ('六年级2班', 6);

#学生表;
INSERT INTO student (sname, gender, class_id)
VALUES
    ('高志粉', '', 1),
    ('李静瓶', '', 2),
    ('崔晓昭', '', 3),
    ('崔晓姗', '', 4),
    ('崔晓思', '', 5),
    ('崔青良', '', 6),
    ('崔晓磊', '', 7),
    ('高志国', '', 8),
    ('崔晓岩', '', 9),
    ('高晨曦', '', 10),
    ('陈浩', '', 11),
    ('陈浩茹', '', 10),
    ('高若曦', '', 9),
    ('武倩倩', '', 12),
    ('武若冰', '', 13);

INSERT INTO teacher (tname)
VALUES
    ('崔树齐'),
    ('宋俊泽'),
    ('孙增良'),
    ('张传伟'),
    ('邓琼');

INSERT INTO course (cname, teacher_id)
VALUES
    ('生物', 1),
    ('物理', 2),
    ('化学', 3),
    ('语文', 3),
    ('数学', 4),
    ('地理', 2),
    ('历史', 5);

INSERT INTO score (student_id, course_id, score)
VALUES
    (1, 1, 60),
    (1, 2, 59),
    (2, 4, 60),
    (2, 5, 59),
    (2, 6, 33),
    (3, 1, 59),
    (3, 5, 28),
    (4, 4, 88),
    (4, 6, 90),
    (5, 4, 88),
    (6, 5, 86),
    (6, 6, 60),
    (7, 3, 57),
    (7, 5, 60),
    (8, 2, 61),
    (8, 4, 59),
    (9, 1, 60),
    (9, 2, 61),
    (9, 3, 33),
    (10, 5, 58),
    (11, 1, 89),
    (12, 3, 70),
    (13, 2, 80),
    (14, 1, 90),
    (15, 3, 91);

INSERT INTO teach2cls (tid, cid)
VALUES
    (1, 1),
    (1, 2),
    (1, 3),
    (1, 7),
    (2, 4),
    (2, 8),
    (2, 7),
    (2, 5),
    (3, 9),
    (3, 3),
    (3, 5),
    (3, 2),
    (4, 8),
    (4, 4),
    (4, 6),
    (5, 10),
    (5, 11),
    (5, 13),
    (5, 12);

3、查询练习;

1、自行创建测试数据;

2、查询学生总人数;

SELECT
    COUNT(cid) AS '班级总人数'
FROM
    class;

3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;

/*
3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;
解题分析:
1、成绩及格,即score >=60;
2、成绩表的course_id 和 课程表的cid建立内连接查询,条件是course.cname 包含('生物','物理')且 成绩大于60;
3、将查询到的结果,作为student表的子查询;
*/
SELECT
    sid AS '学生id',
    sname AS '姓名'
FROM
    student
WHERE
    sid IN (
        SELECT
            score.student_id
        FROM
            score
        INNER JOIN course ON score.course_id = course.cid
        WHERE
            course.cname IN ('生物', '物理')
        AND score.score >= 60
    );

4、查询每个年级的班级数,取出班级数最多的前三个年级;

/*
4、查询每个年级的班级数,取出班级数最多的前三个年级;
解题分析:
1、年级名称及数量,从class_grade表和class表进行联合查询;
2、班级表的grade_id字段与年级表的gid字段,通过内连接关联查询;
3、通过聚合函数count计数caption,来统计班级数;
*/
SELECT
    class_grade.gname,
    COUNT(caption)
FROM
    class
INNER JOIN class_grade ON class.grade_id = class_grade.gid
GROUP BY
    grade_id
ORDER BY
    COUNT(caption) DESC
LIMIT 3;

5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;

/*
5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
解题分析:
1、成绩表中查询score;
2、通过avg成绩升级排序,以及limit1 组合,得到最高和最低的成绩;
3、最高与最低成绩的union去重后,作为学生表的子查询条件;
*/
SELECT
    t3.student_id AS "学号",
    t4.sname AS "姓名",
    t3.avg_score AS "平均成绩"
FROM
    (
        SELECT
            *
        FROM
            (
                SELECT
                    student_id,
                    avg(score) AS avg_score
                FROM
                    score
                GROUP BY
                    student_id
                ORDER BY
                    avg(score) DESC
                LIMIT 1
            ) AS t1
        UNION
            SELECT
                *
            FROM
                (
                    SELECT
                        student_id,
                        avg(score)
                    FROM
                        score
                    GROUP BY
                        student_id
                    ORDER BY
                        avg(score) ASC
                    LIMIT 1
                ) AS t2
    ) AS t3
INNER JOIN student AS t4 ON t3.student_id = t4.sid;

6、查询每个年级的学生人数;

/*
6、查询每个年级的学生人数;
解题分析:
1、学生表与班级表进行左连接;
2、班级表与年级表通过inner join链接;
3、聚合函数count计数sid来统计学生数;
4、需要起多个别名来进行组合查询;
*/
SELECT
    gname AS '年级',
    num AS '总人数'
FROM
    (
        SELECT
            class.grade_id AS cg_id,
            COUNT(sid) AS num
        FROM
            student
        INNER JOIN class ON student.class_id = class.cid
        GROUP BY
            class.grade_id
    ) AS t2
INNER JOIN class_grade ON t2.cg_id = class_grade.gid
ORDER BY
    num DESC;

7、查询每位学生的学号,姓名,选课数,平均成绩;

/*
7、查询每位学生的学号,姓名,选课数,平均成绩;
解题分析:
1、选课数来源于score表,而不是course表;
2、学号及sid,姓名即sname,选课数即count(course_id),平均成绩即avg(score)
*/
SELECT
    student.sid AS '学号',
    student.sname AS '姓名',
    count(score.course_id) AS '选课数',
    avg(score.score) AS '平均成绩'
FROM
    student
INNER JOIN score ON student.sid = score.student_id
GROUP BY
    student.sid;

8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;

/*
8、查询学生编号为“2”的学生的姓名、
该学生成绩最高的课程名、
成绩最低的课程名及分数;
解题分析:
1、max(score)取得最高成绩,min(score)取得最低成绩
2、
*/
SELECT
    student.sname,
    course.cname,
    t1.score
FROM
    (
        SELECT
            student_id,
            course_id,
            score
        FROM
            score
        WHERE
            student_id = 2
        AND score IN (
            (
                SELECT
                    min(score)
                FROM
                    score
                WHERE
                    student_id = 2
            ),
            (
                SELECT
                    max(score)
                FROM
                    score
                WHERE
                    student_id = 2
            )
        )
    ) AS t1
INNER JOIN student ON t1.student_id = student.sid
INNER JOIN course ON t1.course_id = course.cid;

9、查询姓“李”的老师的个数和所带班级数;

/*
9、查询姓“李”的老师的个数和所带班级数;
解题分析:
1、teacher表、teach2cls表连接查询;
2、like模糊匹配;
*/
SELECT
    teacher.tid,
    teacher.tname,
    t1.count_cid
FROM
    teacher
LEFT JOIN (
    SELECT
        tid,
        count(cid) AS count_cid
    FROM
        teach2cls
    WHERE
        tid IN (
            SELECT
                tid
            FROM
                teacher
            WHERE
                tname LIKE '张%'
        )
    GROUP BY
        tid
) AS t1 ON teacher.tid = t1.tid
WHERE
    teacher.tname LIKE '张%';

10、查询班级数小于5的年级id和年级名;

/*
10、查询班级数小于5的年级id和年级名;
解题分析:
1、从class_grade以及teach2cls中联合查询;
*/
#方案1;
SELECT
    class_grade.gid AS '年级id',
    class_grade.gname AS '年级名'
FROM
    class_grade
INNER JOIN class ON class_grade.gid = class.grade_id
GROUP BY
    class_grade.gid
HAVING
    COUNT(caption) < 5;
#方案2;
SELECT
    gid,
    gname
FROM
    class_grade
WHERE
    gid IN (
        SELECT
            grade_id
        FROM
            class
        GROUP BY
            grade_id
        HAVING
            count(caption) < 5
    );

11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;

/*
11、查询班级信息,包括班级id、班级名称、年级、
年级级别(12为低年级,34为中年级,56为高年级),
示例结果如下;
解题分析:
1、从class和class_grade表中使用笛卡尔积联合查询;
2、case when ...then else 0 end as 'xxx'条件语句;
*/
SELECT
    class.cid AS '班级id',
    class.caption AS '班级名称',
    class_grade.gname AS '年级',
    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
    '高年级'
ELSE
    0
END AS '年级级别'
FROM
    class,
    class_grade
WHERE
    class.grade_id = class_grade.gid;

 

12、查询学过“张三”老师2门课以上的同学的学号、姓名;

/*
12、查询学过“张三”老师2门课以上的同学的学号、姓名;
解题分析:
1、先拿到teacher表的tid;
2、再通过tid作为子查询条件,拿到course表的cid;
3、通过cid作为子查询条件,拿到student表的sid;
4、最后通过sid确定student表的sid,sname并起个别名;
*/
SELECT
    sid AS '学生学号',
    sname AS '姓名'
FROM
    student
WHERE
    sid IN (
        SELECT
            student_id
        FROM
            score
        WHERE
            course_id IN (
                SELECT
                    cid
                FROM
                    course
                WHERE
                    teacher_id = (
                        SELECT
                            tid
                        FROM
                            teacher
                        WHERE
                            tname LIKE '宋俊泽'
                    )
                GROUP BY
                    teacher_id
                HAVING
                    count(cname) > 2
            )
    );
/*
12、查询学过“张三”老师2门课以上的同学的学号、姓名;
解题分析:
1、先拿到teacher表的tid;
2、再通过tid作为子查询条件,拿到course表的cid;
3、通过cid与score表的student_id内连接查询;
*/
SELECT
    sid AS '学生学号',
    sname AS '姓名'
FROM
    student
WHERE
    sid IN (
        SELECT
            student_id
        FROM
            score
        WHERE
            course_id IN (
                SELECT
                    course.cid
                FROM
                    teacher
                INNER JOIN course ON teacher.tid = course.teacher_id
                WHERE
                    teacher.tname = '宋俊泽'
            )
        GROUP BY
            student_id
        HAVING
            count(course_id) > 2
    );

13、查询教授课程超过2门的老师的id和姓名;

/*
13、查询教授课程超过2门的老师的id和姓名;;
解题分析:
1、先通过查询teacher表拿到teacher_id;
2、再将teacher_id作为teacher表的子查询条件;
*/
SELECT
    tid AS '老师id',
    tname AS '姓名'
FROM
    teacher
WHERE
    tid IN (
        SELECT
            teacher_id
        FROM
            course
        GROUP BY
            teacher_id
        HAVING
            count(cid) > 2
    );

14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;

/*
14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
解题分析:
1、先通过score表的course_id即1和2,查询到score表的student_id,注意使用distinct关键字进行去重;
2、再通过student_id,作为student表的子查询条件,查询出学号和姓名;
*/
SELECT
    sid AS '学生id',
    sname AS '姓名'
FROM
    student
WHERE
    sid IN (
        SELECT DISTINCT
            student_id
        FROM
            score
        WHERE
            course_id IN (1, 2)
    );

15、查询没有带过高年级的老师id和姓名;

 

/*
15、查询没有带过高年级的老师id和姓名;
解题分析:
1、翻译该话:高年级即5、6年级,但程序不能区分呀,必须通过查询条件指定(参考11小题
查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果如下;);
2、不在5、6年级,即not in ;
3、通过class表的grade_id拿到teach2cls表的tid,
4、通过tid作为teacher表的子查询条件;
*/
SELECT
    tid,
    tname
FROM
    teacher
WHERE
    tid NOT IN (
        SELECT
            tid
        FROM
            teach2cls
        WHERE
            cid IN (
                SELECT
                    t1.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
                            ''
                        ELSE
                            0
                        END AS grade_layer
                        FROM
                            class,
                            class_grade
                        WHERE
                            class.grade_id = class_grade.gid
                    ) AS t1
                WHERE
                    t1.grade_layer = ''
            )
    );

16、查询学过“崔树齐”老师所教的所有课的同学的学号、姓名;

/*
16、查询学过“崔树齐”老师所教的所有课的同学的学号、姓名;
解题分析:
1、通过张三老师的姓名拿到teacher表的tid;
2、通过tid拿到course表的cid;
3、通过cid拿到score表的student_id;
4、通过student_id作为student表的子查询条件;
*/
SELECT
    sid AS '学生id',
    sname AS '姓名'
FROM
    student
WHERE
    sid IN (
        SELECT
            student_id
        FROM
            score
        WHERE
            course_id = (
                SELECT
                    cid
                FROM
                    course
                WHERE
                    teacher_id = (
                        SELECT
                            tid
                        FROM
                            teacher
                        WHERE
                            tname = '崔树齐'
                    )
            )
    );

17、查询带过超过2个班级的老师的id和姓名;

/*
17、查询带过超过2个班级的老师的id和姓名;
解题分析:
1、通过teach2cls表,通过统计cid,拿到tid;
2、将tid作为teacher表的子查询条件;
*/
SELECT
    tid AS '老师id',
    tname AS '姓名'
FROM
    teacher
WHERE
    tid IN (
        SELECT
            tid
        FROM
            teach2cls
        GROUP BY
            tid
        HAVING
            count(cid) > 2
    );

18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;

/*
18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
解题分析:
1、通过编号1、2分别查询到student_id和成绩score作为虚拟表t1,t2;
2、通过比较t1和t2表的score,拿到t1或t2的student_id;
3、将student_id作为student表的子查询条件;
*/
SELECT
    sid,
    sname
FROM
    student
WHERE
    sid IN (
        SELECT
            t1.student_id
        FROM
            (
                SELECT
                    student_id,
                    score
                FROM
                    score
                WHERE
                    course_id = 2
                GROUP BY
                    student_id
            ) AS t1,
            (
                SELECT
                    student_id,
                    score
                FROM
                    score
                WHERE
                    course_id = 1
                GROUP BY
                    student_id
            ) AS t2
        WHERE
            t1.student_id = t2.student_id
        AND t1.score < t2.score
    );

19、查询所带班级数最多的老师id和姓名;

 

/*
19、查询所带班级数最多的老师id和姓名;
解题分析:
1、通过聚合函数count统计出班级数最多的tid;
2、将tid作为teacher表的子查询条件;
*/
SELECT
    tid,
    tname
FROM
    teacher
WHERE
    tid IN (
        SELECT
            tid
        FROM
            teach2cls
        GROUP BY
            tid
        HAVING
            count(cid) = (
                SELECT
                    count(cid)
                FROM
                    teach2cls
                GROUP BY
                    tid
                ORDER BY
                    count(cid) DESC
                LIMIT 1
            )
    );

20、查询有课程成绩小于60分的同学的学号、姓名;

/*
20、查询有课程成绩小于60分的同学的学号、姓名;
解题分析:
1、通过score表,查询出student_id,单要使用distinct语句去重;
2、将student_id作为student表的子查询条件;
*/
SELECT
    sid,
    sname
FROM
    student
WHERE
    sid IN (
        SELECT DISTINCT
            student_id
        FROM
            score
        WHERE
            score < 60
    );

21、查询没有学全所有课的同学的学号、姓名;

/*
21、查询没有学全所有课的同学的学号、姓名;
解题分析:
1、查询学完所有课程的学生id,添加not语句;
2、
*/
SELECT
    sid,
    sname
FROM
    student
WHERE
    sid IN (
        SELECT DISTINCT
            student_id
        FROM
            score
        WHERE
            course_id IN (SELECT cid FROM course)
    );

22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;

/*
22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;
解题分析:
1、查询学号为1的同学的student_id拿到course_id;
2、通过course_id in 学号为1的同学的course_id范围,可拿到至少有一门课与学号为1的同学的sid;
3、将sid 作为student 表的子查询条件;
*/
SELECT
    sid,
    sname
FROM
    student
WHERE
    sid IN (
        SELECT
            student_id
        FROM
            score
        WHERE
            course_id IN (
                SELECT
                    course_id
                FROM
                    score
                WHERE
                    student_id = 1
            )
        GROUP BY
            student_id
    );

23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;

/*
23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;
解题分析:
1、查询学号为1的同学的student_id拿到course_id;
2、通过course_id in 学号为1的同学的course_id范围,可拿到至少有一门课与学号为1的同学的sid;
3、将sid 作为student 表的子查询条件;
4、最后将查询结果通过sid !=1 进行去除学号为1的同学的查询结果;
*/
SELECT
    sid,
    sname
FROM
    student
WHERE
    sid IN (
        SELECT
            student_id
        FROM
            score
        WHERE
            course_id IN (
                SELECT
                    course_id
                FROM
                    score
                WHERE
                    student_id = 1
            )
        GROUP BY
            student_id
    )
AND sid != 1;

24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;

/*
24、查询和“2”号同学学习的课程完全相同的其他同学的学号和姓名;
解题分析:
1、
*/
SELECT
    sid,
    sname
FROM
    student
WHERE
    sid IN (
        SELECT DISTINCT
            student_id
        FROM
            score
        WHERE
            student_id IN (
                SELECT
                    course_id
                FROM
                    score
                WHERE
                    student_id = 2
            )
    )
AND sid != 2;

25、删除学习“张三”老师课的score表记录;

/*
25、删除学习“张三”老师课的score表记录;
解题分析:
1、先查询出'张三'老师的cid;
2、将cid作为score表的子查询条件;
2、将select 改为delete即可;
*/
DELETE
FROM
    score
WHERE
    course_id IN (
        SELECT
            course.cid
        FROM
            course
        INNER JOIN teacher ON course.teacher_id = teacher.tid
        WHERE teacher.tname = '张三'
    );

26、向score表中插入一些记录,这些记录要求符合以下条件:①没有上过编号“2”课程的同学学号;②插入“2”号课程的平均成绩;

/*
26、向score表中插入一些记录,这些记录要求符合以下条件:
①没有上过编号“2”课程的同学学号;
②插入“2”号课程的平均成绩;
解题分析:
1、先将编号为2的课程的student_id查询出来,使用not取反;
2、将课程号为2的平均成绩查询出来;
3、使用insert语句进行查询插入操作;
*/
INSERT INTO score (student_id, course_id, score) SELECT
    t1.sid,
    2,
    t2.avg_score
FROM
    (
        SELECT
            sid
        FROM
            student
        WHERE
            sid NOT IN (
                SELECT
                    student_id
                FROM
                    score
                WHERE
                    course_id = 2
            )
    ) AS t1,
    (
        SELECT
            avg(score) AS avg_score
        FROM
            score
        WHERE
            course_id = 2
    ) AS t2;

27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,数学,英语,有效课程数,有效平均分;

xxxx

28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;

/*
28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
解题分析:
1、课程表与成绩表内连接;
*/
SELECT
    course.cid AS 课程ID,
    max(score.score) AS 最高分,
    min(score.score) AS 最低分
FROM
    course
LEFT JOIN score ON course.cid = score.course_id
GROUP BY
    score.course_id;

29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;

/*
29、按各科平均成绩从低到高和及格率的百分数从高到低顺序;
解题分析:
1、课程表与成绩表内连接;
*/
SELECT
    course_id,
    avg(score) AS avg_score,
    sum(
        CASE
        WHEN score.score > 60 THEN
            1
        ELSE
            0
        END
    ) / count(score.score) * 100 AS 及格率
FROM
    score
GROUP BY
    course_id
ORDER BY
    avg(score) DESC,
    及格率 ASC;

30、课程平均分从高到低显示(显示任课老师);

/*
30、课程平均分从高到低显示(现实任课老师);
解题分析:

*/
SELECT
    t1.avg_score,
    teacher.tname
FROM
    course,
    teacher,
    (
        SELECT
            course_id,
            avg(score) AS avg_score
        FROM
            score
        GROUP BY
            course_id
        ORDER BY
            avg_score DESC
    ) AS t1
WHERE
    course.cid = t1.course_id
AND course.teacher_id = teacher.tid
ORDER BY
    t1.avg_score DESC;

31、查询各科成绩前三名的记录(不考虑成绩并列情况);

/*
31、查询各科成绩前三名的记录(不考虑成绩并列情况);
解题分析:

*/
select score.score,course.cname from score INNER JOIN course 
on score.course_id = course.cid
GROUP BY score.course_id
HAVING MAX(score.score)
ORDER BY score DESC
limit 3;

32、查询每门课程被选修的学生数;
33、查询选修了2门以上课程的全部学生的学号和姓名;

/*
33、查询选修了2门以上课程的全部学生的学号和姓名;
解题分析:
1、通过聚合函数统计course_id的数量,拿到student_id;
2、将student_id作为student表的子查询条件;
*/
SELECT
    sid,
    sname
FROM
    student
WHERE
    sid IN (
        SELECT
            student_id
        FROM
            score
        GROUP BY
            student_id
        HAVING
            count(course_id) > 2
    );

34、查询男生、女生的人数,按倒序排列;

/*
34、查询男生、女生的人数,按倒序排列;
解题分析:
1、通过聚合函数统计course_id的数量,拿到student_id;
2、将student_id作为student表的子查询条件;
*/
SELECT
    COUNT(sid) AS '性别人数',
    gender
FROM
    student
GROUP BY
    gender
ORDER BY
    '性别人数' DESC;

35、查询姓“张”的学生名单;

/*
35、查询姓“张”的学生名单;
解题分析:
1、学生名单及学生以及学生的所有相关信息;
2、学生表包含学生信息,班级表也包含学生信息;
*/
SELECT
    student.*, class.caption
FROM
    student
INNER JOIN class ON student.class_id = class.cid
WHERE
    student.sname LIKE '崔%';

36、查询同名同姓学生名单,并统计同名人数;
37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;

/*
37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
解题分析:
1、先排序平均成绩;在排序课程号;
*/
SELECT
    course_id,
    avg(score) AS avg_score
FROM
    score
GROUP BY
    course_id
ORDER BY
    avg_score ASC,
    course_id DESC;

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和姓名;

 

posted @ 2018-07-29 23:32  天晴天朗  阅读(1294)  评论(0编辑  收藏  举报