您当前的位置:首页 > 计算机 > 软件应用 > 数据库 > MySQL

MySQL查询50例

时间:09-14来源:作者:点击数:

创建表和关系

/*
创建表
*/

/*年级表*/
DROP TABLE IF EXISTS `class_grade`;
CREATE TABLE `class_grade` (
  `gid` int(11) NOT NULL AUTO_INCREMENT,
  `gname` varchar(32) NOT NULL,
  PRIMARY KEY (`gid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


/*班级表*/
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `caption` varchar(32) NOT NULL,
  `grade_id` int(11) NOT NULL,
  PRIMARY KEY (`cid`),
  KEY `fk_class_grade` (`grade_id`),
  CONSTRAINT `fk_class_grade` FOREIGN KEY (`grade_id`) REFERENCES `class_grade` (`gid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

/*老师表*/
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
  `tid` int(11) NOT NULL AUTO_INCREMENT,
  `tname` varchar(32) NOT NULL,
  PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;



/*课程表*/
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `cname` varchar(32) NOT NULL,
  `teacher_id` int(11) NOT NULL,
  PRIMARY KEY (`cid`),
  KEY `fk_course_teacher` (`teacher_id`),
  CONSTRAINT `fk_course_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

/*学生表*/
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `gender` char(1) NOT NULL,
  `class_id` int(11) NOT NULL,
  `sname` varchar(32) NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `fk_class` (`class_id`),
  CONSTRAINT `fk_class` FOREIGN KEY (`class_id`) REFERENCES `class` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;


/*成绩表*/
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `student_id` int(11) NOT NULL,
  `course_id` int(11) NOT NULL,
  `score` int(11) NOT NULL,
  PRIMARY KEY (`sid`),
  KEY `fk_score_student` (`student_id`),
  KEY `fk_score_course` (`course_id`),
  CONSTRAINT `fk_score_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`cid`),
  CONSTRAINT `fk_score_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;



/*班级任职表:*/
DROP TABLE IF EXISTS `teach2cls`;
CREATE TABLE `teach2cls`(
  `tcid` int(11) NOT NULL AUTO_INCREMENT,
  `tid` int(11) NOT NULL,
  `cid` int(11) NOT NULL,
  PRIMARY KEY (`tcid`),
  KEY `fk_teach2cls_class` (`cid`),
  KEY `fk_teach2cls_teacher` (`tid`),
  CONSTRAINT `fk_teach2cls_class` FOREIGN KEY (`cid`) REFERENCES `class` (`cid`),
  CONSTRAINT `fk_teach2cls_teacher` FOREIGN KEY (`tid`) REFERENCES `teacher` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;



/*
验证创建表和数据
*/

mysql> use db4;
Database changed
mysql> desc class;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| cid      | int(11)     | NO   | PRI | NULL    | auto_increment |
| caption  | varchar(32) | NO   |     | NULL    |                |
| grade_id | int(11)     | NO   | MUL | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> select * from class;
+-----+--------------+----------+
| cid | caption      | grade_id |
+-----+--------------+----------+
|   1 | 一年一班     |        1 |
|   2 | 二年一班     |        2 |
|   3 | 三年二班     |        3 |
|   4 | 二年二班     |        2 |
+-----+--------------+----------+
4 rows in set (0.00 sec)

mysql>
mysql> desc student;
+----------+-------------+------+-----+---------+----------------+
| Field    | Type        | Null | Key | Default | Extra          |
+----------+-------------+------+-----+---------+----------------+
| sid      | int(11)     | NO   | PRI | NULL    | auto_increment |
| gender   | char(1)     | NO   |     | NULL    |                |
| class_id | int(11)     | NO   | MUL | NULL    |                |
| sname    | varchar(32) | NO   |     | NULL    |                |
+----------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> select * from student limit 5;
+-----+--------+----------+-----------+
| sid | gender | class_id | sname     |
+-----+--------+----------+-----------+
|   1 | 女     |        1 | 乔丹      |
|   2 | 女     |        1 | 艾弗森    |
|   3 | 男     |        2 | 科比      |
|   4 | 男     |        1 | 张一      |
|   5 | 女     |        1 | 张二      |
+-----+--------+----------+-----------+
5 rows in set (0.00 sec)

mysql> desc teacher;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| tid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| tname | varchar(32) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

mysql> select * from teacher;
+-----+-----------+
| tid | tname     |
+-----+-----------+
|   1 | 张三      |
|   2 | 李四      |
|   3 | 王五      |
|   4 | 朱云海    |
|   5 | 李杰      |
+-----+-----------+
5 rows in set (0.00 sec)

mysql> desc course;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| cid        | int(11)     | NO   | PRI | NULL    | auto_increment |
| cname      | varchar(32) | NO   |     | NULL    |                |
| teacher_id | int(11)     | NO   | MUL | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> select * from course;
+-----+--------+------------+
| cid | cname  | teacher_id |
+-----+--------+------------+
|   1 | 生物   |          1 |
|   2 | 体育   |          1 |
|   3 | 物理   |          2 |
|   4 | 美术   |          2 |
+-----+--------+------------+
4 rows in set (0.00 sec)

mysql> desc score;
+------------+---------+------+-----+---------+----------------+
| Field      | Type    | Null | Key | Default | Extra          |
+------------+---------+------+-----+---------+----------------+
| sid        | int(11) | NO   | PRI | NULL    | auto_increment |
| student_id | int(11) | NO   | MUL | NULL    |                |
| course_id  | int(11) | NO   | MUL | NULL    |                |
| score      | int(11) | NO   |     | NULL    |                |
+------------+---------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

mysql> select * from score limit 5;
+-----+------------+-----------+-------+
| sid | student_id | course_id | score |
+-----+------------+-----------+-------+
|   1 |          1 |         1 |    60 |
|   2 |          1 |         2 |    59 |
|   3 |          2 |         2 |    99 |
|   6 |          2 |         1 |     8 |
|   8 |          2 |         3 |    68 |
+-----+------------+-----------+-------+
5 rows in set (0.00 sec)

mysql> desc class_grade;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| gid   | int(11)     | NO   | PRI | NULL    | auto_increment |
| gname | varchar(32) | NO   |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)

mysql> select * from class_grade;
+-----+-----------+
| gid | gname     |
+-----+-----------+
|   1 | 一年级    |
|   2 | 二年级    |
|   3 | 三年级    |
+-----+-----------+
3 rows in set (0.00 sec)

mysql> desc teach2cls;
+-------+---------+------+-----+---------+----------------+
| Field | Type    | Null | Key | Default | Extra          |
+-------+---------+------+-----+---------+----------------+
| tcid  | int(11) | NO   | PRI | NULL    | auto_increment |
| tid   | int(11) | NO   | MUL | NULL    |                |
| cid   | int(11) | NO   | MUL | NULL    |                |
+-------+---------+------+-----+---------+----------------+
3 rows in set (0.01 sec)

mysql> select * from teach2cls;
+------+-----+-----+
| tcid | tid | cid |
+------+-----+-----+
|    1 |   1 |   1 |
|    2 |   1 |   2 |
|    3 |   2 |   1 |
|    4 |   3 |   2 |
+------+-----+-----+
4 rows in set (0.00 sec)

创建数据与查询

/*1、自行创建测试数据;*/
/*
初始化数据
*/

BEGIN;
INSERT INTO `class_grade` VALUES ('1', '一年级'), ('2', '二年级'), ('3', '三年级');
COMMIT;

BEGIN;
INSERT INTO `class` VALUES ('1', '一年一班','1'), ('2', '二年一班','2'), ('3', '三年二班','3'), ('4', '二年二班','2');
COMMIT; 

BEGIN;
INSERT INTO `teacher` VALUES ('1', '张三'), ('2', '李四'), ('3', '王五'), ('4', '朱云海'), ('5', '李杰');
COMMIT;

BEGIN;
INSERT INTO `student` VALUES ('1', '女', '1', '乔丹'), ('2', '女', '1', '艾弗森'), ('3', '男', '2', '科比'), ('4', '男', '1', '张一'), ('5', '女', '1', '张二'), ('6', '男', '1', '张四'), ('7', '女', '2', '铁锤'), ('8', '男', '2', '李三'), ('9', '男', '2', '李一'), ('10', '女', '2', '李二'), ('11', '男', '2', '李四');
COMMIT;


BEGIN;
INSERT INTO `course` VALUES ('1', '生物', '1'), ('2', '体育', '1'),('3', '物理', '2'), ('4', '美术', '2'),('5', '语文', '4'),('6', '数学', '5'),('7', '英语', '3'),('8', '化学', '3');
COMMIT;


BEGIN;
INSERT INTO `teach2cls` VALUES ('1', '1','1'), ('2', '1','2'), ('3', '2','1'), ('4', '3','2'),('5', '3','4');
COMMIT;


BEGIN;
INSERT INTO `score` VALUES ('1', '1', '1', '60'), ('2', '1', '2', '59'), ('3', '2', '2', '99'), ('6', '2', '1', '8'), ('8', '2', '3', '68'), ('9', '2', '4', '95'), ('10', '3', '1', '77'), ('11', '3', '2', '66'), ('12', '3', '3', '87'), ('13', '3', '4', '99'), ('14', '4', '1', '79'), ('15', '4', '2', '11'), ('16', '4', '3', '67'), ('17', '4', '4', '100'), ('18', '5', '1', '79'), ('19', '5', '2', '11'), ('20', '5', '3', '67'), ('21', '5', '4', '100'), ('22', '6', '1', '9'), ('23', '6', '2', '100'), ('24', '6', '3', '67'), ('25', '6', '4', '100'),
('41', '1', '5', '60'), ('42', '1', '6', '59'), ('43', '2', '7', '99'), ('44', '3', '5', '60'), ('45', '2', '6', '59'), ('46', '3', '7', '89'), ('48', '1', '7', '95'), ('49', '2', '5', '59'), ('50', '3', '6', '87'); 
COMMIT;

/*2、查询学生总人数;*/
select count(sid) as '学生总人数' from student


/*3、查询“生物”课程和“物理”课程成绩都及格的学生id和姓名;*/
SELECT 
    student.sid as '学生id', student.sname as '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            student_id
        FROM
            score
                INNER JOIN
            course ON course.cid = score.course_id
        WHERE
            score >= 60
                AND course.cname IN ('生物' , '物理'))



/*4、查询每个年级的班级数,取出班级数最多的前三个年级;*/
SELECT 
    t1.gid, t1.gname as '年级名称'
FROM
    (SELECT 
        grade_id, COUNT(cid) cid_num
    FROM
        class
    GROUP BY class.grade_id
    ORDER BY cid_num DESC
    LIMIT 3) t2
        INNER JOIN
    class_grade t1 ON t2.grade_id = t1.gid;


/*5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;*/
SELECT 
    student.sid as '学生id', student.sname as '姓名', avg_score as '平均成绩'
FROM
    student
        INNER JOIN
    (SELECT 
        student_id, avg_score
    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 
        student_id, AVG(score) AS avg_score
    FROM
        score
    GROUP BY student_id
    ORDER BY AVG(score)
    LIMIT 1)) t2 ON student.sid = t2.student_id
    
/*6、查询每个年级的学生人数;*/
SELECT 
    gid, COUNT(student.sid) as '学生人数'
FROM
    class_grade
        LEFT JOIN
    class ON class.grade_id = gid
        LEFT JOIN
    student ON student.class_id = class.cid
GROUP BY gid;


/*7、查询每位学生的学号,姓名,选课数,平均成绩;*/
SELECT 
    student.sid as '学号', student.sname as '姓名', IFNULL(avg_score,0) '平均成绩', IFNULL(course_num,0) '选课数'
FROM
    student
        LEFT JOIN
    (SELECT 
        student_id,
            AVG(score) avg_score,
            COUNT(course_id) course_num
    FROM
        score
    GROUP BY student_id) AS t1 ON student.sid = t1.student_id


/*8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;*/
SELECT 
    student.sname as '学生姓名', course.cname as '课程名', t2.score as '分数'
FROM
    (SELECT 
        student_id, course_id, score
    FROM
        (SELECT 
        student_id, course_id, score
    FROM
        score
    WHERE
        student_id = '2'
    ORDER BY score DESC
    LIMIT 1) t1 UNION (SELECT 
        student_id, course_id, score
    FROM
        score
    WHERE
        student_id = '2'
    ORDER BY score
    LIMIT 1)) t2
        INNER JOIN
    student ON sid = t2.student_id
        INNER JOIN
    course ON cid = t2.course_id



/*9、查询姓“李”的老师的个数和所带班级数;*/
  SELECT 
    t1.tid, tid_num as '个数', IFNULL(cid_num, 0) as '班级数'
FROM
    (SELECT 
        tid, COUNT(tid) tid_num
    FROM
        teacher
    WHERE
        tname LIKE '李%'
    GROUP BY tid) t1
        LEFT JOIN
    (SELECT 
        tid, COUNT(cid) cid_num
    FROM
        teach2cls
    GROUP BY tid) t2 ON t2.tid = t1.tid


/*10、查询班级数小于5的年级id和年级名;*/
SELECT 
    t1.gid as '年级id' , t1.gname as '年级名'
FROM
    (SELECT 
        grade_id, COUNT(cid) cid_num
    FROM
        class
    GROUP BY class.grade_id
    HAVING COUNT(cid) < 5) t2
        INNER JOIN
    class_grade t1 ON t2.grade_id = t1.gid;
    
/* 11、查询班级信息,包括班级id、班级名称、年级、年级级别(12为低年级,34为中年级,56为高年级),示例结果
如下;
*/
SELECT 
    cid as '班级id', caption as '班级名称', t1.gname as '年级' , t1.gid_level as '年级级别'
FROM
    class
        INNER JOIN
    (SELECT 
        t.gid,
            t.gname,
            (CASE
                WHEN t.gid IN ('1' , '2') THEN '低年级'
                WHEN t.gid IN ('3' , '4') THEN '中年级'
                WHEN t.gid IN ('5' , '6') THEN '高年级'
            END) gid_level
    FROM
        class_grade t) AS t1 ON t1.gid = class.grade_id



/*12、查询学过“张三”老师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 = (SELECT 
                            tid
                        FROM
                            teacher
                        WHERE
                            tname = '张三'))
        GROUP BY student_id
        HAVING COUNT(student_id) >= 2)



/*13、查询教授课程超过2门的老师的id和姓名;*/
   SELECT 
    tid as '老师id', tname as '老师姓名'
FROM
    teacher
WHERE
    tid IN (SELECT 
            teacher_id
        FROM
            course
        GROUP BY teacher_id
        HAVING COUNT(teacher_id) > 2)



/* 14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;*/
SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            student_id
        FROM
            score
        WHERE
            course_id IN ('1' , '2'))



/*15、查询没有带过高年级的老师id和姓名;*/
SELECT 
    tid '老师id', tname '姓名'
FROM
    teacher
WHERE
    tid IN (SELECT 
            tid
        FROM
            teach2cls
        WHERE
            cid IN (SELECT 
                    cid
                FROM
                    class
                WHERE
                    grade_id IN (SELECT 
                            t.gid
                        FROM
                            class_grade t
                        WHERE
                            t.gid NOT IN ('5' , '6'))))
        



/*16、查询学过“张三”老师所教的所有课的同学的学号、姓名;*/
SELECT 
    sid '学号', sname '姓名'
FROM
    student
WHERE
    sid IN (SELECT 
            student_id
        FROM
            score
                INNER JOIN
            (SELECT 
                cid
            FROM
                course
            WHERE
                teacher_id = (SELECT 
                        tid
                    FROM
                        teacher
                    WHERE
                        tname = '张三')) t1 ON t1.cid = score.course_id
        GROUP BY student_id
        HAVING COUNT(student_id) = (SELECT 
                COUNT(cid)
            FROM
                course
            WHERE
                teacher_id = (SELECT 
                        tid
                    FROM
                        teacher
                    WHERE
                        tname = '张三')))



/*17、查询带过超过2个班级的老师的id和姓名;*/
SELECT 
    tid '老师id', tname '姓名'
FROM
    teacher
WHERE
    tid IN (SELECT 
            tid
        FROM
            teach2cls
        GROUP BY tid
        HAVING COUNT(tid) > 2)


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

SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            t2.student_id
        FROM
            (SELECT 
                student_id, score score_1
            FROM
                score
            WHERE
                course_id = '1') t1
                INNER JOIN
            (SELECT 
                student_id, score score_2
            FROM
                score
            WHERE
                course_id = '2') t2 ON t1.student_id = t2.student_id
        WHERE
            t2.score_2 < t1.score_1)



/*19、查询所带班级数最多的老师id和姓名;*/
SELECT 
    tid '老师id', tname '姓名'
FROM
    teacher
WHERE
    tid IN (SELECT 
            tid
        FROM
            teach2cls
        GROUP BY tid
        HAVING COUNT(tid) = (SELECT 
                MAX(t1.tid_num)
            FROM
                (SELECT 
                    tid, COUNT(tid) tid_num
                FROM
                    teach2cls
                GROUP BY tid) t1))


/*20、查询有课程成绩小于60分的同学的学号、姓名;*/
SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            student_id
        FROM
            score
        WHERE
            score < 60)


/*21、查询没有学全所有课的同学的学号、姓名;*/
SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid NOT IN (SELECT 
            student_id
        FROM
            score
        GROUP BY student_id
        HAVING COUNT(student_id) = (SELECT 
                COUNT(cid)
            FROM
                course))


/*22、查询至少有一门课与学号为“1”的同学所学相同的同学的学号和姓名;*/
SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            student_id
        FROM
            score
        WHERE
            student_id <> '1'
                AND course_id IN (SELECT 
                    course_id
                FROM
                    score
                WHERE
                    student_id = '1'))
            

/*23、查询至少学过学号为“1”同学所选课程中任意一门课的其他同学学号和姓名;*/
SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            student_id
        FROM
            score
        WHERE
            student_id <> '1'
                AND course_id IN (SELECT 
                    course_id
                FROM
                    score
                WHERE
                    student_id = '1'))



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

SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            student_id
        FROM
            score
        WHERE
            student_id <> '2'
                AND course_id IN (SELECT 
                    course_id
                FROM
                    score
                WHERE
                    student_id = '2')
        GROUP BY student_id
        HAVING COUNT(student_id) = (SELECT 
                COUNT(course_id)
            FROM
                score
            WHERE
                student_id = '2'))


/*25、删除学习“张三”老师课的score表记录;*/
DELETE FROM score 
WHERE
    course_id IN (SELECT 
        cid
    FROM
        course
    
    WHERE
        teacher_id = (SELECT 
            tid
        FROM
            teacher
        
        WHERE
            tname = '张三'));



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

①没有上过编号“2”课程的同学学号
SELECT 
    student.sid
FROM
    student
WHERE
    student.sid  not in(
SELECT 
            student_id
        FROM
            score
        WHERE
            course_id in ('2'))

②插入“2”号课程的平均成绩;

SELECT 
    AVG(score)
FROM
    score
WHERE
    course_id = '2'


INSERT INTO `score` VALUES ('35', '7', '1', '57'), ('36', '8', '2', '57'),('37', '9', '3', '57');
commit;



/*27、按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示: 学生ID,语文,
数学,英语,有效课程数,有效平均分;*/
  SELECT 
    student_id AS '学生ID',
    IFNULL(AVG(CASE
                WHEN course.cname = '语文' THEN score
            END),
            0) AS '语文',
    IFNULL(AVG(CASE
                WHEN course.cname = '数学' THEN score
            END),
            0) AS '数学',
    IFNULL(AVG(CASE
                WHEN course.cname = '英语' THEN score
            END),
            0) AS '英语',
    COUNT(student_id) AS '有效课程数',
    AVG(score) AS '有效平均分'
FROM
    score
        INNER JOIN
    course ON course.cid = score.course_id
GROUP BY student_id
ORDER BY AVG(score)


/*28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;*/
SELECT 
    course_id AS '课程ID',
    MAX(score) AS '最高分',
    MIN(score) AS '最低分'
FROM
    score
GROUP BY course_id;


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

SELECT 
    course_id AS '课程ID',
    AVG(score) AS '平均成绩',
    SUM(CASE
        WHEN score >= 60 THEN 1
        ELSE 0
    END) / COUNT(course_id) * 100 AS '及格率'
FROM
    score
GROUP BY course_id
ORDER BY AVG(score) ASC , SUM(CASE
    WHEN score >= 60 THEN 1
    ELSE 0
END) / COUNT(course_id) * 100 DESC;


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

SELECT 
    course_id AS '课程ID',
    AVG(score) AS '平均成绩',
    teacher.tname
FROM
    score
        INNER JOIN
    course ON course_id = course.cid
        INNER JOIN
    teacher ON teacher.tid = course.teacher_id
GROUP BY course_id
ORDER BY AVG(score) DESC;


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

SELECT 
    course_id, MAX(score) score
FROM
    score
GROUP BY course_id 
UNION (SELECT 
    score.course_id, MAX(score) AS score
FROM
    score
        INNER JOIN
    (SELECT 
        course_id, MAX(score) first_num
    FROM
        score
    GROUP BY course_id) t1 ON score.course_id = t1.course_id
WHERE
    score.score < t1.first_num
GROUP BY score.course_id)
UNION (SELECT 
    score.course_id, MAX(score) AS score
FROM
    score
        INNER JOIN
    (SELECT 
        score.course_id, MAX(score) AS second_num
    FROM
        score
    INNER JOIN (SELECT 
        course_id, MAX(score) first_num
    FROM
        score
    GROUP BY course_id) t1 ON score.course_id = t1.course_id
    WHERE
        score.score < t1.first_num
    GROUP BY score.course_id) t2 ON score.course_id = t2.course_id
WHERE
    score.score < t2.second_num
GROUP BY score.course_id) ORDER BY course_id , score DESC


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

*/
SELECT 
    cname '课程', IFNULL(stu_num, 0) '学生数'
FROM
    course
        LEFT JOIN
    (SELECT 
        course_id, COUNT(student_id) stu_num
    FROM
        score
    GROUP BY course_id) t1 ON course.cid = t1.course_id



/*33、查询选修了2门以上课程的全部学生的学号和姓名;*/
SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            student_id
        FROM
            score
        GROUP BY student_id
        HAVING COUNT(course_id) > 2)


/*34、查询男生、女生的人数,按倒序排列;*/
SELECT 
    gender '性别', COUNT(sid) '人数'
FROM
    student
GROUP BY gender
ORDER BY COUNT(sid) DESC

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

SELECT 
    sid '学号', sname '姓名'
FROM
    student
WHERE
    sname LIKE '张%'


/*36、查询同名同姓学生名单,并统计同名人数;
*/
SELECT 
    sname '学生名', COUNT(sname) '人数'
FROM
    student
GROUP BY sname
HAVING COUNT(sname) > 1

/*37、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列;
*/
SELECT 
    course_id '课程号' , AVG(score) '平均成绩'
FROM
    score
GROUP BY course_id
ORDER BY AVG(score) ASC , course_id DESC



/*38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;*/
SELECT 
    student.sname '学生姓名', score '成绩'
FROM
    student
        INNER JOIN
    (SELECT DISTINCT
        student_id, score
    FROM
        score
    INNER JOIN course ON course.cid = score.course_id
    WHERE
        score < 60 AND course.cname = '数学') t1 ON student.sid = t1.student_id


/*39、查询课程编号为“3”且课程成绩在80分以上的学生的学号和姓名;*/
SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            student_id
        FROM
            score
        WHERE
            course_id = '3' AND score > 80)


/*40、求选修了课程的学生人数;*/
SELECT 
    COUNT(DISTINCT student_id) '学生人数'
FROM
    score


/*41、查询选修“王五”老师所授课程的学生中,成绩最高和最低的学生姓名及其成绩;
*/
SELECT 
    sname '学生姓名', t4.score '成绩'
FROM
    student
        INNER JOIN
    (SELECT 
        student_id, score
    FROM
        (SELECT 
        student_id, score
    FROM
        score
    INNER JOIN (SELECT 
        cid
    FROM
        course
    WHERE
        teacher_id = (SELECT 
                tid
            FROM
                teacher
            WHERE
                tname = '王五')) t1 ON t1.cid = score.course_id
    ORDER BY score DESC
    LIMIT 1) t2 UNION (SELECT 
        student_id, score
    FROM
        score
    INNER JOIN (SELECT 
        cid
    FROM
        course
    WHERE
        teacher_id = (SELECT 
                tid
            FROM
                teacher
            WHERE
                tname = '王五')) t1 ON t1.cid = score.course_id
    ORDER BY score ASC
    LIMIT 1)) t4 ON t4.student_id = student.sid



/*42、查询各个课程及相应的选修人数;*/
SELECT 
    cname '课程', IFNULL(stu_num, 0) '选修人数' 
FROM
    course
        LEFT JOIN
    (SELECT 
        t1.course_id, COUNT(t1.student_id) stu_num
    FROM
        score t1
    GROUP BY t1.course_id) t2 ON course.cid = t2.course_id


/*43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;*/
SELECT DISTINCT
    t1.student_id '学号', t1.course_id '课程号', t1.score '学生成绩'
FROM
    score t1
        INNER JOIN
    score ON t1.student_id = score.student_id
WHERE
    t1.course_id <> score.course_id
        AND t1.score = score.score

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

SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            score.student_id
        FROM
            score
                INNER JOIN
            (SELECT 
                t3.course_id, first_num, second_num
            FROM
                (SELECT 
                course_id, MAX(score) first_num
            FROM
                score
            GROUP BY course_id) t3
            INNER JOIN (SELECT 
                score.course_id, MAX(score) AS second_num
            FROM
                score
            INNER JOIN (SELECT 
                course_id, MAX(score) first_num
            FROM 
                score
            GROUP BY course_id) t1 ON score.course_id = t1.course_id
            WHERE
                score.score < t1.first_num
            GROUP BY score.course_id) t2 ON t3.course_id = t2.course_id) t4 ON score.course_id = t4.course_id
        WHERE
            score.score <= t4.first_num
                AND score.score >= t4.second_num)



/*45、检索至少选修两门课程的学生学号;*/
SELECT 
    student_id
FROM
    score
GROUP BY student_id
HAVING COUNT(course_id) >= 2;


/*46、查询没有学生选修的课程的课程号和课程名;*/
SELECT 
    cid '课程号', cname '课程名'
FROM
    course
WHERE
    cid NOT IN (SELECT DISTINCT
            course_id
        FROM
            score)


/*47、查询没带过任何班级的老师id和姓名;*/
SELECT 
    tid '老师id', tname '姓名'
FROM
    teacher
WHERE
    tid NOT IN (SELECT 
            tid
        FROM
            teach2cls)


/*48、查询有两门以上课程超过80分的学生id及其平均成绩*/
SELECT 
    student_id '学号', AVG(score) '平均成绩'
FROM
    score
WHERE
    score > 80
GROUP BY student_id
HAVING COUNT(course_id) > 2

/*49、检索“3”课程分数小于60,按分数降序排列的同学学号;*/
SELECT 
    student_id '学号'
FROM
    score
WHERE
    course_id = '3' AND score < 60
ORDER BY score DESC;


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

delete from score where student_id='2' and course_id='1';


/*51、查询同时选修了物理课和生物课的学生id和姓名。*/
SELECT 
    student.sid '学号', student.sname '姓名'
FROM
    student
WHERE
    student.sid IN (SELECT 
            student_id
        FROM
            score
                INNER JOIN
            course ON course.cid = score.course_id
        WHERE
            course.cname IN ('生物' , '物理')
        GROUP BY student_id
        HAVING COUNT(course_id) = 2)
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门