SQL语句练习题

参考资料:
https://blog.csdn.net/fashion2014/article/details/78826299
https://blog.csdn.net/paul0127/article/details/82529216
https://www.cnblogs.com/coder-wf/p/11128033.html
https://www.cnblogs.com/clwsec/p/11615615.html
https://www.cnblogs.com/qixuejia/p/3637735.html
https://zhuanlan.zhihu.com/p/43289968
https://blog.csdn.net/flycat296/article/details/63681089

表名和字段

–1.学生表
Student(s_id,s_name,s_birth,s_sex) --学生编号,学生姓名, 出生年月,学生性别
–2.课程表
Course(c_id,c_name,t_id) – --课程编号, 课程名称, 教师编号
–3.教师表
Teacher(t_id,t_name) --教师编号,教师姓名
–4.成绩表
Score(s_id,c_id,s_score) --学生编号,课程编号,分数

测试数据

#学生表
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);

练习题

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

SELECT stu.*,sco_1.s_score as s_score_01,sco_2.s_score as s_score_02 FROM Student as stu 
LEFT JOIN Score as sco_1 ON stu.s_id = sco_1.s_id AND sco_1.c_id = '01' 
LEFT JOIN Score as sco_2 ON stu.s_id = sco_2.s_id AND sco_2.c_id = '02' 
where sco_1.s_score>sco_2.s_score;

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

SELECT stu.*,sco_1.s_score as s_score_01,sco_2.s_score as s_score_02 FROM Student as stu 
LEFT JOIN Score as sco_1 ON stu.s_id = sco_1.s_id AND sco_1.c_id = '01' 
LEFT JOIN Score as sco_2 ON stu.s_id = sco_2.s_id AND sco_2.c_id = '02' 
where sco_1.s_score<sco_2.s_score;

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

SELECT stu.s_id,stu.s_name,ROUND(AVG(sco.s_score),2) as avg_score FROM Student as stu 
LEFT JOIN Score as sco ON stu.s_id = sco.s_id WHERE sco.s_score IS NOT NULL 
GROUP BY stu.s_id HAVING avg_score <60;

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)

SELECT stu_1.s_id,stu_1.s_name,ROUND(AVG(sco.s_score),2) AS avg_score FROM Student stu_1 
LEFT JOIN Score sco on stu_1.s_id = sco.s_id 
GROUP BY stu_1.s_id HAVING avg_score <60 
UNION
SELECT s_id,s_name,0 AS avg_score FROM Student WHERE s_id NOT IN (SELECT DISTINCT s_id FROM Score);

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

SELECT stu.s_id,stu.s_name,count(sco.c_id) AS sum_course,sum(sco.s_score) AS sum_score FROM Student stu 
LEFT JOIN Score sco on stu.s_id=sco.s_id GROUP BY stu.s_id;

6、查询"李"姓老师的数量

SELECT count(t_id) FROM Teacher WHERE t_name LIKE '李%';

7、查询学过"张三"老师授课的同学的信息

SELECT stu.* FROM Student as stu JOIN Score AS sco ON stu.s_id = sco.s_id 
WHERE sco.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 stu.s_id FROM Student stu join Score sco on stu.s_id=sco.s_id where sco.c_id IN(
SELECT cou.c_id FROM Course cou JOIN Teacher tea ON cou.t_id = tea.t_id WHERE tea.t_name ='张三'));

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

SELECT stu.* FROM Student stu,Score sco_1,Score sco_2 
WHERE stu.s_id = sco_1.s_id AND stu.s_id = sco_2.s_id AND sco_1.c_id = '01' AND sco_2.c_id = '02';

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

SELECT stu.* FROM Student stu WHERE stu.s_id IN (SELECT s_id FROM Score WHERE c_id='01' ) 
AND stu.s_id NOT IN(SELECT s_id from Score where c_id='02');

11、查询没有学全所有课程的同学的信息

SELECT stu.* FROM Student stu LEFT JOIN Score sco ON stu.s_id=sco.s_id
GROUP BY sco.s_id HAVING count(sco.c_id)<(SELECT count(*) FROM Course);

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

SELECT stu.* FROM Student stu WHERE stu.s_id IN(
SELECT DISTINCT sco_1.s_id FROM Score sco_1 where sco_1.c_id IN(
SELECT sco_2.c_id from Score sco_2 where sco_2.s_id='01'));

13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

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

14、查询没学过"张三"老师讲授的任一门课程的学生姓名

SELECT a.s_name FROM Student a WHERE a.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 = '张三')));

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

SELECT stu.s_id,stu.s_name,ROUND(AVG(sco.s_score),2) FROM Student stu LEFT JOIN Score sco ON stu.s_id = sco.s_id 
WHERE sco.s_score IS NOT NULL AND sco.s_id IN(
SELECT s_id FROM Score WHERE s_score<60 GROUP BY s_id having count(*)>=2)GROUP BY sco.s_id

16、检索"01"课程分数小于60,按分数降序排列的学生信息

SELECT stu.*,sco.c_id,sco.s_score FROM Student stu,Score sco 
WHERE stu.s_id = sco.s_id and sco.c_id='01' and sco.s_score<60 ORDER BY sco.s_score DESC;

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

