一、現有數據庫casemanage中表結構如下圖
TABLENAME:afinfo
Id |
name |
age |
birth |
sex |
memo |
1 |
徐洪國 |
37 |
1979-03-23 |
男 |
高中 |
2 |
王芳芳 |
26 |
1988-02-06 |
女 |
本科 |
3 |
徐曉盛 |
24 |
1990-04-02 |
男 |
碩士 |
4 |
陳曉 |
30 |
1984-09-12 |
女 |
博士 |
5 |
鄭凱 |
27 |
1987-12-30 |
男 |
大專 |
1)請編寫sql語句對年齡進行升序排列
select * from afinfo order by birth;
2)請編寫sql語句查詢對“徐”姓開頭的人員名單
select * from afinfo where name like '徐%';
3)請編寫sql語句修改“陳曉”的年齡爲“45”
update afinfo set age=45 and birth=birth-YEAR(45) where name="陳曉";
4)請編寫sql刪除王芳芳這表數據記錄。
delete from afinfo where name="王芳芳";
二、現有以下幾個表
學生信息表(student)
姓名name |
學號code |
張三 |
001 |
李四 |
002 |
馬五 |
003 |
甲六 |
004 |
考試信息表(exam)
學號code |
學科subject |
成績score |
001 |
數學 |
80 |
002 |
數學 |
75 |
001 |
語文 |
90 |
002 |
語文 |
80 |
001 |
英語 |
90 |
002 |
英語 |
85 |
003 |
英語 |
80 |
004 |
英語 |
70 |
1)查詢出所有學生信息,SQL怎麼編寫?
select * from stu;
2)新學生小明,學號爲005,需要將信息寫入學生信息表,SQL語句怎麼編寫?
insert into stu values ("小明",005);
3)李四語文成績被登記錯誤,成績實際爲85分,更新到考試信息表中,SQL語句怎麼編寫?
update exam set score=85 where id=(select id from stu where name="李四") and subject="語文";
4)查詢出各科成績的平均成績,顯示字段爲:學科、平均分,SQL怎麼編寫?
select subject,avg(score) from exam group by subject;
5)查詢出所有學生各科成績,顯示字段爲:姓名、學號、學科、成績,並以學號與學科排序,沒有成績的學生也需要列出,SQL怎麼編寫?
select s.name,s.id,e.subject,e.score from stu s left join exam e on s.id=e.id order by id,subject;
6)查詢出單科成績最高的,顯示字段爲:姓名、學號、學科、成績,SQL怎麼編寫?
select s.name,s.id,e.subject,e.score from stu s join exam e on s.id=e.id where (e.subject,e.score) in (select subject,max(score) from exam group by subject);
7)列出每位學生的各科成績,要求輸出格式:姓名、學號、語文成績、數學成績、英語成績,SQL怎麼編寫?
三、根據要求寫出SQL語句。
Student(s_no,sname,sage,sex)學生表
Course(c_no,cname,t_no)課程表
Sc(s_no,c_no,score)成績表
Teacher(t_no,tname)教師表
1、查詢“001”課程比“002”課程成績高的所有學生的學號。
select a.s_no from (select s_no,score from Sc where c_no='1') a,(select s_no,score from Sc where c_no='2') b where a.score>b.score and a.s_no=b.s_no;
2、查詢平均成績大於60分的同學的學號和平均成績。
select s_no,avg(score) from Sc group by s_no having avg(score)>60;
3、查詢所有同學的學號、姓名、選課數、總成績。
select Student.s_no,Student.sname,count(Sc.c_no),sum(score) from Student left outer join Sc on Student.s_no=Sc.s_no group by Student.s_no, Student.sname;
4、查詢姓李的老師的個數。
select count(distinct(tname)) from Teacher where tname like '李';
5、查詢沒學過“葉平”老師課的同學的學號、姓名
select Student.s_no,Student.sname from Student where s_no not in(select distinct (Sc.s_no) from Sc,Course,Teacher where Sc.s_no=Course.c_no and Teacher.t_no=Course.t_no and Teacher.tname='葉平');
6、查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名。
select Student.s_no,Student.sname from Student,Sc where Student.s_no=Sc.s_no and Sc.c_no='002' and exists(select * from Sc as Sc1 where Sc.s_no=Sc1.s_no and Sc1.s_no='002');
7、查詢所有課程成績小於60分的同學的學號、姓名。
select s_no,sname from Student where s_no not in (select S.s_no from Student AS S,Sc where S.s_no=Sc.s_no and score>60);
8、查詢沒有學全所有課的同學的學號、姓名。
select Student.s_no,Student.sname from Student,Sc where Student.s_no=Sc.s_no group by Student.s_no,Student.sname having count(c_no)<(select count(*) from Course);
10、查詢至少學過學號爲“001”同學所有一門課的其他同學學號和姓名。
select distinct s_no,sname from Student,Sc where Student.s_no=Sc.s_no and Sc.c_no in (select c_no from Sc where s_no='1001');
11、把“sc”表中“葉平”老師教的課的成績都更改爲此課程的平均成績。
update Sc set score=(select avg(Sc_2.score) from Sc Sc_2 where SC_2.c_no=Sc.c_no ) from Course,Teacher where Course.c_no=Sc.c_no and Course.t_no=Teacher.t_no and Teacher.tname='葉平');
12、查詢和“1002”號同學學習的課程完全相同的其他同學學號和姓名。
select s_no from Sc where c_no in (select c_no from Sc where s_no='1002') group by s_no having count(*)=(select count(*) from Sc where s_no='1002');
13、刪除學習“葉平”老師課的sc表記錄。
delete Sc from course,Teacher where Course.c_no=SC.c_no and Course.t_no=Teacher.t_no and tname='葉平';
14、向sc表中插入一些記錄,這些記錄要求符合一下條件:沒有上過編號“003”課程的同學學號
insert into Sc select s_no from Student where s_no not in (Select s_no from Sc where c_no='003');
15、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分。
SELECT L.c_no As c_no,L.score AS max_score,R.score AS mix_score FROM Sc L ,Sc AS R
WHERE L.c_no = R.c_no and
L.score = (SELECT MAX(IL.score)
FROM Sc AS IL,Student AS IM
WHERE L.c_no = IL.c_no and IM.s_no=IL.s_no
GROUP BY IL.c_no)
AND
R.Score = (SELECT MIN(IR.score)
FROM Sc AS IR
WHERE R.c_no = IR.c_no
GROUP BY IR.c_no
) order by L.c_no;
16、查詢不同老師所教不同課程平均分從高到低顯示。
select c_no,avg(score) avg_score from Sc group by c_no order by avg_score desc ;
17、統計各科成績,各分數段人數:課程ID,課程名稱,【100-85】,【85-70】,【70-60】,【<60】
select Course.c_no,cname,
count(case when score>85 and score<=100 then score end) '[85-100]',
count(case when score>70 and score<=85 then score end) '[70-85]',
count(case when score>=60 and score<=70 then score end) '[60-70]',
count(case when score<60 then score end) '[<60]'
from Course,Sc
where Course.c_no=Sc.c_no
group by Course.c_no,c_name;
18、查詢每門課程被選修的學生數
select c_no,count(*) from Sc group by c_no;
19、查詢出只選修了一門課程的全部學生的學號和姓名
select Student.s_no,Student.sname,count(c_no) from Student join Sc on Student.s_no=Sc.s_no group by Student.s_no, Student.sname having count(c_no)=1;
20、查詢男生、女生人數
select count(*) from Student group by sex;
21、查詢姓“張”的學生名單
select * from Student where sname like '張%';
22、查詢同名同性學生名單,並統計同名人數。
select sname ,count(*) from Student group by sname having count(*)>1;
23、查詢1994年出生的學生名單(注:student表中sage列的類型是datatime)
select * from Student where year(curdate())-age='1994';
24、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列。
select c_no ,avg(score)from Sc group by c_no order by avg(score) asc,c_no desc;
25、查詢平均成績都大於85的所有學生的學號,姓名和平均成績
select Student.s_no,Student.sname,avg(score) from Student,Sc where Student.s_no=Sc.s_no group by Student.s_no, Student.sname having avg(score)>85;
26、查詢課程名稱爲“數據庫”且分數低於60的學生姓名和分數
select Student.sname,Sc.score from Student,Sc where Student.s_no=Sc.s_no and Sc.score<60 and Sc.c_no=(select c_no from Course where cname='數據庫');
27、查詢所有學生的選課情況
select Student.s_no,Student.sname,Sc.s_no,Course.cname from Student,Sc,Course where Student.s_no=Sc.s_no and Sc.c_no=Course.c_no;
28、查詢不及格的課程,並按課程號從大到小排序。
select Student.sname,Sc.c_no,Course.cname,Sc.score from Student,Sc,Course where Student.s_no=Sc.s_no and Sc.c_no=Course.c_no and Sc.score<60 order by c_no;
29、查詢課程編號爲003且課程成績在80分以上的學生的學號和姓名。
select Student.s_no,Student.sname from Student,Sc,Course where Sc.score>80 and Course.c_no='003';
30、求選修了課程的學生人數。
select count(*) from (select count(*) from Sc group by s_no) b;
31、查詢選修了“馮老師”所授課程的學生中,成績最高的學生姓名及其成績。
select Student.sname,Sc.score from Student,Sc,Course where Student.s_no=Sc.s_no and Sc.c_no=Course.c_no order by score desc limit 1;
32、查詢各個課程及相應的選修人數。
select Course.c_no,Course.cname,count(s_no) from Course join Sc on Course.c_no=Sc.c_no group by Course.c_no, Course.cname;
33、查詢每門課程最好的前兩名。
select a.s_no,a.c_no,a.score from Sc a where (select count(distinct score) from Sc b where b.c_no=a.c_no and b.score>=a.score)<=2 order by a.c_no,a.score desc ;
34、查詢每門課程的學生選修人數(超過10人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,查詢結果按人數降序排列,若人數相同,按課程號升序排列。
select Sc.c_no,count(*) from Sc group by c_no having count(*)>10 order by count(*) desc,c_no;
35、檢索至少選修兩門課程的學生學號。
select s_no from Sc group by s_no having count(*)>2;
36、查詢全部學生都選修的課程的課程號和課程名。
select Course.c_no,Course.cname from Course join Sc on Course.c_no=Sc.c_no join (select c_no,count(s_no) from Sc group by c_no having count(s_no)=(select count(*) from Student) )as a on Course.c_no=a.c_no;
37、查詢兩門以上不及格課程的同學的學號及其平均成績。
select s_no,avg(score) from Sc where s_no in (select s_no from Sc where score<60 group by s_no having count(*)>2) group by s_no;