A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© tanmingming2019 初级黑马   /  2019-12-24 16:44  /  1157 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 tanmingming2019 于 2019-12-27 16:50 编辑

sql是大数据课程的重点,以后大部分工作都是写sql,学习mysql时做了比较多的练习,尤其是网传的sql练习经典50题,前后练习了好几遍,
sql经典练习记录如下[sqlyog可以跑通]:
CREATE DATABASE myExercise;
-- 学生表
CREATE TABLE `Student`(
        `s_id` VARCHAR(20),-- 学生编号
        `s_name` VARCHAR(20) NOT NULL DEFAULT '',-- 学生姓名
        `s_birth` VARCHAR(20) NOT NULL DEFAULT '',-- 出生日期
        `s_sex` VARCHAR(10) NOT NULL DEFAULT '',-- 学生性别
        PRIMARY KEY(`s_id`)
);
-- 课程表
CREATE TABLE `Course`(
        `c_id`  VARCHAR(20),-- 课程编号
        `c_name` VARCHAR(20) NOT NULL DEFAULT '',-- 课程名
        `t_id` VARCHAR(20) NOT NULL, -- 教师编号
        PRIMARY KEY(`c_id`)
);
-- 教师表
CREATE TABLE `Teacher`(
        `t_id` VARCHAR(20), -- 教师编号
        `t_name` VARCHAR(20) NOT NULL DEFAULT '',-- 教师姓名
        PRIMARY KEY(`t_id`)
);
-- 成绩表
CREATE TABLE `Score`(
        `s_id` VARCHAR(20),-- 学生编号
        `c_id`  VARCHAR(20), -- 课程编号
        `s_score` INT(3),-- 课程成绩
        PRIMARY KEY(`s_id`,`c_id`)
);
-- 插入学生表测试数据
INSERT INTO Student VALUES('01' , '赵雷' , '1990-01-01' , '男');
INSERT INTO Student VALUES('02' , '钱电' , '1990-12-21' , '男');
INSERT INTO Student VALUES('03' , '孙风' , '1990-05-20' , '男');
INSERT INTO Student VALUES('04' , '李云' , '1990-08-06' , '男');
INSERT INTO Student VALUES('05' , '周梅' , '1991-12-01' , '女');
INSERT INTO Student VALUES('06' , '吴兰' , '1992-03-01' , '女');
INSERT INTO Student VALUES('07' , '郑竹' , '1989-07-01' , '女');
INSERT INTO Student VALUES('08' , '王菊' , '1990-01-20' , '女');
-- 课程表测试数据
INSERT INTO Course VALUES('01' , '语文' , '02');
INSERT INTO Course VALUES('02' , '数学' , '01');
INSERT INTO Course VALUES('03' , '英语' , '03');

-- 教师表测试数据
INSERT INTO Teacher VALUES('01' , '张三');
INSERT INTO Teacher VALUES('02' , '李四');
INSERT INTO Teacher VALUES('03' , '王五');

-- 成绩表测试数据
INSERT INTO Score VALUES('01' , '01' , 80);
INSERT INTO Score VALUES('01' , '02' , 90);
INSERT INTO Score VALUES('01' , '03' , 99);
INSERT INTO Score VALUES('02' , '01' , 70);
INSERT INTO Score VALUES('02' , '02' , 60);
INSERT INTO Score VALUES('02' , '03' , 80);
INSERT INTO Score VALUES('03' , '01' , 80);
INSERT INTO Score VALUES('03' , '02' , 80);
INSERT INTO Score VALUES('03' , '03' , 80);
INSERT INTO Score VALUES('04' , '01' , 50);
INSERT INTO Score VALUES('04' , '02' , 30);
INSERT INTO Score VALUES('04' , '03' , 20);
INSERT INTO Score VALUES('05' , '01' , 76);
INSERT INTO Score VALUES('05' , '02' , 87);
INSERT INTO Score VALUES('06' , '01' , 31);
INSERT INTO Score VALUES('06' , '03' , 34);
INSERT INTO Score VALUES('07' , '02' , 89);
INSERT INTO Score VALUES('07' , '03' , 98);

SELECT * FROM score;
SELECT * FROM student;
SELECT * FROM course;

-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT t1.*,t2.`s_score` AS '01课程成绩',t3.`s_score` AS '02课程成绩'
FROM student t1,score t2, score t3
WHERE t1.s_id = t2.`s_id` AND t1.s_id = t3.`s_id` AND t2.`c_id` = '01' AND t3.`c_id` = '02' AND t2.`s_score` > t3.`s_score`;

        
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT
        t1.*,t2.`s_score` AS '01课程成绩',t3.`s_score` AS '02课程成绩'
