建表
create database wuxinyu;
use wuxinyu;
学生表student(SId,Sname,Sage,Ssex)
create table student(SId varchar(10),Sname varchar(10),Sbirth datetime,Ssex varchar(10));
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('07' , '王菊' , '1990-01-20' , '女');
课程表course(CId,Cname,TId) --CId --课程编号,Cname 课程名称,TId 教师编号
create table course(CId varchar(10),Cname nvarchar(10),TId varchar(10))
insert into course values('01' , '语文' , '02');
insert into course values('02' , '数学' , '01');
insert into course values('03' , '英语' , '04');
//改名
rename
教师表teacher(TId,Tname) --TId 教师编号,Tname 教师姓名
create table teacher(TId varchar(10),Tname varchar(10));
insert into teacher values('01' , '张三');
insert into teacher values('02' , '李四');
insert into teacher values('03' , '王五');
成绩表 score(SId,CId,score) --SId 学生编号,CId 课程编号,score 分数
create table score(SId varchar(10),CId varchar(10),score decimal(18,1));
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 scorevalues('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 * from
(select score.SId, score.score from score where score.CId = '01') as t1 inner join
(select score.SId, score.score from score where score.CId = '02') as t2 on t1.SId = t2.SId
where t1.score > t2.score;
2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
mysql> select sc1.sid,sc1.score score1,sc2.score score2
from score sc1
join score sc2 on sc1.sid=sc2.sid
where sc1.cid = 1 and sc2.cid = 2 and sc1.score<sc2.score;
3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select s.SId,avgscore,Sname from(
select SId, AVG(score) as avgscore from score
group by SId
having AVG(score)> 60
)r left join
(select student.SId,student.Sname from
student)s on s.SId = r.SId;
4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)
1
5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
> select stu.sid,stu.sname,
-> count(score.cid) countcourse,
-> case when sum(score.score) is null then 0 else sum(score.score) end sumscore
-> from student stu
-> left join score
-> on score.sid=stu.sid
-> group by stu.sid,stu.sname;
6、查询"李"姓老师的数量
select count(*) from teacher where tname like '李%';
7、询学过"张三"老师授课的同学的信息
select student.* from student,teacher,course,score
where
student.sid = score.sid
and course.cid=score.cid
and course.tid = teacher.tid
and tname = '张三';
8、查询没学过"张三"老师授课的同学的信息
select * from student
where student.sid not in(
select score.sid from score where score.cid in(
select course.cid from course where course.tid in(
select teacher.tid from teacher where tname = "张三"
)
)
);
9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
select st.sid, st.sname from(
select t1.sid from
(select s1.sid from score s1 where s1.cid = 1) t1,
(select s2.sid from score s2 where s2.cid = 2) t2
where t1.sid = t2.sid
)stu, student st
where stu.sid = st.sid
10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
1
11、查询没有学全所有课程的同学的信息
select * from student
where student.sid not in (
select score.sid from score
group by score.sid
having count(score.cid)= (select count(cid) from course)
);
12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
select * from student
where student.sid in (
select score.sid from score
where score.cid in(
select score.cid from score
where score.sid = '01'
)
);
13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
1
14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student
where student.sid not in(
select score.sid from score where score.cid in(
select course.cid from course where course.tid in(
select teacher.tid from teacher where tname = "张三"
)
)
);
15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student.SId, student.Sname,b.avg
from student right join
(select sid, AVG(score) as avg from score
where sid in (
select sid from score
where score<60
group by sid
having count(score)>1)
group by sid) b on student.sid=b.sid;
16、检索"01"课程分数小于60,按分数降序排列的学生信息
group by sid) b on student.sid=b.sid;
select student.*, score.score from student, score
where student.sid = score.sid
and score.score < 60
and cid = "01"
order by score.score desc;
17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select * from score
left join (
select sid,avg(score) as avscore from score
group by sid
)r
on score.sid = r.sid
order by avscore desc;
18、查询各科成绩最高分、最低分和平均分,
select
score.CId ,
max(score.score)as 最高分,
min(score.score)as 最低分,
AVG(score.score)as 平均分,
count(*)as 选修人数,
sum(case when score.score>=60 then 1 else 0 end )/count(*)as 及格率,
sum(case when score.score>=70 and score.score<80 then 1 else 0 end )/count(*)as 中等率,
sum(case when score.score>=80 and score.score<90 then 1 else 0 end )/count(*)as 优良率,
sum(case when score.score>=90 then 1 else 0 end )/count(*)as 优秀率
from score
GROUP BY score.CId
ORDER BY count(*)DESC, score.CId ASC
19、按各科成绩进行排序,并显示排名
1
20、查询学生的总成绩并进行排名
select sid,SUM(score)'总成绩' from score
group by sid
order by SUM(score) DESC