mysql50到例题_关于MySQL的经典例题50道 答案参考
答案不全面,歡迎交流溝通
-- 1、查詢"01"課程比"02"課程成績高的學生的信息及課程分數
select * from? sc s INNER JOIN sc c? INNER JOIN student st on s.S = c.S and st.S=s.S where s.C=01 and c.C=02 and s.score > c.score;
-- //select * from sc s inner join sc c on s.s=c.s where s.c=01 and c.c=02 and s.scorse>c.scorse;
-- select * from student,(select * from sc where c=01) q INNER JOIN(select * from sc where c=02) w on q.s=w.s? on? where q.score > w.score;//
-- 2、查詢"01"課程比"02"課程成績低的學生的信息及課程分數
select * from? sc s INNER JOIN sc c? INNER JOIN student st on s.S = c.S and st.S=s.S where s.C=01 and c.C=02 and s.score < c.score;
-- 3、查詢平均成績大于等于60分的同學的學生編號和學生姓名和平均成績
select s.S,avg(s.score),st.Sname from student st INNER JOIN sc s on st.S=s.S? group by s.S having avg(s.score) >=60;
-- 4、查詢平均成績小于60分的同學的學生編號和學生姓名和平均成績
select s.S,avg(s.score),st.Sname from student st INNER JOIN sc s on st.S=s.S GROUP BY s.S HAVING avg(score)>60;
-- 5、查詢所有同學的學生編號、學生姓名、選課總數、所有課程的總成績
select st.S,st.Sname, sum(s.score),count(s.C)from student st INNER JOIN sc s ON st.S=s.S GROUP BY s.S;
-- 6、查詢"江"姓老師的數量
select count(s) as '總人數'from sc where sc.C=(select t from teacher where Tname='江老師')
-- 7、查詢學過"向老師"授課的同學的信息
select * from sc INNER JOIN student on sc.S =student.S where sc.C=(select t from teacher where Tname='向老師') and sc.S=student.S;
-- 8、查詢沒學過"向老師"授課的同學的信息
//select * from teacher tt INNER JOIN course c ON tt.T=c.T INNER JOIN sc s on c.C=s.C INNER JOIN student st on s.S=st.S where tt.Tname !='向老師';
-- 9、查詢學過編號為"01"并且也學過編號為"02"的課程的同學的信息
select * from sc s INNER JOIN student st INNER JOIN sc c on s.S =st.S and st.S=c.S where s.C= 01 and c.C=02;
-- 10、查詢學過編號為"01"但是沒有學過編號為"02"的課程的同學的信息
select * from (select * from sc where c=01) s INNER JOIN (select * from sc where c!=02) c on s.s=c.s;
-- 11、查詢沒有學全所有課程的同學的信息
select * from sc GROUP BY s HAVING count(c) != (select count(c) from course);
select s,group_concat(c) from sc group by s
select datediff('2019-09-09','2018-07-06')
-- 12、查詢至少有一門課與學號為"01"的同學所學相同的同學的信息
select * from student sd where sd.S in(select s.S from sc s where s.C in(select s.C from sc s where s.S=01) GROUP BY s.S) and sd.S !=01;
-- 13、查詢和"01"號的同學學習的課程完全相同的其他同學的信息
select * from student sd where sd.S in(select s.S from sc s INNER JOIN sc c INNER JOIN sc cs on s.S=c.S and c.S= cs.S where s.C=01 and c.C=02 and cs.C=03 GROUP BY s.S);
-- 14、查詢沒學過"哈哈"老師講授的任一門課程的學生姓名
select * from teacher tt INNER JOIN course c on tt.T=c.T INNER JOIN sc s on c.C=s.C INNER JOIN student sd on s.S=sd.S where tt.Tname='哈哈';
-- 15、查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select sc.S,sd.Sname,avg(sc.score) from sc INNER JOIN student sd on sc.S=sd.S where score <60? HAVING count(c) >= (select count(c)-1 from course);
-- 16、檢索"01"課程分數小于60,按分數降序排列的學生信息
select * from sc s INNER JOIN student sd on s.s=sd.s where s.C=01 and s.score<60 GROUP BY s.score desc;
-- 17、按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
select s.S,a.d as'平均成績'from sc s INNER JOIN (select avg(s.score) d,s.S from sc s GROUP BY s.S) a on s.S=a.S GROUP BY a.d DESC;
-- 18、查詢各科成績最高分、最低分和平均分:以如下形式顯示:課程ID,課程name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率
-- 及格為>=60,中等為:70-80,優良為:80-90,優秀為:>=90
select c.c '課程ID',cs.Cname '課程name',max(c.score) '最高分',min(c.score) '最低分',avg(c.score) '平均分',from sc c INNER JOIN course cs on c.C=cs.C where
SELECT
L.C AS 課程ID,
L.score AS 最高分,
R.score AS 最低分
FROM
SC L,
SC AS R
WHERE
L.C = R.C
AND L.score = (
SELECT
MAX(IL.score)
FROM
SC AS IL,
Student AS IM
WHERE
L.C = IL.C
AND IM.S = IL.S
GROUP BY
IL.C
)
AND R.Score = (
SELECT
MIN(IR.score)
FROM
SC AS IR
WHERE
R.C = IR.C
GROUP BY
IR.C
);
-- 19、按各科成績進行排序,并顯示排名
select s.score,cs.Cname from sc s INNER JOIN course cs on s.C=cs.C GROUP BY s.C ORDER BY s.score desc;
-- 20、查詢學生的總成績并進行排名
select d.a from sc e INNER JOIN (select sum(s.score) a,s.C from sc s GROUP BY s.S) d on e.C=d.C GROUP BY d.a desc ;
-- 21、查詢不同老師所教不同課程平均分從高到低顯示
select * from teacher tc INNER JOIN course cs INNER JOIN (select avg(s.score) 平均分,s.C from sc s? GROUP BY s.C) w on tc.T=cs.T and cs.C=w.C GROUP BY w.平均分 DESC;
-- 22、查詢所有課程的成績第2名到第3名的學生信息及該課程成績
-- 23、統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[0-60]及所占百分比
-- 24、查詢學生平均成績及其名次
select 1+(select COUNT( distinct 平均成績)FROM (select S,AVG(score) AS 平均成績 FROM SC GROUP BY S) AS T1
WHERE 平均成績 > T2.平均成績) as 名次,S as 學生學號,平均成績
FROM (SELECT S,AVG(score) 平均成績 FROM SC GROUP BY S) AS T2 ORDER BY 平均成績 desc;
-- 25、查詢各科成績前三名的記錄
select s.score,s.C from sc s GROUP BY s.C ORDER BY s.score DESC;
-- 26、查詢每門課程被選修的學生數
select count(s.C) as '每門課的總人數',s.C as'科目', cs.Cname from sc s INNER JOIN course cs on s.C=cs.C GROUP BY s.C;
-- 27、查詢出只有兩門課程的全部學生的學號和姓名
-- 28、查詢男生、女生人數
select count(s) as'個數',Ssex from student where ssex='男' or ssex='女' GROUP BY Ssex;
-- 29、查詢名字中含有"風"字的學生信息
select * from student where Sname like '%風%';
-- 30、查詢同名同性學生名單,并統計同名人數
-- 錯誤答案:select? * from student st INNER JOIN student sd ON st.S=sd.S where st.Sname=sd.Sname and st.Ssex=sd.Sname GROUP BY st.Sname;
select Sname,count(s)from student GROUP BY Sname having count(*)>1;
-- 31、查詢1990年出生的學生名單(注:Student表中Sage列的類型是datetime)
select * from student sd where sd.Sage like '%1990%';
-- 32、查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號
select avg(score),c from sc where c=c group by c ORDER BY avg(score) desc;
-- 33、查詢平均成績大于等于85的所有學生的學號、姓名和平均成績
select s.S AS'學號',sd.Sname as'姓名',avg(s.score) as'平均成績'from sc s INNER JOIN student sd on s.S=sd.S? GROUP BY s.S HAVING avg(s.score)>=85;
-- 34、查詢課程名稱為"數學",且分數低于60的學生姓名和分數
select s.S,sd.Sname,cs.Cname,s.score from sc s INNER JOIN course cs ON s.C=cs.C INNER JOIN student sd ON s.S=sd.S where cs.Cname='數學' and s.score<60;
-- 35、查詢所有學生的課程及分數情況;
select c.Cname,c.c,stu.Sname,s.score from course c inner join sc s on c.C=s.C INNER JOIN student stu on s.S=stu.S ;
-- 36、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
//-- select * from sc s INNER JOIN student sd on s.S=sd.S where s.score>70 GROUP BY s.C
Select c.Cname,stu.Sname,s.score from course c inner join sc s on c.C=s.C INNER JOIN student stu on s.S=stu.S where score>70 ;
-- 37、查詢不及格的課程
select c.Cname,c.c,stu.Sname,s.score from course c inner join sc s on c.C=s.C INNER JOIN student stu on s.S=stu.S where score<60 ;
-- 38、查詢課程編號為01且課程成績在80分以上的學生的學號和姓名;
select stu.Sname,s.score from sc s INNER JOIN student stu on s.S=stu.S where s.C=01 and s.score>80;
-- 39、求每門課程的學生人數
select count(s.C) as '每門課的總人數',s.C as'科目', cs.Cname from sc s INNER JOIN course cs on s.C=cs.C GROUP BY s.C;
-- 40、查詢選修"哈哈"老師所授課程的學生中,成績最高的學生信息及其成績
select max(score),stu.Sname,t.Tname from teacher t INNER JOIN course c on t.T=c.T INNER JOIN sc s on c.C=s.C INNER JOIN student stu on s.S=stu.S where t.tname='哈哈' ;
-- 41、查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
///select * from sc where c<>c and score=score;
-- 42、查詢每門功成績最好的前兩名
-- 43、統計每門課程的學生選修人數(超過5人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列
-- 44、檢索至少選修兩門課程的學生學號
-- 45、查詢選修了全部課程的學生信息
select * from sc s INNER JOIN sc c INNER JOIN sc cs on s.C=c.C and c.S=cs.S where s.C=01 ;
-- 46、查詢各學生的年齡
select round((TO_DAYS(now())-TO_DAYS(Sage))/365),Sname from student;
-- 47、查詢本周過生日的學生
select * from student where week(now())=week(Sage);
-- 48、查詢下周過生日的學生
select * from student where date_add(now(),interval 1 week)=week(sage);
-- 49、查詢本月過生日的學生
select * from student where month(now())= month(Sage);
-- 50、查詢下月過生日的學生
select * from student where date_add(now(),interval 1 month)=month(sage);
總結
以上是生活随笔為你收集整理的mysql50到例题_关于MySQL的经典例题50道 答案参考的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql with ties_MySQ
- 下一篇: pythonexcel工具介绍_Pyth