FROM
        student t1,score t2, score t3
WHERE
        t1.s_id = t2.`s_id` AND t1.s_id = t3.`s_id` AND t2.`c_id` = '01' AND t3.`c_id` = '02' AND t2.`s_score` < t3.`s_score`;

-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT
        t1.s_id,
        t1.s_name,
        ROUND (AVG(t2.`s_score`),1)'平均成绩'
FROM
        student t1,
        score t2
WHERE
        t1.s_id = t2.`s_id` GROUP BY t2.s_id HAVING AVG(t2.`s_score`)>= 60;
-- WHERE t1.s_id = t2.`s_id` GROUP BY t1.s_id,t1.s_name HAVING AVG(t2.`s_score`)>= 60; 查不出结果 having跟聚合函数
SELECT b.s_id,b.s_name,ROUND(AVG(a.s_score),2) AS avg_score FROM
        student b
        JOIN score a ON b.s_id = a.s_id
        GROUP BY a.s_id HAVING avg_score >=60;        
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
                -- (包括有成绩的和无成绩的)
SELECT
        t1.s_id,
        t1.s_name,
        ROUND (AVG(t2.`s_score`),1)'平均成绩'
FROM
        student t1,
        score t2
WHERE t1.s_id = t2.`s_id` GROUP BY t2.s_id HAVING AVG(t2.`s_score`)<60
UNION -- 两张表当做一张表
(SELECT s_id, s_name,0 AS  '平均成绩' FROM student  WHERE s_id  NOT IN(SELECT DISTINCT s_id FROM score)); -- 无成绩的,序号不在成绩表中

-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT
        t1.s_id,
        t1.s_name,
        COUNT(t2.`c_id`) AS '选课总数',
        SUM(t2.`s_score`) AS '总成绩'
FROM
        student t1,
        score t2
WHERE
        t1.s_id = t2.`s_id` GROUP BY t2.s_id,t1.s_name        
UNION
SELECT s_id,s_name, 0 AS '选课总数',0 AS '总成绩' FROM student WHERE s_id NOT IN (SELECT DISTINCT s_id FROM score);
-- 6、查询"李"姓老师的数量
SELECT         
        COUNT(t_name) AS '李姓老师数量'
FROM
        Teacher
WHERE t_name LIKE '李%';


-- 7、查询学过"张三"老师授课的同学的信息
SELECT
        t1.*
FROM
        student t1,
        course t2,
        Teacher t3,  
        score t4
WHERE t1.s_id = t4.s_id AND t2.`c_id` = t4.c_id AND  t2.`t_id` = t3.`t_id` AND t3.`t_name` = '张三';

SELECT
        t1.*
FROM
        student t1,
        score t2                     #这里的'='最好换成'in',万一张三教的不止一门课程
WHERE t1.s_id = t2.s_id AND t2.`c_id` = (SELECT t3.c_id FROM course t3,teacher t4 WHERE t3.`t_id` = t4.`t_id` AND t4.`t_name` = '张三');

-- 8、查询没学过"张三"老师授课的同学的信息
SELECT *
FROM
        student
WHERE  s_name NOT IN (
        SELECT t1.s_name
        FROM student t1, course t2, Teacher t3,  score t4
        WHERE t1.s_id = t4.s_id AND t2.`c_id` = t4.c_id AND  t2.`t_id` = t3.`t_id` AND t3.`t_name` = '张三');


-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT
        t1.*
FROM
        student t1 JOIN
        (SELECT t3.`s_id` FROM score t3 WHERE t3.`c_id` = '01' AND t3.`s_id` IN
                (SELECT t4.`s_id` FROM score  t4 WHERE t4.`c_id` = '02') ) t2
ON t1.s_id = t2.s_id;

SELECT
        t1.*
FROM
        student t1,
        score t2
WHERE
        t1.`s_id` = t2.`s_id` AND t2.`c_id` = '01' AND t2.s_id IN(SELECT s_id FROM score WHERE  c_id = '02');

-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT
        t1.*
FROM
        student t1 JOIN  
        (SELECT t4.`s_id` FROM score  t4 WHERE t4.`c_id` = '01'
AND t4.`s_id` NOT IN (SELECT t3.`s_id` FROM score t3 WHERE t3.`c_id` = '02') )t2 ON t1.s_id = t2.s_id;

