MYSQL 进阶

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、成绩表的course_id 和 课程表的cid建立内连接查询,条件是course.cname 包含('生物','物理')且 score.score大于60;
2、将查询到的结果,作为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表进行联合查询left join后group by分年纪;再order by count()即可;
2、班级表的grade_id字段与年级表的gid字段,通过内连接关联查询;
3、group by后通过聚合函数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、group by sid;order by avg(score) desc limit 1取最高和最低;union合并后;inner join取名字;
解题分析:
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、inner join班级表和学生表并group by年级count;再inner join年级表取年级名称;
解题分析:
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、学生表inner join成绩表group by sid;count(cid);avg(score)
解题分析:
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、id=2;score表拿到所属的课程;取最高最低方法1:排序limit 1直接拿cid;方法2:max min再where查cid;
解题分析:
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;
复制代码
select * from 
(select sid,sname from student where sid=2) as t5,
(select course.cname,score from (select * from (select course_id,score from score where student_id=2 order by score desc limit 1) as t1 
union 
select * from (select course_id,score from score where student_id=2 order by score asc limit 1 ) as t2)
as t3
inner join course on course.cid = t3.course_id) as t4 

9、查询所有姓“李”的老师和所带班级数,可为空;

复制代码
/*
9、name like "李%"拿到tid;再where in tid group by;inner join拿到name
解题分析:
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、inner join;group by gid;或者先group by拿到id再where
解题分析:
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
    );
复制代码
查询班级数小于5的年级id和年级名、年级数;
select gid,gname,num from class_grade 
left join
(select grade_id,count(caption) as num from class group by grade_id ) as t1
on class_grade.gid = t1.grade_id where num<4

 

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

复制代码
/*
11、case when xx between and then when...else ... end as "";
解题分析:
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、根据tid拿到所属的课程;再group by sid having count()
解题分析:
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
    );
复制代码
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 like "宋俊泽")) 
   group by student_id having count(course_id)>=2)

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

复制代码
/*
13、group by having count;
解题分析:
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、where;
解题分析:
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、没带过高年级=not in 带过的tid;
解题分析:
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 = '高'
            )
    );
复制代码
select * from teacher where tid not in (
select DISTINCT tid from teach2cls where cid in (
select cid from class where grade_id in(
select gid from (select gid,
case 
when gid between 1 and 2 then "low" 
when gid between 3 and 4 then "mid" 
when gid between 5 and 6 then "high" 
end as "level"
from class_grade) as t1
where level="high")))

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

复制代码
/*
16、where;
解题分析:
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、group by having count;
解题分析:
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、1和2的score表作inner join on where <拿到sid;
解题分析:
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、group by;子查询不能用 limit,嵌套多一层
解题分析:
1、通过聚合函数count统计出班级数最多的tid;
2、将tid作为teacher表的子查询条件;
*/
SELECT
    tid,
    tname
FROM
    teacher
WHERE
    tid IN (
        SELECT
            tid
        FROM
        ( SELECT count(cid) FROM teach2cls GROUP BY tid ORDER BY count(cid) DESC LIMIT 1 ) );
复制代码

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

复制代码
/*
20、distinct;where score<
解题分析:
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、学全所有课=count(cid)=7;
解题分析:
1、查询学完所有课程的学生id,添加not语句;
2、
*/

select * from student where sid in (
  select student_id from score group by student_id having count(course_id) = (
    select count(cid) from course))

复制代码
注:exists用法:报错
select * from student as t1 where EXISTS(
select course_id from (
select course_id from score where t1.sid=score.student_id) as t
where count(course_id)<
(select count(cid) from course))

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

复制代码
/*
22、where course_id in ();
解题分析:
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
            DISTINCT student_id
        FROM
            score
        WHERE
            course_id IN (
                SELECT
                    course_id
                FROM
                    score
                WHERE
                    student_id = 1
            )
        GROUP BY
            student_id
    );
复制代码

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

复制代码
/*
23、多一个条件and;
解题分析:
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号同学的所有课程id与其他每一个同学的课程id比较;
解题分析:
1、
*/

select sid from student as t where EXISTS(
select * from (select course_id from score where student_id=t.sid)as t1,
(select course_id from score where student_id=2) as t2 where t1.course_id !=t2.course_id
)

复制代码

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

