表結構如下: –1.學生表 Student(s_id,s_name,s_birth,s_sex) –學生編號,學生姓名, 出生年月,學生性別 –2.課程表 Course(c_id,c_name,t_id) – –課程編號, 課程名稱, 教師編號 –3.教師表 Teacher(t_id,t_name) ...
表結構如下:
–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