SELECT a.s_id,(SELECT s_score FROM Score WHERE s_id=a.s_id AND c_id='01') AS 语文,
(SELECT s_score FROM Score WHERE s_id=a.s_id AND c_id='02') AS 数学,
(SELECT s_score FROM Score WHERE s_id=a.s_id AND c_id='03') AS 英语,
round(avg(s_score),2) as 平均分 FROM Score a  GROUP BY a.s_id ORDER BY 平均分 DESC;

18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率,及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

SELECT a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2),
ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率,
ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率,
ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率,
ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率
FROM Score a LEFT JOIN Course b ON a.c_id = b.c_id GROUP BY a.c_id,b.c_name

19、按各科成绩进行排序,并显示排名

(select * from (select 
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from Score t2 where t2.s_score>=t1.s_score and t2.c_id='01') rank
FROM Score t1 where t1.c_id='01'
order by t1.s_score desc) t1)
union
(select * from (select 
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from Score t2 where t2.s_score>=t1.s_score and t2.c_id='02') rank
FROM Score t1 where t1.c_id='02'
order by t1.s_score desc) t2)
union
(select * from (select 
t1.c_id,
t1.s_score,
(select count(distinct t2.s_score) from Score t2 where t2.s_score>=t1.s_score and t2.c_id='03') rank
FROM Score t1 where t1.c_id='03'
order by t1.s_score desc) t3)

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)a,
    (select @k:=0,@i:=0,@score:=0)s

21、查询不同老师所教不同课程平均分从高到低显示

SELECT a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score from Course a
        left join Score b on a.c_id=b.c_id 
        left join Teacher c on a.t_id=c.t_id
        GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC;

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

select d.*,c.排名,c.s_score,c.c_id from (
                select a.s_id,a.s_score,a.c_id,@i:=@i+1 as 排名 from Score a,(select @i:=0)s where a.c_id='01'  
                                ORDER BY a.s_score DESC  
            )c
            left join Student d on c.s_id=d.s_id
            where 排名 BETWEEN 2 AND 3
            UNION
            select d.*,c.排名,c.s_score,c.c_id from (
                select a.s_id,a.s_score,a.c_id,@j:=@j+1 as 排名 from Score a,(select @j:=0)s where a.c_id='02'  
                                ORDER BY a.s_score DESC
            )c
            left join Student d on c.s_id=d.s_id
            where 排名 BETWEEN 2 AND 3
            UNION
            select d.*,c.排名,c.s_score,c.c_id from (
                select a.s_id,a.s_score,a.c_id,@k:=@k+1 as 排名 from Score a,(select @k:=0)s where a.c_id='03' 
                                ORDER BY a.s_score DESC
            )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 distinct f.c_name,a.c_id,b.`85-100`,b.百分比,c.`70-85`,c.百分比,d.`60-70`,d.百分比,e.`0-60`,e.百分比 from Score a
                left join (select c_id,SUM(case when s_score >85 and s_score <=100 then 1 else 0 end) as `85-100`,
                                            ROUND(100*(SUM(case when s_score >85 and s_score <=100 then 1 else 0 end)/count(*)),2) as 百分比
                                from Score GROUP BY c_id)b on a.c_id=b.c_id
                left join (select c_id,SUM(case when s_score >70 and s_score <=85 then 1 else 0 end) as `70-85`,
                                            ROUND(100*(SUM(case when s_score >70 and s_score <=85 then 1 else 0 end)/count(*)),2) as 百分比
                                from Score GROUP BY c_id)c on a.c_id=c.c_id
                left join (select c_id,SUM(case when s_score >60 and s_score <=70 then 1 else 0 end) as `60-70`,
                                            ROUND(100*(SUM(case when s_score >60 and s_score <=70 then 1 else 0 end)/count(*)),2) as 百分比
                                from Score GROUP BY c_id)d on a.c_id=d.c_id
                left join (select c_id,SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end) as `0-60`,
                                            ROUND(100*(SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end)/count(*)),2) as 百分比
                                from Score GROUP BY c_id)e on a.c_id=e.c_id
                left join Course f on a.c_id = f.c_id

24、查询学生平均成绩及其名次

select a.s_id,
                @i:=@i+1 as '不保留空缺排名',
                @k:=(case when @avg_score=a.avg_s then @k else @i end) as '保留空缺排名',
                @avg_score:=avg_s as '平均分'
        from (select s_id,ROUND(AVG(s_score),2) as avg_s from Score GROUP BY s_id ORDER BY avg_s DESC)a,(select @avg_score:=0,@i:=0,@k:=0)b;

25、查询各科成绩前三名的记录
-- 1.选出b表比a表成绩大的所有组
-- 2.选出比当前id成绩大的 小于三个的


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,125评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,293评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,054评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,077评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,096评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,062评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,988评论 3 417
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,817评论 0 273
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,266评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,486评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,646评论 1 347
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,375评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,974评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,621评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,796评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,642评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,538评论 2 352

推荐阅读更多精彩内容

  • Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 S...
    忘了呼吸的那只猫阅读 2,862评论 0 8
  • 50个常用的sql语句Student(S#,Sname,Sage,Ssex) 学生表Course(C#,Cname...
    哈哈海阅读 1,231评论 0 7
  • 说明:以下五十个语句都按照测试数据进行过测试,最好每次只单独运行一个语句。 问题及描述: --1.学生表 Stud...
    lijun_m阅读 1,302评论 0 1
  • 表名和字段 –1.学生表 Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓...
    友人A_b238阅读 173评论 0 0
  • CREATE TABLE student(s_no INT(20) PRIMARY KEY COMMENT'学生学...
    li_荔枝阅读 538评论 0 0