SELECT
        t1.*
FROM
        student t1,
        score t2
WHERE
        t1.`s_id` = t2.`s_id` AND t2.`c_id` = '01' AND t2.s_id NOT IN(SELECT s_id FROM score WHERE  c_id = '02');

-- 11、查询没有学全所有课程的同学的信息
SELECT
        t1.*
FROM
        student t1 JOIN
        (SELECT s_id,COUNT(c_id) FROM score GROUP BY c_id HAVING COUNT(c_id) <> (SELECT COUNT(c_id) FROM course)) t2
ON t1.s_id = t2.s_id ;

-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

SELECT
        t1.*
FROM
        student t1 JOIN
        (SELECT DISTINCT s_id FROM score WHERE c_id IN(SELECT c_id FROM score WHERE s_id = '01')) t2
ON t1.s_id = t2.s_id AND t1.s_id != '01';

-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
-- 所学课程门数相同并且课程号之和相同,就认为学的课程一样
SELECT
t1.*
FROM student t1 JOIN
(SELECT DISTINCT s_id,SUM(c_id),COUNT(c_id) FROM score  WHERE s_id != '01' GROUP BY s_id HAVING SUM(c_id) =(SELECT SUM(c_id) FROM score WHERE s_id = '01') AND
COUNT(c_id) = (SELECT COUNT(c_id) FROM score WHERE s_id = '01')) t2
ON t1.s_id = t2.s_id; ## 该方法有漏洞

-- 课程数和01同学相同,并且学的课程不能是01同学没学过的课程
SELECT
    t1.*
FROM
    student t1
WHERE  t1.s_id IN(
#和01同学学的课程数一样
SELECT s_id FROM score GROUP BY s_id HAVING
     COUNT(c_id) = (SELECT COUNT(c_id) FROM score WHERE s_id = '01')
) AND t1.s_id IN(
                #学过的课程不能是01同学没学过的课程
        SELECT DISTINCT s_id FROM score WHERE c_id NOT IN (
                #01同学没学过的课程
                SELECT c_id FROM score WHERE  c_id NOT IN(
                        #01同学学过的课程
                        SELECT c_id FROM score WHERE s_id = '01'
                        )
      )
) AND t1.s_id != '01'; #去除01学生本身

SELECT
        Student.*
FROM
        Student
WHERE s_id IN (
        SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(s_id) = (
             #下面的语句是找到'01'同学学习的课程数
             SELECT COUNT(c_id) FROM Score WHERE s_id = '01')
        )
AND s_id NOT IN (
        #下面的语句是找到学过‘01’同学没学过的课程,有哪些同学。并排除他们
        SELECT s_id FROM Score
WHERE c_id IN(
   #下面的语句是找到‘01’同学没学过的课程
   SELECT DISTINCT c_id FROM Score WHERE c_id NOT IN (
        #下面的语句是找出‘01’同学学习的课程
        SELECT c_id FROM Score WHERE s_id = '01'
    )
  ) GROUP BY s_id
) AND s_id NOT IN ('01')#下面的条件是排除01同学

-- 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
SELECT
    s_name
FROM
    student
WHERE
    s_id NOT IN(
        #学过张三老师课程的学生[张三老师可能教多门课程,并且有的学生也可能上过张三老师的多门课,所以这里筛选出来的学生用distinct去除重复记录]
        SELECT  DISTINCT s_id FROM score  WHERE c_id  IN (
            #张三老师所教的课程
            SELECT c_id FROM course t1,Teacher t2 WHERE t2.`t_name` = '张三' AND t1.`t_id` = t2.`t_id`
        )
);

-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
SELECT
         t1.s_name,-- 学生姓名
         t2.* -- 两门不及格学生学号及平均成绩
FROM
         student t1
JOIN
        #有两门以上不及格的学生学号和平均成绩
        (SELECT s_id,ROUND(AVG(s_score),1)'平均成绩' FROM score  WHERE s_score < 60 GROUP BY s_id HAVING COUNT(c_id) >=2) t2

ON
        t1.s_id = t2.s_id;

-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息
SELECT
         t1.*,
         t2.c_id,
         t2.s_score
FROM
         student t1,
         (SELECT s_id,c_id, s_score FROM score WHERE s_score < 60 AND c_id = '01' ORDER BY s_score DESC) t2
WHERE t1.s_id = t2.s_id;

SELECT
         t1.*,
         t2.c_id,
         t2.s_score
FROM
         student t1,
         score t2
