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);
持续更新中...