表结构如下:
–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) –学生编号,课程编号,分数
--建表SQL
--学生表
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);
(一)评分标准:
- 学习每门课程的学生人数。(3分)
select COUNT(c_name),c_name from course join score on course.c_id=score .c_id join student on score.s_id=student.s_id
- 查询1990年出生的学生信息。(3分)
select * from student WHERE s_birth=1990
- 查询同名同姓且同性别的信息及各自的数量。(3分)
select COUNT(s_name&&s_sex),s_name,s_sex from student GROUP BY s_name,s_sex
- 查询名字中有‘风’字的学生信息。(3分)
select * from student WHERE s_name LIKE '%风'
- 查询出男生及女生的数量。(3分)
select COUNT(s_sex) from student GROUP BY s_sex='男',s_sex='女'
- 检索"01"课程分数小于60,按分数降序排列的学生信息。(5分)
select t2.* from score t1,student t2 where t2.s_id = t1.s_id and t1.s_score < '60' and t1.c_id = '01' order by t1.s_score;
- 查询"01"课程比"02"课程成绩低的学生的信息及课程分数。(5分)
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_id
and t1.c_id = '01'
and t2.c_id = '02'
and t1.s_score > t2.s_score ) s2
where s1.s_id = s2.s_id;
- 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩。(5分)
select t1.s_id,t2.s_name,avg(t1.s_score) as avg_score from score t1
left join student t2 on t1.s_id = t2.s_id
group by t1.s_id
having avg(t1.s_score) >= 60
- 查询学过"张三"老师授课的同学的信息。(5分)
select * from student JOIN score on student.s_id=score.s_id
JOIN course on score.c_id=course.c_id
JOIN teacher on course.t_id=teacher.t_id
WHERE t_name='张三'
- 查询至少有一门没有考试的学生信息。(5分)
select s_score,s_name,c_name from score JOIN student on student.s_id=score.s_id
JOIN course on course.c_id=score.c_id
WHERE s_score=0
- 查询所有学生的出生日期及年龄。(5分)
select (YEAR(CURDATE())-YEAR(s_birth)) as age,s_name FROM student
- 查询半月过生日的学生信息。(5分)
select *,DATE_FORMAT(s_birth,"%d") as day from student as ab inner join score as bb on ab.s_id=bb.s_id join course as cb on bb.c_id=cb.c_id join teacher as db on cb.t_id=db.t_id having day < 15
- 绩。(10分)
select c_name,student.s_id,s_name,s_score from course JOIN score on course.c_id=score.c_id
JOIN student on student.s_id=score.s_id
WHERE s_score<60
- 查询各科成绩最高分、最低分和平均分:(10分)
以如下形式显示:课程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
- 查询学生的总成绩并进行排名(高到低),显示排名数、姓名、性别、学号、总分。(10分)
select s_name,s_sex,student.s_id,SUM(s_score) from course JOIN score on course.c_id=score.c_id
JOIN student on score.s_id=student.s_id GROUP BY s_name
ORDER BY SUM(s_score) DESC
- 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比。(10分)
select course.c_name, course.c_id,
sum(case when score.s_score<=100 and score.s_score>85 then 1 else 0 end) as "[100-85]",
sum(case when score.s_score<=85 and score.s_score>70 then 1 else 0 end) as "[85-70]",
sum(case when score.s_score<=70 and score.s_score>60 then 1 else 0 end) as "[70-60]",
sum(case when score.s_score<=60 and score.s_score>0 then 1 else 0 end) as "[60-0]"
from score left join course
on score.c_id = course.c_id
group by score.c_id;
- 查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩。(10分)
select t5.*,t4.s_score as MAX_score,t4.c_id,t6.c_name
from (score t4,student t5)
right join course t6 on t6.c_id = t4.c_id
where t4.s_score = (select max(t3.s_score)
from teacher t1,course t2,score t3
where t1.t_name = '张三' and t1.t_id = t2.t_id and t2.c_id = t3.c_id)and t4.s_id = t5.s_id