WHERE t1.s_id = t2.s_id AND t2.s_score < 60 AND t2.c_id = '01' ORDER BY t2.s_score DESC;

-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
SELECT t1.s_id,IFNULL((SELECT s_score FROM score WHERE t1.`s_id` = s_id AND c_id = '01'),0)AS '语文',
               IFNULL((SELECT s_score FROM score WHERE t1.`s_id` = s_id AND c_id = '02'),0)AS '数学',
               IFNULL((SELECT s_score FROM score WHERE t1.`s_id` = s_id AND c_id = '03'),0)AS '英语',
               ROUND(AVG(s_score),1) AS '平均成绩'
               FROM score t1 GROUP BY t1.s_id ORDER BY ROUND(AVG(s_score),1) DESC;

-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
SELECT      
        t1.c_id AS '课程号',
       (SELECT c_name FROM course WHERE c_id  = t1.c_id)AS'课程名',
        MAX(s_score)AS '最高分',
        MIN(s_score)AS '最低分',
        ROUND(AVG(s_score),1)AS '平均分',
        CONCAT(ROUND((SELECT COUNT(s_score) FROM score WHERE s_score >=60 AND c_id = t1.c_id)/(SELECT COUNT(s_score) FROM score WHERE c_id = t1.c_id)*100,1),'%') AS '及格率',
        CONCAT(ROUND((SELECT COUNT(s_score) FROM score WHERE s_score >=70 AND s_score < 80 AND c_id = t1.c_id)/(SELECT COUNT(s_score) FROM score WHERE c_id = t1.c_id)*100,1),'%') AS '中等率',
        CONCAT(ROUND((SELECT COUNT(s_score) FROM score WHERE s_score >=80 AND s_score < 90 AND c_id = t1.c_id)/(SELECT COUNT(s_score) FROM score WHERE c_id = t1.c_id)*100,1),'%') AS '优良率',
        CONCAT(ROUND((SELECT COUNT(s_score) FROM score WHERE s_score >=90 AND c_id = t1.c_id)/(SELECT COUNT(s_score) FROM score WHERE c_id = t1.c_id)*100,1),'%') AS '优秀率'
FROM
        score t1
GROUP BY c_id ;                  

-- 19、按各科成绩进行排序,并显示排名
SELECT
        t3.*,
       (@rank:=@rank+1) AS '排名'
FROM
        #找出各科平均成绩并降序排列
        (SELECT t1.C_id '课程号', t1.c_name '课程名',ROUND(AVG(t2.s_score),1)'平均成绩'FROM course t1,score t2  WHERE  t1.`c_id` = t2.`c_id`  GROUP BY t2.`c_id` ORDER BY AVG(t2.s_score) DESC) t3,
        (SELECT @rank :=0)t4;

-- 20、查询学生的总成绩并进行排名
SELECT
        t3.*,
        (@rank:=@rank + 1)AS'排名'

FROM     #学生成绩降序排列
        (SELECT t1.s_id,t2.s_name,SUM(s_score)'总成绩' FROM score t1,student t2 WHERE t1.s_id = t2.s_id  GROUP BY t1.s_id ORDER BY SUM(s_score) DESC) t3,
        (SELECT @rank:=0)t4;

-- 21、查询不同老师所教不同课程平均分从高到低显示
SELECT
        t1.`t_id`'教师编号',
        t3.`t_name`'教师姓名',
        t1.`c_id`'课程编号',
        t1.`c_name` '课程名',
        ROUND(AVG(s_score),1) AS'平均成绩'
FROM
        course t1 LEFT JOIN score t2
ON t1.`c_id` = t2.`c_id` LEFT JOIN teacher t3
ON t3.`t_id` = t1.`t_id` GROUP BY t1.c_id,t1.`t_id`,t3.`t_name` ORDER BY AVG(s_score) DESC;

-- 22、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
SELECT
        DISTINCT t1.`c_id`'课程号',
        t1.`c_name`'课程名',
        (SELECT COUNT(s_score) FROM score WHERE s_score >= 85 AND s_score <= 100 AND t1.`c_id` = c_id)'[85-100]人数',
        ((SELECT COUNT(s_score) FROM score WHERE s_score >= 85 AND s_score <= 100 AND t1.`c_id` = c_id)*100/(SELECT COUNT(s_score) FROM score WHERE  t1.`c_id` = c_id))'百分比'
FROM
        course t1,score t2
WHERE
        t1.`c_id` = t2.`c_id`;