复制代码
/*
25、tid;course;delete from score where ;
解题分析:
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、insert score () ();
解题分析:
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,语文,数学,英语,有效课程数,有效平均分;

select sid,sname,yuwen,shuxue,score as yingyu from
(select sid,sname,yuwen,score as shuxue from
(select sid,sname,score as yuwen from student left join

(select student_id,score from score where course_id =
(select cid from course where cname = "语文")) as t1 on student_id=sid) as t2 left join

(select student_id,score from score where course_id =
(select cid from course where cname ="数学")) as t3 on student_id=sid) as t4 left join

(select student_id,score from score where course_id =
(select cid from course where cname ="英语")) as t5 on student_id=sid

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

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

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

复制代码
/*
29、sum(case when score> then else end);
解题分析:
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、先把score表group by再三个表一起查询;
解题分析:

*/
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.sid,score.course_id,score.score,T.first_num,T.second_num from score left join
(select sid,
(select score from score as s2 where s2.course_id = s1.course_id order by score desc limit 0,1) as first_num,
(select score from score as s2 where s2.course_id = s1.course_id order by score desc limit 3,1) as second_num
from score as s1) as T
on score.sid =T.sid
where score.score <= T.first_num and score.score >= T.second_num
order by course_id

复制代码

32、查询每门课程被选修的学生数;

select course_id,course.cname,count(student_id) from course inner join score on score.course_id=course.cid group by course_id order by cid

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

复制代码
/*
33、having count;
解题分析:
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、group by;
解题分析:
1、通过聚合函数统计course_id的数量,拿到student_id;
2、将student_id作为student表的子查询条件;
*/
SELECT
    COUNT(sid) AS '性别人数',
    gender
FROM
    student
GROUP BY
    gender
ORDER BY
    '性别人数' DESC;
复制代码

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

复制代码
/*
35、name like "";
解题分析:
1、学生名单及学生以及学生的所有相关信息;
2、学生表包含学生信息,班级表也包含学生信息;
*/
SELECT
    student.*, class.caption
FROM
    student
INNER JOIN class ON student.class_id = class.cid
WHERE
    student.sname LIKE '崔%';
复制代码

36、查询同名同姓学生名单,并统计同名人数;

select sname,count(1) as count from student group by sname;

37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;

复制代码
/*
37、group by;
解题分析:
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的学生姓名和分数;

select sname,score from score inner join student on student.sid = score.student_id where course_id in (
select cid from course where cname="数学") 
and score <60

39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;

select sname,score from score inner join student on student.sid = score.student_id where course_id in (
select cid from course where cid=3) 
and score >80

40、求选修了课程的学生人数;

select count(distinct student_id) from score

41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;

select * from student where sid in (select * from 
(select student_id from score where course_id in (
select cid from course where teacher_id in(
select tid from teacher where tname = "孙增良" ))
order by score desc limit 1) as t)

or sid in (select * from  
(select student_id from score where course_id in (
select cid from course where teacher_id in(
select tid from teacher where tname = "孙增良" ))
order by score asc limit 1) 
as t2 )

42、查询各个课程及相应的选修人数;

select cname,count(1) from score inner join course on cid=course_id group by course_id

43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;

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

44、查询每门课程成绩最好的前两名学生id和姓名;

select score.sid,score.course_id,score.score,T.first_num,T.second_num from score left join

    (

    select

        sid,

        (select score from score as s2 where s2.course_id = s1.course_id order by score desc limit 0,1) as first_num,

        (select score from score as s2 where s2.course_id = s1.course_id order by score desc limit 1,1) as second_num

    from

        score as s1

    ) as T

    on score.sid =T.sid

    where score.score <= T.first_num and score.score >= T.second_num

45、检索至少选修两门课程的学生学号;

select student_id from score group by student_id having count(student_id) > 1

46、查询没有学生选修的课程的课程号和课程名;

select course_id,cname from score inner join course on cid = course_id group by course_id having count(1) = 0

47、查询没带过任何班级的老师id和姓名;

select * from teacher where tid not in (
select tid from teach2cls GROUP BY tid)

48、查询有两门以上课程超过80分的学生id及其平均成绩;

select student.sid,avg(score.score) from student inner join score on score.student_id = student.sid where student.sid in (
select student_id from score where score>80 group by student_id having count(1)>=2 ) group by student.sid

49、检索“3”课程分数小于60,按分数降序排列的同学学号;

select student_id from score where course_id=3 ORDER BY score desc

50、删除编号为“2”的同学的“1”课程的成绩;

delete from score where course_id=1 and student_id=2 

51、查询同时选修了物理课和生物课的学生id和姓名;

select student_id,sname from score inner join student on student.sid = score.student_id where course_id in ( 
select cid from course where cname in ("物理","生物")) GROUP BY student_id

 

posted @ 2018-09-20 03:00  心平万物顺  阅读(112)  评论(0编辑  收藏  举报