sql作业

Posted jason-lin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql作业相关的知识,希望对你有一定的参考价值。

初始化

/*
Navicat mysql Data Transfer

Source Server         : 本地测试
Source Server Version : 50639
Source Host           : localhost:3306
Source Database       : luffy

Target Server Type    : MYSQL
Target Server Version : 50639
File Encoding         : 65001

Date: 2018-03-24 00:22:13
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `class`
-- ----------------------------
DROP TABLE IF EXISTS `class`;
CREATE TABLE `class` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `caption` varchar(16) DEFAULT NULL,
  `grade_id` int(11) DEFAULT NULL,
  UNIQUE KEY `cid` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of class
-- ----------------------------
INSERT INTO `class` VALUES (‘1‘, ‘一年一班‘, ‘1‘);
INSERT INTO `class` VALUES (‘2‘, ‘一年二班‘, ‘1‘);
INSERT INTO `class` VALUES (‘3‘, ‘二年二班‘, ‘2‘);
INSERT INTO `class` VALUES (‘4‘, ‘二年二班‘, ‘2‘);
INSERT INTO `class` VALUES (‘5‘, ‘三年一班‘, ‘3‘);
INSERT INTO `class` VALUES (‘6‘, ‘三年二班‘, ‘3‘);
INSERT INTO `class` VALUES (‘7‘, ‘三年三班‘, ‘3‘);
INSERT INTO `class` VALUES (‘8‘, ‘四年二班‘, ‘4‘);
INSERT INTO `class` VALUES (‘9‘, ‘五年二班‘, ‘5‘);
INSERT INTO `class` VALUES (‘10‘, ‘六年二班‘, ‘6‘);

-- ----------------------------
-- Table structure for `class_grade`
-- ----------------------------
DROP TABLE IF EXISTS `class_grade`;
CREATE TABLE `class_grade` (
  `gid` int(11) NOT NULL AUTO_INCREMENT,
  `gname` varchar(16) DEFAULT NULL,
  UNIQUE KEY `gid` (`gid`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of class_grade
-- ----------------------------
INSERT INTO `class_grade` VALUES (‘1‘, ‘一年级‘);
INSERT INTO `class_grade` VALUES (‘2‘, ‘二年级‘);
INSERT INTO `class_grade` VALUES (‘3‘, ‘三年级‘);
INSERT INTO `class_grade` VALUES (‘4‘, ‘四年级‘);
INSERT INTO `class_grade` VALUES (‘5‘, ‘五年级‘);
INSERT INTO `class_grade` VALUES (‘6‘, ‘六年级‘);

-- ----------------------------
-- Table structure for `course`
-- ----------------------------
DROP TABLE IF EXISTS `course`;
CREATE TABLE `course` (
  `cid` int(11) NOT NULL AUTO_INCREMENT,
  `cname` varchar(16) DEFAULT NULL,
  `teacher_id` int(11) DEFAULT NULL,
  UNIQUE KEY `cid` (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of course
-- ----------------------------
INSERT INTO `course` VALUES (‘1‘, ‘生物‘, ‘1‘);
INSERT INTO `course` VALUES (‘2‘, ‘体育‘, ‘1‘);
INSERT INTO `course` VALUES (‘3‘, ‘物理‘, ‘2‘);
INSERT INTO `course` VALUES (‘4‘, ‘语文‘, ‘2‘);
INSERT INTO `course` VALUES (‘5‘, ‘数学‘, ‘3‘);
INSERT INTO `course` VALUES (‘6‘, ‘英语‘, ‘3‘);
INSERT INTO `course` VALUES (‘7‘, ‘化学‘, ‘2‘);

-- ----------------------------
-- Table structure for `score`
-- ----------------------------
DROP TABLE IF EXISTS `score`;
CREATE TABLE `score` (
  `sid` int(11) DEFAULT NULL,
  `student_id` int(11) DEFAULT NULL,
  `course_id` int(11) DEFAULT NULL,
  `score` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of score
-- ----------------------------
INSERT INTO `score` VALUES (‘1‘, ‘1‘, ‘1‘, ‘60‘);
INSERT INTO `score` VALUES (‘2‘, ‘1‘, ‘2‘, ‘59‘);
INSERT INTO `score` VALUES (‘3‘, ‘2‘, ‘2‘, ‘99‘);
INSERT INTO `score` VALUES (‘4‘, ‘2‘, ‘3‘, ‘59‘);
INSERT INTO `score` VALUES (‘5‘, ‘4‘, ‘3‘, ‘87‘);
INSERT INTO `score` VALUES (‘6‘, ‘5‘, ‘5‘, ‘88‘);
INSERT INTO `score` VALUES (‘7‘, ‘6‘, ‘5‘, ‘45‘);
INSERT INTO `score` VALUES (‘8‘, ‘7‘, ‘4‘, ‘66‘);
INSERT INTO `score` VALUES (‘9‘, ‘8‘, ‘7‘, ‘55‘);
INSERT INTO `score` VALUES (‘10‘, ‘3‘, ‘6‘, ‘88‘);
INSERT INTO `score` VALUES (‘11‘, ‘3‘, ‘3‘, ‘99‘);
INSERT INTO `score` VALUES (‘12‘, ‘10‘, ‘4‘, ‘99‘);
INSERT INTO `score` VALUES (‘13‘, ‘1‘, ‘3‘, ‘88‘);

-- ----------------------------
-- Table structure for `student`
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `sname` varchar(16) DEFAULT NULL,
  `gender` enum(‘男‘,‘女‘) DEFAULT NULL,
  `class_id` int(11) DEFAULT NULL,
  UNIQUE KEY `sid` (`sid`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (‘1‘, ‘乔丹1‘, ‘女‘, ‘1‘);
INSERT INTO `student` VALUES (‘2‘, ‘张云‘, ‘女‘, ‘1‘);
INSERT INTO `student` VALUES (‘3‘, ‘科比‘, ‘男‘, ‘2‘);
INSERT INTO `student` VALUES (‘4‘, ‘张飞‘, ‘男‘, ‘3‘);
INSERT INTO `student` VALUES (‘5‘, ‘路飞‘, ‘男‘, ‘3‘);
INSERT INTO `student` VALUES (‘6‘, ‘路飞‘, ‘男‘, ‘4‘);
INSERT INTO `student` VALUES (‘7‘, ‘海贼王‘, ‘男‘, ‘6‘);
INSERT INTO `student` VALUES (‘8‘, ‘熊大‘, ‘男‘, ‘6‘);
INSERT INTO `student` VALUES (‘9‘, ‘熊二‘, ‘男‘, ‘7‘);
INSERT INTO `student` VALUES (‘10‘, ‘熊三‘, ‘女‘, ‘8‘);
INSERT INTO `student` VALUES (‘11‘, ‘熊四‘, ‘女‘, ‘9‘);

-- ----------------------------
-- Table structure for `teach2cls`
-- ----------------------------
DROP TABLE IF EXISTS `teach2cls`;
CREATE TABLE `teach2cls` (
  `tcid` int(11) NOT NULL AUTO_INCREMENT,
  `tid` int(11) DEFAULT NULL,
  `cid` int(11) DEFAULT NULL,
  UNIQUE KEY `tcid` (`tcid`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of teach2cls
-- ----------------------------
INSERT INTO `teach2cls` VALUES (‘1‘, ‘1‘, ‘1‘);
INSERT INTO `teach2cls` VALUES (‘2‘, ‘1‘, ‘2‘);
INSERT INTO `teach2cls` VALUES (‘3‘, ‘2‘, ‘1‘);
INSERT INTO `teach2cls` VALUES (‘4‘, ‘2‘, ‘3‘);
INSERT INTO `teach2cls` VALUES (‘5‘, ‘2‘, ‘3‘);
INSERT INTO `teach2cls` VALUES (‘6‘, ‘3‘, ‘4‘);
INSERT INTO `teach2cls` VALUES (‘7‘, ‘3‘, ‘5‘);
INSERT INTO `teach2cls` VALUES (‘8‘, ‘3‘, ‘6‘);
INSERT INTO `teach2cls` VALUES (‘9‘, ‘3‘, ‘7‘);
INSERT INTO `teach2cls` VALUES (‘10‘, ‘3‘, ‘2‘);

-- ----------------------------
-- Table structure for `teacher`
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher` (
  `tid` int(11) NOT NULL AUTO_INCREMENT,
  `tname` varchar(16) DEFAULT NULL,
  UNIQUE KEY `tid` (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES (‘1‘, ‘张三‘);
INSERT INTO `teacher` VALUES (‘2‘, ‘李四‘);
INSERT INTO `teacher` VALUES (‘3‘, ‘王五‘);
INSERT INTO `teacher` VALUES (‘4‘, ‘李杰‘);

参考答案

2、查询学生总人数;

SELECT count(sid) FROM student;

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

4、查询每个年级的班级数,取出班级数最多的前三个年级;
SELECT gname,t.num ‘班级数‘ FROM class_grade INNER JOIN
(
SELECT 
    grade_id, count(grade_id) as num
FROM 
    class 
GROUP BY 
    grade_id 
ORDER BY 
    count(grade_id) DESC LIMIT 3
) AS t ON t.grade_id=class_grade.gid;

5、查询平均成绩最高和最低的学生的id和姓名以及平均成绩;
SELECT * FROM
(SELECT sid,sname,t1.max FROM student 
INNER JOIN
    (SELECT student_id, avg(score) max FROM score GROUP BY student_id ORDER BY max DESC LIMIT 1) t1
ON t1.student_id=student.sid) a,

(SELECT sid,sname,t2.min FROM student 
INNER JOIN
    (SELECT student_id, avg(score) min FROM score GROUP BY student_id ORDER BY min LIMIT 1) t2
ON t2.student_id=student.sid) b
; 

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

SELECT gname, t1.num FROM class_grade 
INNER JOIN
    ( 
    SELECT grade_id,count(t.sid) AS num FROM class 
    INNER JOIN 
        (SELECT class_id,sid FROM student) AS t 
    ON class.cid = t.class_id
    GROUP BY grade_id 
    ) AS t1 
ON class_grade.gid = t1.grade_id
;

7、查询每位学生的学号,姓名,选课数,平均成绩;
SELECT sid ‘学号‘,sname ‘姓名‘,t.avg_num ‘平均成绩‘,t.c_num ‘选课数‘ FROM student 
INNER JOIN
    (SELECT student_id, avg(score) avg_num,count(course_id) c_num 
    FROM
        score
    GROUP BY 
        student_id
    ) t ON student.sid=t.student_id
; 
8、查询学生编号为“2”的学生的姓名、该学生成绩最高的课程名、成绩最低的课程名及分数;
SELECT * FROM
(
SELECT cname ‘课程‘,t1.max ‘最高分‘ FROM course 
INNER JOIN
    (SELECT course_id, score max FROM score WHERE student_id=2 ORDER BY score DESC LIMIT 1) t1
On t1.course_id=course.cid
) a,
(
SELECT cname ‘课程‘,t2.min ‘最低分‘ FROM course 
INNER JOIN
    (SELECT course_id, score min FROM score WHERE student_id=2 ORDER BY score LIMIT 1) t2
On t2.course_id=course.cid
) b 
;


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

SELECT t.t_num ‘个数‘,count(cid) ‘班级数‘ FROM teach2cls 
INNER JOIN 
    (SELECT tid, count(tid) t_num FROM teacher WHERE tname LIKE ‘李%‘) AS t
ON t.tid=teach2cls.tid

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

SELECT gid,gname FROM class_grade WHERE gid IN 
(SELECT grade_id FROM class GROUP BY grade_id HAVING count(cid) < 5);

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

班级id 班级名称 年级 年级级别
1 一年一班 一年级 低

SELECT t.cid ‘班级id‘,t.caption ‘班级名称‘,class_grade.gname ‘年级‘  FROM class_grade 
INNER JOIN
    (SELECT cid,caption,grade_id FROM class ORDER BY grade_id) t
    ON t.grade_id = class_grade.gid
;

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

SELECT sid,sname FROM student INNER JOIN 
(
SELECT student_id FROM score WHERE course_id 
IN
    (
    SELECT cid FROM course 
    WHERE 
        teacher_id=(SELECT tid FROM teacher WHERE tname LIKE ‘zhangsan‘)
    )

GROUP BY student_id HAVING count(course_id) >= 2
) AS t ON student.sid = t.student_id

13、查询教授课程超过2门的老师的id和姓名;
SELECT tid,tname FROM teacher WHERE tid IN 
(SELECT tid FROM teach2cls GROUP BY tid HAVING count(cid)>2);

14、查询学过编号“1”课程和编号“2”课程的同学的学号、姓名;
SELECT sid,sname FROM student WHERE sid IN
(
    SELECT student_id FROM score 
    WHERE course_id IN (1,2)
    GROUP BY 
        student_id  
);
15、查询没有带过高年级的老师id和姓名;
SELECT tid,tname FROM teacher 
WHERE 
    tid NOT IN
    (
    SELECT tid FROM teach2cls 
    WHERE 
        cid IN (SELECT cid FROM class WHERE grade_id=5 OR grade_id=6)
    );

16、查询学过“张三”老师所教的所有课的同学的学号、姓名;
SELECT sid,sname FROM student 
WHERE 
sid IN (
        SELECT student_id FROM score 
        WHERE 
            course_id IN
            (
            SELECT cid FROM teach2cls 
            WHERE 
                tid IN (SELECT tid FROM teacher WHERE   tname = ‘张三‘)
            )
);

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

18、查询课程编号“2”的成绩比课程编号“1”课程低的所有同学的学号、姓名;
SELECT score FROM score WHERE course_id=1
SELECT score FROM score WHERE course_id=2
SELECT student_id FROM score GROUP BY student_id
WHERE 
    (SELECT score FROM score WHERE course_id=2) <
    (SELECT score FROM score WHERE course_id=1)

19、查询所带班级数最多的老师id和姓名;
SELECT tid,tname FROM teacher 
WHERE 
tid=(SELECT tid FROM teach2cls GROUP BY tid ORDER BY count(cid) DESC LIMIT 1);

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

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

22、查询至少有一门课与学号为“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) 
    AND
        student_id != 1
);

23、查询至少学过学号为“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)
);

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

SELECT student_id,COUNT(course_id) FROM score 
WHERE 
    course_id 
IN 
    (SELECT course_id FROM score WHERE student_id=2)

GROUP BY 
    student_id 
HAVING 
    count(course_id)=
    (SELECT count(course_id) FROM score WHERE student_id=2)
;

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

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

INSERT INTO 
    score 
SELECT 
    sid,student_id,
    course_id,
    (SELECT avg(score) FROM score WHERE course_id=2) 
    FROM score
WHERE 
    student_id 
NOT IN 
    (SELECT student_id FROM score WHERE course_id=2);

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

SELECT
        sc.student_id ‘学生ID‘,
        (SELECT score FROM score LEFT JOIN course
            ON 
                score.course_id = course.cid 
            WHERE 
            course.cname = ‘语文‘ AND score.student_id=sc.student_id) AS ‘语文‘,
        (SELECT score FROM score LEFT JOIN course 
            ON 
                score.course_id = course.cid 
            WHERE course.cname = ‘数学‘ AND score.student_id=sc.student_id) AS ‘数学‘,

        (SELECT score FROM score LEFT JOIN course
            ON score.course_id = course.cid 
            WHERE 
                course.cname = ‘英语‘ AND score.student_id=sc.student_id) AS ‘英语‘,

        count(sc.course_id) ‘有效课程‘,

        avg(sc.score) ‘平均成绩‘

FROM score AS sc GROUP BY student_id;
 
28、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分;
SELECT course_id ‘课程ID‘, max(score) ‘最高分‘,min(score) ‘最低分‘ 
FROM 
    score GROUP BY course_id;

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

SELECT score.course_id, 
       avg(score.score) ‘平均成绩‘,
       t.num/count(score.student_id) precent
FROM 
    score 
INNER JOIN 
    (SELECT course_id, count(student_id)*100 num FROM score WHERE score>=60 GROUP BY course_id) t
ON t.course_id=score.course_id
GROUP BY course_id ORDER BY precent DESC;

30、课程平均分从高到低显示(显示任课老师);
SELECT tname,t2.course_id,t2.avg_num FROM teacher INNER JOIN
(
SELECT tid,t.course_id,t.avg_num FROM teach2cls INNER JOIN
(
SELECT course_id, avg(score) avg_num 
FROM 
    score GROUP BY course_id 
) t ON teach2cls.cid=t.course_id
) t2 ON teacher.tid=t2.tid
ORDER BY t2.avg_num DESC;

31、查询各科成绩前三名的记录(不考虑成绩并列情况)
SELECT score.sid,score.course_id,t.first_num ‘第一‘,score.score ‘第二‘,t.third_num ‘第三‘
FROM score LEFT JOIN
(
SELECT
     sid,
    (SELECT score FROM score s1 WHERE s1.course_id=s2.course_id ORDER BY score DESC LIMIT 1) first_num,
    (SELECT IFNULL(score,0) FROM score s1 WHERE s1.course_id=s2.course_id ORDER BY score DESC LIMIT 3,1) third_num
FROM  
    score s2
) AS t ON t.sid=score.sid
WHERE score.score>=t.third_num AND score.score <= t.first_num;
;

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

SELECT course_id, count(student_id) FROM score GROUP BY course_id;

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

34、查询男生、女生的人数,按倒序排列;
SELECT * FROM
(SELECT count(sid) ‘男‘ FROM student WHERE gender=‘男‘) as a,
(SELECT count(sid) ‘女‘ FROM student WHERE gender=‘女‘) as b
;

35、查询姓“张”的学生名单;
SELECT * FROM student WHERE sname LIKE ‘张%‘;

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

SELECT sname, count(sid) FROM student GROUP BY sname HAVING count(sid)!=1

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

38、查询课程名称为“数学”,且分数低于60的学生姓名和分数;

SELECT sname ‘姓名‘, t.score ‘分数‘ FROM student INNER JOIN
(
SELECT student_id,score FROM score 
WHERE 
    course_id=(SELECT cid FROM course WHERE cname=‘数学‘)
AND score<60
) t ON student.sid=t.student_id;

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

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

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

SELECT * FROM 

(SELECT student.sname,a.max FROM student 
INNER JOIN 
    (
    SELECT student_id,max(score) max FROM score 
    WHERE 
        course_id IN (SELECT cid FROM teach2cls 
        WHERE 
            tid=(SELECT tid FROM teacher WHERE tname=‘王五‘))
    ) a ON student.sid=a.student_id
) AS t1,
(
SELECT student.sname,b.min FROM student 
INNER JOIN 
    (
    SELECT student_id,min(score) min FROM score 
    WHERE 
        course_id IN (SELECT cid FROM teach2cls 
        WHERE 
            tid=(SELECT tid FROM teacher WHERE tname=‘王五‘))
    ) b ON student.sid=b.student_id
) AS t2;

42、查询各个课程及相应的选修人数;
SELECT cname ‘课程‘,t.num ‘选修人数‘ FROM course INNER JOIN

(SELECT course_id, count(student_id) num FROM score GROUP BY course_id) t
ON t.course_id=course.cid
;

43、查询不同课程但成绩相同的学生的学号、课程号、学生成绩;
SELECT s1.student_id,s1.course_id,s1.score,s2.student_id,s2.course_id,s2.score 
FROM 
    score AS s1,score AS s2 
WHERE
s1.score=s2.score AND s1.course_id != s2.course_id;

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

SELECT student.sid,student.sname,t4.course_id,t4.first_num,t4.second_num FROM student
INNER JOIN
    (
    SELECT score.student_id,t3.course_id,t3.first_num, t3.second_num FROM score 
    INNER JOIN
    (
    SELECT t1.course_id,t1.first_num,t2.second_num 
    FROM
        (SELECT course_id, max(score) AS first_num FROM score GROUP BY course_id) AS t1
    INNER JOIN 
        (
        SELECT 
            score.course_id, max(score) AS second_num 
        FROM 
            score 
        INNER JOIN 
            (SELECT course_id, max(score) AS first_num FROM score GROUP BY course_id ) as t
        ON 
            score.course_id=t.course_id 
        WHERE
            score.score < t.first_num
        GROUP BY
            course_id
        ) AS t2 ON t1.course_id = t2.course_id
    ) AS t3 ON t3.course_id=score.course_id 
    WHERE 
        score.score >= t3.second_num
    AND
        score.score<= t3.first_num
) t4 
ON t4.student_id=student.sid;

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 course_id FROM score);

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

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

SELECT student_id,avg(score) FROM score WHERE student_id IN 
(SELECT student_id 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 sid,sname FROM student WHERE sid IN 
(
    SELECT student_id FROM score
    INNER JOIN
    (
        SELECT student_id FROM score 
        WHERE 
            course_id=(SELECT cid FROM course WHERE cname=‘物理‘)
    ) t ON t.student_id=score.student_id
    WHERE 
        course_id=( SELECT student_id FROM score WHERE 
                    course_id=(SELECT cid FROM course WHERE cname=‘生物‘))
);

以上是关于sql作业的主要内容,如果未能解决你的问题,请参考以下文章

HTML5期末大作业:餐饮美食网站设计——咖啡(10页) HTML+CSS+JavaScript 学生DW网页设计作业成品 web课程设计网页规划与设计 咖啡网页设计 美食餐饮网页设计...(代码片段

Microsoft SQL Server 代码片段收集

缺少 SQL SERVER 2014 代码片段

sql Oracle代码片段

sql 日期转换代码片段 - Dato,120,konvertere

以下代码片段是不是容易受到 Rails 5 中 SQL 注入的影响?