-- 24、查询学生平均成绩及其名次
SELECT t3.name'学生姓名',
       t3.id'学号',
       t3.avg_socre'平均成绩',
      (@rank:=@rank+1)'排名'
FROM
        (SELECT
                t1.s_name NAME,
                t2.*
        FROM
                student t1,
        #查出学生平均成绩降序排列
                (SELECT s_id id,AVG(s_score) avg_socre FROM score GROUP BY s_id ORDER BY avg_socre DESC)t2
        WHERE
                 t1.s_id = t2.id )t3,
        (SELECT @rank:=0)t4;
        
-- 25、查询各科成绩前三名的记录[???]
                        -- 1.选出b表比a表成绩大的所有组
                        -- 2.选出比当前id成绩大的 小于三个的
SELECT a.s_id,a.c_id,a.s_score FROM score a
        LEFT JOIN score b ON a.c_id = b.c_id AND a.s_score<b.s_score
        GROUP BY a.s_id,a.c_id,a.s_score HAVING COUNT(a.s_id)<3
        ORDER BY a.c_id,a.s_score DESC;

#牛逼写法                                                
SELECT a.s_id,a.c_id,a.s_score FROM score a
WHERE (SELECT COUNT(1) FROM score b WHERE b.c_id=a.c_id AND b.s_score>a.s_score)<3 ORDER BY a.c_id, a.`s_score`DESC;

-- 26、查询每门课程被选修的学生数
SELECT c_id, COUNT(s_score) FROM score GROUP BY c_id;
SELECT c_id,COUNT(s_id) FROM score a GROUP BY c_id;

-- 27、查询出只有两门课程的全部学生的学号和姓名
-- 多行多列查询
SELECT t1.s_id,t1.s_name
FROM student t1,(SELECT s_id, COUNT(c_id) num FROM score GROUP BY s_id)t2
WHERE t1.s_id = t2.s_id  AND t2.num = 2;

-- 内连接查询
SELECT t1.s_id,t1.s_name
FROM student t1 JOIN score t2 ON t1.s_id = t2.s_id  GROUP BY  t2.`s_id` HAVING COUNT(c_id) = 2;

-- 多行单列 多表查询
SELECT t1.s_id,t1.s_name FROM student t1
WHERE t1.s_id IN(SELECT s_id FROM score GROUP BY s_id HAVING COUNT(c_id) = 2);

-- 28、查询男生、女生人数
SELECT s_sex, COUNT(s_id)'人数' FROM student GROUP BY s_sex;

-- 29、查询名字中含有"风"字的学生信息
SELECT * FROM student WHERE s_name LIKE"%风%";

-- 30、查询同名同性学生名单,并统计同名人数
SELECT a.s_name,a.s_sex,COUNT(*) FROM student a  JOIN
                                        student b ON a.s_id !=b.s_id AND a.s_name = b.s_name AND a.s_sex = b.s_sex
                GROUP BY a.s_name,a.s_sex
-- 31、查询1990年出生的学生名单
SELECT s_name FROM student WHERE s_birth LIKE"1990%";
SELECT s_name FROM student WHERE YEAR(DATE_FORMAT(s_birth),'%Y') = 1990;

-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT c_id, ROUND(AVG(s_score),1) avg_score FROM score GROUP BY c_id ORDER BY avg_score DESC,c_id ASC;

-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
SELECT
    t1.s_id,-- 学号
    t1.s_name,-- 学生姓名
    t2.avg_score -- 平均成绩
FROM
    student t1 JOIN (SELECT s_id,ROUND(AVG(s_score),1) avg_score FROM score GROUP BY s_id HAVING avg_score > 85) t2
ON
    t1.s_id = t2.`s_id`;

SELECT
    t1.s_id,-- 学号
    t1.s_name,-- 学生姓名
    AVG(s_score) -- 平均成绩
FROM
    student t1 JOIN score t2
ON
    t1.s_id = t2.`s_id` GROUP BY t2.`s_id` HAVING AVG(s_score) > 85;

-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数
SELECT
    t3.s_name'学生姓名',
    t4.s_score'数学分数'
FROM
    student t3 RIGHT JOIN  

#查询数学成绩低于60分的学生id和分数
        (SELECT
             s_id,
             s_score
        FROM
             course t1,
             score t2
        WHERE
            t1.`c_id` = t2.`c_id` AND t1.`c_name` = '数学' AND t2.`s_score` < 60)t4
ON t3.s_id = t4.s_id;        

