Mysql练习题

Mysql一套完整练习题

测试表格

复制代码
--1.学生表
Student(S#,Sname,Sage,Ssex)
--S# 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别
--2.课程表 
Course(C#,Cname,T#)
--C# --课程编号,Cname 课程名称,T# 教师编号
--3.教师表 
Teacher(T#,Tname)
--T# 教师编号,Tname 教师姓名
--4.成绩表 
SC(S#,C#,score)
--S# 学生编号,C# 课程编号,score 分数
复制代码

创建测试数据

 

学生表 Student

复制代码
create table Student(S# varchar(10),Sname nvarchar(10),Sage datetime,Ssex nvarchar(10))
insert into Student values('01' , N'赵雷' , '1990-01-01' , N'男')
insert into Student values('02' , N'钱电' , '1990-12-21' , N'男')
insert into Student values('03' , N'孙风' , '1990-05-20' , N'男')
insert into Student values('04' , N'李云' , '1990-08-06' , N'男')
insert into Student values('05' , N'周梅' , '1991-12-01' , N'女')
insert into Student values('06' , N'吴兰' , '1992-03-01' , N'女')
insert into Student values('07' , N'郑竹' , '1989-07-01' , N'女')
insert into Student values('08' , N'王菊' , '1990-01-20' , N'女')
复制代码

科目表 Course

create table Course(C# varchar(10),Cname nvarchar(10),T# varchar(10))
insert into Course values('01' , N'语文' , '02')
insert into Course values('02' , N'数学' , '01')
insert into Course values('03' , N'英语' , '03')

教师表 Teacher

create table Teacher(T# varchar(10),Tname nvarchar(10))
insert into Teacher values('01' , N'张三')
insert into Teacher values('02' , N'李四')
insert into Teacher values('03' , N'王五')

成绩表 SC

复制代码
create table SC(S# varchar(10),C# varchar(10),score decimal(18,1))
insert into SC values('01' , '01' , 80)
insert into SC values('01' , '02' , 90)
insert into SC values('01' , '03' , 99)
insert into SC values('02' , '01' , 70)
insert into SC values('02' , '02' , 60)
insert into SC values('02' , '03' , 80)
insert into SC values('03' , '01' , 80)
insert into SC values('03' , '02' , 80)
insert into SC values('03' , '03' , 80)
insert into SC values('04' , '01' , 50)
insert into SC values('04' , '02' , 30)
insert into SC values('04' , '03' , 20)
insert into SC values('05' , '01' , 76)
insert into SC values('05' , '02' , 87)
insert into SC values('06' , '01' , 31)
insert into SC values('06' , '03' , 34)
insert into SC values('07' , '02' , 89)
insert into SC values('07' , '03' , 98)
复制代码

练习题

复制代码
1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
1.1 查询同时存在" 01 "课程和" 02 "课程的情况
1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
3. 查询在 SC 表存在成绩的学生信息
4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
4.1 查有成绩的学生信息
5. 查询「李」姓老师的数量
6. 查询学过「张三」老师授课的同学的信息
7. 查询没有学全所有课程的同学的信息
8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
9. 查询和" 01 "号的同学学习的课程完全相同的其他同学的信息
10. 查询没学过"张三"老师讲授的任一门课程的学生姓名
11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
12. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
14. 查询各科成绩最高分、最低分和平均分:
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
16. 查询学生的总成绩,并进行排名,总分重复时保留名次空缺
16.1 查询学生的总成绩,并进行排名,总分重复时不保留名次空缺
17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
18. 查询各科成绩前三名的记录
19. 查询每门课程被选修的学生数
20. 查询出只选修两门课程的学生学号和姓名
21. 查询男生、女生人数
22. 查询名字中含有「风」字的学生信息
23. 查询同名同性学生名单,并统计同名人数
24. 查询 1990 年出生的学生名单
25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
30. 查询不及格的课程
31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
32. 求每门课程的学生人数
33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
36. 查询每门功成绩最好的前两名
37. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。
38. 检索至少选修两门课程的学生学号
39. 查询选修了全部课程的学生信息
40. 查询各学生的年龄,只按年份来算
41. 按照出生日期来算,当前月日 < 出生年月的月日则,年龄减一
42. 查询本周过生日的学生信息
43. 查询下周过生日的学生信息
44. 查询本月过生日的学生信息
45. 查询下月过生日的学生信息
复制代码

答案参考

 

创建表

mysql -u root -p 密码;进入mysql

复制代码
show databases;查看数据库
create database test;创建数据库
use test; --表在 test 中创建

-- 学生表 Student create table Student ( S char(10), Sname char(10), Sage datetime, Ssex char(10) ); insert into Student values('01' , N'赵雷' , '1990-01-01' , N'男'); insert into Student values('02' , N'钱电' , '1990-12-21' , N'男'); insert into Student values('03' , N'孙风' , '1990-05-20' , N'男'); insert into Student values('04' , N'李云' , '1990-08-06' , N'男'); insert into Student values('05' , N'周梅' , '1991-12-01' , N'女'); insert into Student values('06' , N'吴兰' , '1992-03-01' , N'女'); insert into Student values('07' , N'郑竹' , '1989-07-01' , N'女'); insert into Student values('08' , N'王菊' , '1990-01-20' , N'女'); -- 科目表 Course create table Course ( C char(10), Cname char(10), T char(10) ); insert into Course values('01' , N'语文' , '02'); insert into Course values('02' , N'数学' , '01'); insert into Course values('03' , N'英语' , '03'); -- 教师表 Teacher create table Teacher ( T char(10), Tname char(10) ); insert into Teacher values('01' , N'张三'); insert into Teacher values('02' , N'李四'); insert into Teacher values('03' , N'王五'); -- 成绩表 SC create table SC ( S char(10), C char(10), score decimal(18,1) ); insert into SC values('01' , '01' , 80); insert into SC values('01' , '02' , 90); insert into SC values('01' , '03' , 99); insert into SC values('02' , '01' , 70); insert into SC values('02' , '02' , 60); insert into SC values('02' , '03' , 80); insert into SC values('03' , '01' , 80); insert into SC values('03' , '02' , 80); insert into SC values('03' , '03' , 80); insert into SC values('04' , '01' , 50); insert into SC values('04' , '02' , 30); insert into SC values('04' , '03' , 20); insert into SC values('05' , '01' , 76); insert into SC values('05' , '02' , 87); insert into SC values('06' , '01' , 31); insert into SC values('06' , '03' , 34); insert into SC values('07' , '02' , 89); insert into SC 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);
 
-- 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')
复制代码

 结束

posted @ 2020-11-05 18:28  奥斯科  阅读(254)  评论(0编辑  收藏  举报