SQL语句练习(经典sql语句)

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

select s.s_id, s.s_name, s.s_birth, s.s_sex, c.s_id, c.c_id, c.s_score 01score ,c1.s_score 02score from Student s inner join Score c on s.s_id = c.s_id and c.c_id = '01' left join score c1 on s.s_id = c1.s_id and c1.c_id = '02' where c.s_score > c1.s_score;

或者

select s1.*, s2.01_score, s2.02_score from student s1,(select t1.s_id as s_id, t1.s_score as 01_score,t2.s_score as 02_score from score t1,score t2 where t1.s_id = t2.s_idand t1.c_id = '01' and t2.c_id = '02' and t1.s_score > t2.s_score ) s2 where s1.s_id = s2.s_id;


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

select s.s_id, s.s_name, s.s_birth, s.s_sex, c.s_id, c.c_id, c.s_score 01score ,c1.s_score 02score from Student s inner join Score c on s.s_id = c.s_id and c.c_id = '01' left join score c1 on s.s_id = c1.s_id and c1.c_id = '02' where c.s_score < c1.s_score;

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

select s.s_name, s.s_id, avg(c.s_score) cvg from student s inner join score c on s.s_id = c.s_id group by s.s_id having cvg > 60;

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

select s.s_name, s.s_id, avg(c.s_score) cvg from student s inner join score c on s.s_id = c.s_id group by s.s_id having cvg < 60 union select s.s_name, s.s_id, avg(c.s_score) cvg from student s left join score c on s.s_id = c.s_id group by s.s_id having cvg is null;

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

select s.s_id,s.s_name name, sum(c.s_score) sum, count(1) count from student s left join score c on s.s_id = c.s_id group by s.s_id;

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

select substring(t_name,1,1) as name , count(1) as count from teacher where t_name like '李%' group by name;

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

select t.t_name tname, st.s_name stname, sc.s_score scscore from teacher t left join  Course cou on t.t_id = cou.t_id left join score sc on cou.c_id = sc.c_id left join student st on sc.s_id = st.s_id where t.t_name = '张三';

8、 查询没学过"张三"老师授课的同学的信息 (吴兰,王菊)

1.哪些学生学习 ‘张三’老师的课程 (s_id)

select st.s_id sid from teacher t left join Course cou on t.t_id = cou.t_id left join score sc on cou.c_id = sc.c_id left join student st on sc.s_id = st.s_id where t.t_name = '张三';

2.通过查询学生表,通过where使用not int 进行过滤  

select * from student where student.s_id not in (select st.s_id sid from teacher t left join Course cou on t.t_id = cou.t_id left join score sc on cou.c_id = sc.c_id left join student st on sc.s_id = st.s_id where t.t_name = '张三');

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

1.查询出课程编号为02的学生 sid

select * from student st inner join score sc on st.s_id = sc.s_id and sc.c_id = 02;

2.查询出课程编码为01的学生, 通过where sid in () 进行过滤  

select * from student st1 inner join score sc1 on st1.s_id = sc1.s_id and sc1.c_id = 01 where st1.s_id in (select sc.s_id from student st inner join score sc on st.s_id = sc.s_id and sc.c_id = 02) 

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

select * from student st1 left join score sc1 on st1.s_id = sc1.s_id and sc1.c_id = 01 where st1.s_id not in (select sc.s_id from student st left join score sc on st.s_id = sc.s_id and sc.c_id = 02);

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

1.三张分数表连接,查询学习过所有课程的学生

select st2.s_id from student st2 inner join score sc2 on st2.s_id = sc2.s_id and sc2.c_id = 03 where st2.s_id in (select sc1.s_id from student st1 inner join score sc1 on st1.s_id = sc1.s_id and sc1.c_id = 01 where st1.s_id in (select sc.s_id from student st inner join score sc on st.s_id = sc.s_id and sc.c_id = 02))

2.通过not in 进行过滤  

select * from student where student.s_id not in (select st2.s_id from student st2 inner join score sc2 on st2.s_id = sc2.s_id and sc2.c_id = 03 where st2.s_id in (select sc1.s_id from student st1 inner join score sc1 on st1.s_id = sc1.s_id and sc1.c_id = 01 where st1.s_id in (select sc.s_id from student st inner join score sc on st.s_id = sc.s_id and sc.c_id = 02))); 


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

1.查询出01同学的课程信息 (c_id)  

select sc.c_id from score sc inner join student st on sc.s_id = st.s_id where st.s_id = '01'

2.查询所有学生c_id2  

select distinct st1.s_name from student st1 left join score sc1 on st1.s_id = sc1.s_id 

3.c_id2 in (c_id) , 有同学报名多门课程,可以使用distinct过滤

select distinct st1.s_name from student st1 left join score sc1 on st1.s_id = sc1.s_id where sc1.c_id in (select sc.c_id from score sc inner join student st on sc.s_id = st.s_id where st.s_id = '01');

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

1. 01学生学习的课程id (c_id),我们把c_id给拼接(concat)到一起  

select group_concat(sc.c_id) from student st inner join score sc on st.s_id = sc.s_id and st.s_id = '01'; 

查询其他学生的信息, 也使用concat拼接到一起,或者使用 in   

select st.s_name,group_concat(sc.c_id) cc from student st inner join score sc on st.s_id = sc.s_id group by st.s_id having cc in (select group_concat(sc.c_id) from student st inner join score sc on st.s_id = sc.s_id and st.s_id = '01');

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

1.查询出张三讲授的课程,通过课程查询到学生的s_id  

select sc.s_id from score sc inner join Course cu on sc.c_id = cu.c_id inner join teacher tc on cu.t_id = tc.t_id where tc.t_name = '张三';

2.not in 进行过滤  

select * from student where student.s_id not in (select sc.s_id from score sc inner join Course cu on sc.c_id = cu.c_id inner join teacher tc on cu.t_id = tc.t_id where tc.t_name = '张三');


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

 1.where 分数 < 60 , 通过 group by(s_id) 进行分组, 通过count函数进行过滤

// 60分一下的不加入统计 

select st.s_name, st.s_id, avg(sc.s_score) avg where sc1.s_id = st.s_id as avg from student st left join score sc on st.s_id = sc.s_id where sc.s_score < 60 group by st.s_id having count(1) >= 2​

// 60分以上的也加入统计  

select st.s_name, st.s_id, (select avg(sc1.s_score) from student st1 left join score sc1 on st1.s_id = sc1.s_id) avg where sc1.s_id = st.s_id as avg from student st left join score sc on st.s_id = sc.s_id where sc.s_score < 60 group by st.s_id having count(1) >= 2 

 2, 某个同学的平均分  

select avg(sc1.s_score) from student st1 left join score sc1 on st1.s_id = sc1.s_id where sc1.s_id = '01'; 


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

select st.s_name, sc.c_id, sc.s_score from student st inner join score sc on st.s_id = sc.s_id and sc.c_id = '01' and sc.s_score < 60 order by sc.s_score desc;

--表信息

--学生表

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);

持续更新中...

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