SELECT a.s_name,b.s_score FROM score b JOIN student a ON a.s_id=b.s_id WHERE b.c_id=(
                                        SELECT c_id FROM course WHERE c_name ='数学') AND b.s_score<60;
-- 35、查询所有学生的课程及分数情况;
SELECT t1.s_name'学生姓名',t1.s_id'学号',
               (SELECT s_score FROM score WHERE c_id = '01' AND s_id = t1.s_id)'语文',
               (SELECT s_score FROM score WHERE c_id = '02' AND s_id = t1.s_id)'数学',
               (SELECT s_score FROM score WHERE c_id = '03' AND s_id = t1.s_id)'英语'
FROM student t1 JOIN score t2 ON t1.s_id = t2.s_id GROUP BY t2.`s_id`;

-- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;
SELECT
        t1.s_name,
        t2.s_score,
        t3.c_name
FROM
        student t1 JOIN

#任何一门课程成绩在70分以上的学号、课程号和分数;
(SELECT s_id, c_id, s_score,COUNT(s_score) num FROM  score WHERE s_score > 70 GROUP BY s_id HAVING num >=1)t2
ON t1.s_id = t2.s_id JOIN course t3 ON t2.c_id = t3.`c_id`;

-- 37、查询不及格的课程
SELECT
        t2.`c_name`,
        t1.`s_id`,
        t1.`c_id`,
        t1.`s_score`
FROM
        score t1
        JOIN course t2
ON
        t1.`c_id` = t2.`c_id` AND t1.`s_score` < 60;

-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名;
SELECT
    t1.s_name,-- 学生姓名
    t1.s_id, -- 学号
    t2.`s_score` -- 01课程成绩
FROM
    student t1 JOIN score t2
ON  t1.s_id = t2.`s_id` AND t2.`s_score` >= 80 AND t2.`c_id` = '01';

-- 39、求每门课程的学生人数

SELECT
     c_id'课程号',
     COUNT(s_id)'学生数'
FROM score GROUP BY c_id;

-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

SELECT
        t1.*,
        MAX(t2.s_score)'最高成绩'
FROM student t1 JOIN score t2
ON t2.c_id = (SELECT c_id FROM course a JOIN  teacher b  ON b.`t_name` = '张三' AND a.`t_id` = b.`t_id`);

-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
SELECT
        DISTINCT t1.`s_id`,
        t2.`c_id`,
        t2.`s_score`
FROM
        score t1
        JOIN score t2
ON
        t1.`s_id` = t2.`s_id` AND  t1.`s_score` = t2.`s_score` AND t1.`c_id`!= t2.`c_id`;

-- 42、查询每门功成绩最好的前两名         
SELECT a.s_id,a.c_id,a.s_score FROM score a
WHERE (SELECT COUNT(1) FROM score b WHERE b.c_id=a.c_id AND b.s_score>a.s_score)<2 ORDER BY a.c_id DESC;

-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
SELECT c_id,COUNT(s_id) num FROM score GROUP BY c_id HAVING num>= 5 ORDER BY num DESC,c_id ASC;

-- 44、检索至少选修两门课程的学生学号
SELECT s_id, COUNT(c_id) num FROM score GROUP BY s_id HAVING num >=2;

-- 45、查询选修了全部课程的学生信息
SELECT
        t1.*
FROM
        student t1,score t2
WHERE
        t1.s_id = t2.`s_id` GROUP BY t2.`s_id` HAVING COUNT(t2.`c_id`) = (SELECT COUNT(c_id) FROM course);

--  46、查询各学生的年龄
        -- 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一

        SELECT s_birth,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y') -
                                (CASE WHEN DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(s_birth,'%m%d') THEN 0 ELSE 1 END)) AS age
                FROM student;


-- 47、查询本周过生日的学生
        SELECT * FROM student WHERE WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))=WEEK(s_birth)
        SELECT * FROM student WHERE YEARWEEK(s_birth)=YEARWEEK(DATE_FORMAT(NOW(),'%Y%m%d'))
        
        SELECT WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))

-- 48、查询下周过生日的学生
        SELECT * FROM student WHERE WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))+1 = WEEK(s_birth)

-- 49、查询本月过生日的学生

        SELECT * FROM student WHERE MONTH(DATE_FORMAT(NOW(),'%Y%m%d')) = MONTH(s_birth)
        
-- 50、查询下月过生日的学生
        SELECT * FROM student WHERE MONTH(DATE_FORMAT(NOW(),'%Y%m%d'))+1 = MONTH(s_birth)

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马