mySQL经典面试题


---------创建数据库、表、插入数据----------------------
 
-- 建表
-- 学生表
CREATE TABLE Student(
    s_id VARCHAR(20) COMMENT '学生编号',
    s_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '学生姓名',
    s_birth VARCHAR(20) NOT NULL DEFAULT '' COMMENT '出生年月',
    s_sex VARCHAR(10) NOT NULL DEFAULT '' COMMENT '学生性别',
    PRIMARY KEY(s_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '学生表';
-- 课程表
CREATE TABLE Course(
    c_id  VARCHAR(20) COMMENT '课程编号',
    c_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '课程名称',
    t_id VARCHAR(20) NOT NULL COMMENT '教师编号',
    PRIMARY KEY(c_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '课程表';
-- 教师表
CREATE TABLE Teacher(
    t_id VARCHAR(20) COMMENT '教师编号',
    t_name VARCHAR(20) NOT NULL DEFAULT '' COMMENT '教师姓名',
    PRIMARY KEY(t_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '教师表';
-- 成绩表
CREATE TABLE Score(
    s_id VARCHAR(20) COMMENT '学生编号',
    c_id  VARCHAR(20) COMMENT '课程编号',
    s_score INT(3) COMMENT '分数',
    PRIMARY KEY(s_id,c_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '成绩表';
 
-- 插入学生表测试数据
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);
 
--------------------------------------------------
 
-- 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT
    a.*, b.s_score AS score1,
    c.s_score AS score2
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
AND b.c_id = '01'
LEFT JOIN score c ON a.s_id = c.s_id
AND (c.c_id = '02' OR c.c_id =NULL)
WHERE
    b.s_score > c.s_score ;
 
-- 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT
    a.*, b.s_score AS score1,
    c.s_score AS score2
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
AND (b.c_id = '01' OR b.c_id =NULL)
LEFT JOIN score c ON a.s_id = c.s_id
AND c.c_id = '02' 
WHERE
    b.s_score < c.s_score ;
 
-- 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT
    a.s_id,
    a.s_name,
    ROUND(AVG(b.s_score), 1) AS 平均成绩
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY
    a.s_id
HAVING
    AVG(b.s_score) >= 60;
 
-- 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
-- (包括有成绩的和无成绩的)
-- 方法一:
SELECT
    a.s_id,
    a.s_name,
    ROUND(AVG(b.s_score), 1) AS 平均成绩
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY
    a.s_id
HAVING
    AVG(b.s_score) < 60
OR
a.s_id NOT IN(SELECT DISTINCT a.s_id FROM student a JOIN score b WHERE a.s_id=b.s_id);
 
-- 方法二:
SELECT
    a.s_id,a.s_name,
    ROUND(AVG(b.s_score),2) AS avg_score
FROM
    student a
JOIN score b ON a.s_id = b.s_id
GROUP BY
    a.s_id
HAVING AVG(b.s_score) < 60
UNION
SELECT a.s_id,a.s_name,0 AS avg_score FROM 
student a
WHERE a.s_id NOT IN (SELECT DISTINCT s_id FROM score);
 
-- 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩,并从高到低排序
SELECT
    a.s_id,
    a.s_name,
    COUNT(b.c_id) AS 选课总数 ,
    SUM(b.s_score) AS 总成绩
FROM
    student a
LEFT JOIN score b
ON
    a.s_id = b.s_id
GROUP BY a.s_id
ORDER BY SUM(b.s_score) DESC;
 
-- 6、查询"李"姓老师的数量 
SELECT COUNT(t_id) FROM teacher WHERE t_name LIKE '李%';
 
-- 7、查询学过"张三"老师授课的同学的信息 
#张三编号
SELECT t_id FROM teacher WHERE t_name = '张三'
#张三代课的课程编号
SELECT c_id FROM course WHERE t_id = (SELECT t_id FROM teacher WHERE t_name = '张三')
#学张三课程的学生编号
SELECT s_id FROM score WHERE c_id = (SELECT c_id FROM course WHERE t_id = (SELECT t_id FROM teacher WHERE t_name = '张三'))
-- 方法一:
SELECT *FROM student WHERE 
s_id IN (SELECT s_id FROM score WHERE 
c_id = (SELECT c_id FROM course WHERE
t_id = (SELECT t_id FROM teacher WHERE t_name = '张三'))
);
 
-- 方法二:
SELECT a.* FROM student a
JOIN score b ON a.s_id = b.s_id
WHERE 
b.c_id IN (SELECT c_id FROM course WHERE t_id = (SELECT t_id FROM teacher WHERE t_name = '张三'));
 
-- 8、查询没学过"张三"老师授课的同学的信息 
-- 方法一:
SELECT *FROM student WHERE 
s_id NOT IN (SELECT s_id FROM score WHERE 
c_id = (SELECT c_id FROM course WHERE
t_id = (SELECT t_id FROM teacher WHERE t_name = '张三'))
);
 
-- 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息,及两门课程成绩
-- 方法一
SELECT
    a.*, b.s_score,
    c.s_score 
FROM
    student a
JOIN score b ON a.s_id = b.s_id
AND b.c_id = '01'
JOIN score c ON a.s_id = c.s_id
AND c.c_id = '02';
 
-- 方法二:
SELECT
    a.*, b.s_score,
    c.s_score
FROM
    student a,
    score b,
    score c
WHERE
    a.s_id = b.s_id
AND a.s_id = c.s_id
AND b.c_id = '01'
AND c.c_id = '02';
 
-- 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT s_id FROM score  WHERE c_id = '01'
SELECT s_id FROM score  WHERE c_id = '02'
 
SELECT *FROM student a
WHERE
a.s_id IN (SELECT s_id FROM score  WHERE c_id = '01')
AND a.s_id NOT IN (SELECT s_id FROM score  WHERE c_id = '02');
 
 
-- 11、查询没有学全所有课程的同学的信息 
 
SELECT s_id FROM score
GROUP BY s_id
HAVING COUNT(s_id) != 3
 
#方法一:
SELECT *FROM student WHERE s_id IN(
    SELECT s_id FROM score
    GROUP BY s_id
    HAVING COUNT(s_id) != 3
);
 
#方法二:
SELECT a.s_id FROM score a 
    JOIN score b ON a.s_id=b.s_id AND b.c_id='02'
    JOIN score c ON a.s_id=c.s_id AND c.c_id='03'
    WHERE a.c_id='01'
 
SELECT *FROM student d WHERE d.s_id IN(
    SELECT e.s_id FROM score e WHERE e.s_id NOT IN(
        SELECT a.s_id FROM score a 
            JOIN score b ON a.s_id=b.s_id AND b.c_id='02'
            JOIN score c ON a.s_id=c.s_id AND c.c_id='03'
            WHERE a.c_id='01')
);
 
#--------------
 
# 上述两种方法结果都少了没有选课的8号学生,但看具体条件是否需要查出
 
# 学全选取所有课程的同学的id 
SELECT s_id FROM score
    GROUP BY s_id
    HAVING COUNT(s_id) = 3
 
#方法一:
SELECT *FROM student WHERE s_id NOT IN(
    SELECT s_id FROM score
    GROUP BY s_id
    HAVING COUNT(s_id) = 3
);
 
-- 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息 
 
SELECT c_id FROM score WHERE s_id ='01'
 
SELECT DISTINCT s_id FROM score WHERE c_id IN (SELECT c_id FROM score WHERE s_id ='01')
 
SELECT * FROM student a
WHERE
a.s_id IN (SELECT DISTINCT b.s_id FROM score b WHERE 
b.c_id IN (SELECT c.c_id FROM score c WHERE c.s_id ='01')
);
 
-- 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息 
 
SELECT * FROM student WHERE s_id IN(
    SELECT DISTINCT s_id FROM score WHERE s_id!='01' AND c_id IN (SELECT c_id FROM score WHERE s_id ='01') 
    GROUP BY s_id
    HAVING COUNT(1)=(SELECT COUNT(1) FROM score WHERE s_id='01')
)
 
-- 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 
SELECT s_id FROM score WHERE s_score<60 GROUP BY s_id HAVING COUNT(1) >= 2
 
SELECT
    a.s_id,
    a.s_name,
    ROUND(AVG(b.s_score), 1) AS 平均成绩 
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
GROUP BY a.s_id
HAVING
a.s_id IN(SELECT s_id FROM score WHERE s_score<60 GROUP BY s_id HAVING COUNT(1) >= 2)
 
 
-- 16、检索"01"课程分数小于60,按分数降序排列的学生信息及01分数
-- 方法一:
SELECT
    a.*, b.s_score
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
WHERE
    b.c_id = '01'
AND b.s_score < 60
ORDER BY
    b.s_score DESC;
 
-- 方法二:
SELECT
    a.*, b.s_score
FROM
    student a,score b
WHERE
a.s_id = b.s_id AND b.c_id='01' AND b.s_score < 60
ORDER BY
    b.s_score DESC;
 
-- 方法三(有点瑕疵):
SELECT
    a.*, b.s_score
FROM
    student a
LEFT JOIN score b ON a.s_id = b.s_id
AND    b.c_id = '01'
AND b.s_score < 60
ORDER BY
    b.s_score DESC;
 
-- 17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
 
-- 方法一(自连接):
SELECT
 a.s_id,
(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id='01') AS score1,
(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id='02') AS score2,
(SELECT s_score FROM score WHERE s_id=a.s_id AND c_id='03') AS score3,
ROUND(avg(a.s_score), 2) AS 平均分
FROM score a
GROUP BY a.s_id
ORDER BY 平均分 DESC;
 
-- 方法二(自连接):
SELECT 
 a.s_id,
 b.s_score,
 c.s_score,
 d.s_score,
 ROUND(avg(a.s_score), 2) AS 平均分
FROM
    score a
LEFT JOIN score b ON a.s_id = b.s_id AND b.c_id='01'
LEFT JOIN score c ON a.s_id = c.s_id AND c.c_id='02'
LEFT JOIN score d ON a.s_id = d.s_id AND d.c_id='03'
GROUP BY a.s_id
ORDER BY 平均分 DESC;
 
-- 18.查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
-- 及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
 
SELECT 
a.c_id,
MAX(a.s_score),
 MIN(a.s_score),
 AVG(a.s_score)
FROM
    score a
GROUP BY a.c_id;
 
-- 方法一:
SELECT
    a.c_id AS 课程ID,
    b.c_name AS 课程name,
    MAX(a.s_score) AS 最高分,
    MIN(a.s_score) AS 最低分,
    ROUND(AVG(a.s_score),2) AS 平均分,
  ROUND(100*(SUM(CASE WHEN a.s_score >= 60 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '及格率',
    ROUND(100*(SUM(CASE WHEN a.s_score >= 70 AND  a.s_score <80 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '中等率',
  ROUND(100*(SUM(CASE WHEN a.s_score >= 80 AND  a.s_score <90 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '优良率',
  ROUND(100*(SUM(CASE WHEN a.s_score >= 90 THEN 1 ELSE 0 END) / COUNT(1)) , 2)  AS '优秀率'
FROM
    score a
LEFT JOIN course b ON a.c_id = b.c_id
GROUP BY
    b.c_id;
 
-- 19、按各科成绩进行排序,并显示排名
-- mysql没有rank顺序函数
 select a.s_id,a.c_id,
        @i:=@i +1 as i保留排名,
        @k:=(case when @score=a.s_score then @k else @i end) as rank不保留排名,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score WHERE c_id='01' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
    union
    select a.s_id,a.c_id,
        @i:=@i +1 as i,
        @k:=(case when @score=a.s_score then @k else @i end) as rank,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score WHERE c_id='02' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
    union
    select a.s_id,a.c_id,
        @i:=@i +1 as i,
        @k:=(case when @score=a.s_score then @k else @i end) as rank,
        @score:=a.s_score as score
    from (
        select s_id,c_id,s_score from score WHERE c_id='03' GROUP BY s_id,c_id,s_score ORDER BY s_score DESC
)a,(select @k:=0,@i:=0,@score:=0)s
 
-- 20、查询学生的总成绩并进行排名
SELECT a.s_id,
    @i:=@i+1 AS i,
    @k:=(CASE WHEN @score=a.sum_score THEN @k ELSE @i END) AS rank,
    @score:=a.sum_score AS score
FROM (SELECT s_id,SUM(s_score) AS sum_score FROM score GROUP BY s_id ORDER BY sum_score DESC) AS a,
(SELECT @i:=0,@score:=0) AS b
 
-- 21、查询不同老师所教不同课程平均分从高到低显示 
SELECT
    a.t_name,
    b.c_id,
    b.c_name,
    ROUND(AVG(c.s_score) ,2) AS 平均分
FROM
    teacher a
LEFT JOIN course b ON a.t_id = b.t_id
LEFT JOIN score c ON b.c_id=c.c_id
GROUP BY c.c_id
ORDER BY AVG(c.s_score) DESC;
 
-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
SELECT c.*,d.s_name,d.s_birth,d.s_sex FROM 
(SELECT a.s_id,a.s_score,a.c_id,@i:=@i+1 AS 排名 FROM score a,(SELECT @i:=0)b WHERE a.c_id='01') c 
LEFT JOIN student d ON c.s_id=d.s_id
WHERE 排名 BETWEEN 2 AND 3
UNION
SELECT c.*,d.s_name,d.s_birth,d.s_sex FROM 
(SELECT a.s_id,a.s_score,a.c_id,@j:=@j+1 AS 排名 FROM score a,(SELECT @j:=0)b WHERE a.c_id='02') c 
LEFT JOIN student d ON c.s_id=d.s_id
WHERE 排名 BETWEEN 2 AND 3
UNION
SELECT c.*,d.s_name,d.s_birth,d.s_sex FROM 
(SELECT a.s_id,a.s_score,a.c_id,@k:=@k+1 AS 排名 FROM score a,(SELECT @k:=0)b WHERE a.c_id='03') c 
LEFT JOIN student d ON c.s_id=d.s_id
WHERE 排名 BETWEEN 2 AND 3;
 
-- 23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比
  
SELECT
    a.c_id AS 课程编号, a.c_name AS 课程名称,
  c.`[100-85]的人数`, c.`[100-85]所占百分比`,
    d.`[85-70]的人数`, d.`[85-70]所占百分比`,
    e.`[70-60]的人数`, e.`[70-60]所占百分比`,
    f.`[0-60]的人数`, f.`[0-60]所占百分比`
FROM
    course a
LEFT JOIN score b ON a.c_id = b.c_id
LEFT JOIN
    (SELECT *,SUM(CASE WHEN s_score >85 AND s_score <=100 THEN 1 ELSE 0 END) AS '[100-85]的人数' ,
    ROUND(SUM(CASE WHEN s_score >85 AND s_score <=100 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[100-85]所占百分比'
    FROM score GROUP BY c_id) c ON a.c_id=c.c_id
LEFT JOIN
    (SELECT*,SUM(CASE WHEN s_score >70 AND s_score <=85 THEN 1 ELSE 0 END) AS '[85-70]的人数' ,
    ROUND(SUM(CASE WHEN s_score >70 AND s_score <=85 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[85-70]所占百分比'
    FROM score GROUP BY c_id) d ON a.c_id=d.c_id
LEFT JOIN
    (SELECT*,SUM(CASE WHEN s_score >60 AND s_score <=70 THEN 1 ELSE 0 END) AS '[70-60]的人数' ,
    ROUND(SUM(CASE WHEN s_score >60 AND s_score <=70 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[70-60]所占百分比'
    FROM score GROUP BY c_id) e ON a.c_id=e.c_id
LEFT JOIN
    (SELECT *,SUM(CASE WHEN s_score >0 AND s_score <=60 THEN 1 ELSE 0 END) AS '[0-60]的人数' ,
    ROUND(SUM(CASE WHEN s_score >0 AND s_score <=60 THEN 1 ELSE 0 END)/COUNT(1)*100,2) AS '[0-60]所占百分比'
    FROM score GROUP BY c_id) f ON a.c_id=f.c_id
GROUP BY a.c_id
 
-- 24、查询学生平均成绩及其名次 
SELECT
    b.s_id,
    @i:=@i+1 AS 相同分数的不同名次,
    @k:=(CASE WHEN @avg_s=b.avg_score THEN @k ELSE @i END) AS 相同分数的相同名次,
    @avg_s:=b.avg_score AS 平均成绩
FROM
(SELECT
    a.s_id,
    ROUND(AVG(a.s_score), 2) AS avg_score
FROM
    score a
GROUP BY
    a.s_id
ORDER BY AVG(a.s_score) DESC) b,(SELECT @i:=0,@avg_s:=0,@k:=0) c
 
 
-- 24.1添加名次rank,(相同分数的相同名次,并列排名)
-- 上面24难以看出并列排名
SELECT
    b.s_id,    b.c_id,
    -- 顺序一直在变大
    @i:=@i+1 AS 相同分数的不同名次,
  -- 只有在前后二次分数不同时才会使用顺序号
    @k:=(CASE WHEN @s=b.s_score THEN @k ELSE @i END) AS 相同分数的相同名次,
    @s:=b.s_score AS 成绩
FROM
(SELECT *FROM score  WHERE s_id='03' ORDER BY s_score DESC)b,
(SELECT @i:=0,@k:=0,@s:=0)c
 
-- 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(b.s_id)<3
-- ORDER BY a.c_id,a.s_score DESC
 
-- 26、查询每门课程被选修的学生数 
SELECT c_id,COUNT(1) FROM  score GROUP BY c_id
 
-- 27、查询出只有两门课程的全部学生的学号和姓名 
-- 方法一:
SELECT a.s_id,a.s_name FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id
GROUP BY a.s_id
HAVING COUNT(1)=2
 
-- 方法二:
SELECT a.s_id,a.s_name FROM student a WHERE a.s_id IN 
(SELECT s_id FROM score GROUP BY s_id HAVING COUNT(1)=2)
 
-- 28、查询男生、女生人数 
SELECT s_sex,COUNT(1) FROM student GROUP BY s_sex
 
-- 29、查询名字中含有"风"字的学生信息
SELECT *FROM student WHERE s_name LIKE '%风%' 
 
-- 30、查询同名同性学生名单,并统计同名人数 
SELECT a.s_name,a.s_sex,COUNT(1) AS 人数 FROM student a 
JOIN student b ON a.s_name=b.s_name AND a.s_sex=b.s_sex AND a.s_id!=b.s_id
GROUP BY a.s_name,a.s_sex
 
-- 31、查询1990年出生的学生名单
-- 方法一
SELECT s_name FROM student WHERE YEAR(s_birth)='1990'
-- 方法二
SELECT s_name FROM student WHERE s_birth LIKE '1990%'
 
-- 32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
SELECT c_id,ROUND(avg(s_score),2)FROM score 
GROUP BY c_id
ORDER BY avg(s_score) DESC,c_id ASC 
 
-- 33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩 
SELECT a.s_id,a.s_name,ROUND(avg(b.s_score),2) AS 平均成绩 FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id
GROUP BY a.s_id
HAVING avg(b.s_score) >= 85
 
-- 34、查询课程名称为"数学",且分数低于60的学生姓名和分数 
SELECT a.s_name,b.s_score FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id
WHERE c_id=(
SELECT c_id FROM course WHERE c_name='数学'
) AND b.s_score < 60
 
-- 35、查询所有学生的课程及分数情况; 
-- 方法一:
SELECT a.s_id,a.s_name,b.s_score,c.s_score,d.s_score FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id AND b.c_id='01'
LEFT JOIN score c ON a.s_id=c.s_id AND c.c_id='02'
LEFT JOIN score d ON a.s_id=d.s_id AND d.c_id='03'
GROUP BY a.s_id
 
-- 方法二:
 SELECT a.s_id,a.s_name,
SUM(CASE c.c_name WHEN '语文' THEN b.s_score ELSE 0 END) AS '语文',
SUM(CASE c.c_name WHEN '数学' THEN b.s_score ELSE 0 END) AS '数学',
SUM(CASE c.c_name WHEN '英语' THEN b.s_score ELSE 0 END) AS '英语',
SUM(b.s_score) as  '总分'
FROM student a 
LEFT JOIN score b ON a.s_id = b.s_id 
LEFT JOIN course c ON b.c_id = c.c_id 
GROUP BY a.s_id,a.s_name
 
 -- 36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数; 
 
SELECT a.s_name,c.c_name,b.s_score FROM student a
LEFT JOIN score b ON a.s_id=b.s_id
LEFT JOIN course c ON b.c_id=c.c_id
HAVING b.s_score > 70
 
-- 37、查询不及格的学生id,姓名,及其课程名称,分数
SELECT a.s_id,a.s_name,c.c_name,b.s_score FROM student a 
LEFT JOIN score b ON a.s_id=b.s_id
LEFT JOIN course c ON b.c_id=c.c_id
WHERE b.s_score < 60
 
 
-- 38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名; 
SELECT b.s_id,b.s_name FROM score a
LEFT JOIN student b ON a.s_id=b.s_id
WHERE a.c_id='01' AND a.s_score>80
 
-- 39、求每门课程的学生人数 
SELECT c_id,COUNT(1) FROM score GROUP BY c_id
 
 
-- 40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
 
SELECT t_id FROM teacher a WHERE a.t_name='张三'
 
SELECT c_id FROM course b WHERE t_id=(SELECT t_id FROM teacher a WHERE a.t_name='张三')
 
SELECT c.*,d.s_score FROM student c
LEFT JOIN score d ON c.s_id=d.s_id AND d.c_id=
(SELECT c_id FROM course b WHERE t_id=(SELECT t_id FROM teacher a WHERE a.t_name='张三'))
HAVING  MAX(d.s_score)
 
 
-- 41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩 
SELECT DISTINCT a.s_id,a.c_id,a.s_score 
FROM score a,score b WHERE a.s_score=b.s_score AND a.c_id!=b.c_id
 
-- 42、查询每门功课成绩最好的前两名 
-- 方法一
SELECT *FROM
(SELECT a.s_id,a.c_id,a.s_score, @i:=@i+1 as 排名 FROM score a,(SELECT @i:=0)b WHERE a.c_id='01' ORDER BY a.s_score DESC
) c
WHERE 排名 BETWEEN 1 AND 2
UNION
SELECT *FROM
(SELECT a.s_id,a.c_id,a.s_score, @j:=@j+1 as 排名 FROM score a,(SELECT @j:=0)b WHERE a.c_id='02' ORDER BY a.s_score DESC
) c
WHERE 排名 BETWEEN 1 AND 2
UNION
SELECT *FROM
(SELECT a.s_id,a.c_id,a.s_score, @k:=@k+1 as 排名 FROM score a,(SELECT @k:=0)b WHERE a.c_id='03' ORDER BY a.s_score DESC
) c
WHERE 排名 BETWEEN 1 AND 2
 
-- 方法二
-- 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
 
-- 43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列  
SELECT c_id AS 课程号,COUNT(1) AS 选修人数 FROM score 
GROUP BY c_id 
HAVING COUNT(1)>5 
ORDER BY COUNT(1) DESC,c_id
 
-- 44、检索至少选修两门课程的学生学号 
SELECT s_id,COUNT(1) FROM score GROUP BY s_id HAVING COUNT(1)>=2
 
 
-- 45、查询选修了全部课程的学生信息 
SELECT COUNT(1) FROM course
 
SELECT b.* FROM score a
LEFT JOIN student b ON a.s_id=b.s_id
GROUP BY a.s_id 
HAVING COUNT(1)=(SELECT COUNT(1) FROM course)
 
-- 46、查询各学生的年龄
 -- 按照出生日期来算,当前月日<出生年月的月日则,年龄减一
-- 方法一
SELECT s_id,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 1 ELSE 0 END) AS age
FROM student
 
-- 47、查询本周过生日的学生
-- 方法一
SELECT * FROM student WHERE WEEK(CURRENT_DATE)=WEEK(s_birth)
-- 方法二
SELECT * FROM student WHERE WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))=WEEK(s_birth)
 
-- 48、查询下周过生日的学生
SELECT * FROM student WHERE WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))+1=WEEK(s_birth)
 
-- 49、查询本月过生日的学生
SELECT * FROM student WHERE MONTH(NOW())=MONTH(s_birth)
 
-- 50、查询下月过生日的学生
SELECT * FROM student WHERE MONTH(NOW())+1=MONTH(s_birth)
 
SELECT DATE_FORMAT(NOW(),'%Y')
SELECT DATE_FORMAT(NOW(),'%Y%m%d')
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容