MySQL查詢練習題,自我解答版本

#一、查詢"01"課程比"02"課程成績高的學生的信息及課程分數
SELECT c.*,a.s_score,b.s_score
FROM score AS a,score AS b,student AS c
WHERE c.s_id=a.s_id AND a.s_id=b.s_id AND a.c_id='01'AND b.c_id='02'AND a.s_score>b.s_score;ide

#二、查詢"01"課程比"02"課程成績低的學生的信息及課程分數
SELECT c.*,a.s_score,b.s_score
FROM score AS a,score AS b,student AS c
WHERE c.s_id=a.s_id AND a.s_id=b.s_id AND a.c_id='01'AND b.c_id='02'AND a.s_score<b.s_score;學習

#三、查詢平均成績大於等於60分的同窗的學生編號和學生姓名和平均成績
SELECT a.*,AVG(b.s_score)AS total
FROM student AS a,score AS b
WHERE a.s_id=b.s_id
GROUP BY a.s_id
HAVING total>=60;code

#四、查詢平均成績小於60分的同窗的學生編號和學生姓名和平均成績
SELECT a.*,AVG(IFNULL(b.s_score,0))AS total
FROM student AS a
LEFT JOIN score AS b ON a.s_id=b.s_id
GROUP BY a.s_id
HAVING total<60;數學

#五、查詢全部同窗的學生編號、學生姓名、選課總數、全部課程的總成績
SELECT a.s_id,a.s_name,COUNT(b.c_id),SUM(IFNULL(b.s_score,0))
FROM student AS a
LEFT JOIN score AS b ON a.s_id=b.s_id
GROUP BY a.s_id;it

#六、查詢"李"姓老師的數量
SELECT COUNT(1)
FROM teacher AS a
WHERE a.t_name LIKE'李%';class

#七、查詢學過"張三"老師授課的同窗的信息
SELECT a.*
FROM student AS a WHERE a.s_idIN(
SELECT s.s_id FROM score AS s WHERE s.c_id=(
(SELECT c.c_idFROM course AS c WHERE c.t_id=(
SELECT t.t_id FROM teacher AS t WHERE t.t_name='張三'))));統計

#八、查詢沒學過"張三"老師授課的同窗的信息
SELECT a.*
FROM student AS a WHERE a.s_id NOT IN(
SELECT s.s_id FROM score AS s WHERE s.c_id=(
(SELECT c.c_idFROM course AS c WHERE c.t_id=(
SELECT t.t_id FROM teacher AS t WHERE t.t_name='張三'))));查詢

#九、查詢學過編號爲"01"而且也學過編號爲"02"的課程的同窗的信息
SELECT a.*
FROM student AS a
WHERE a.s_idIN(SELECT b.s_id FROM score AS b WHERE b.c_id='01')
AND a.s_idIN(SELECT c.s_id FROM score AS c WHERE c.c_id='02');英語

#十、查詢學過編號爲"01"可是沒有學過編號爲"02"的課程的同窗的信息
SELECT a.*
FROM student AS a
WHERE a.s_idIN(SELECT b.s_id FROM score AS b WHERE b.c_id='01')
AND a.s_id NOT IN(SELECT c.s_id FROM score AS c WHERE c.c_id='02');di

#十一、查詢沒有學全全部課程的同窗的信息
SELECT a.*
FROM student AS a
LEFT JOIN score AS b ON a.s_id=b.s_id
GROUP BY a.s_id
HAVING COUNT(1)<(SELECT COUNT(1)FROM course);

#十二、查詢至少有一門課與學號爲"01"的同窗所學相同的同窗的信息
SELECT DISTINCT a.*
FROM student AS a
LEFT JOIN score AS b ON a.s_id=b.s_id
WHERE b.c_id IN
(SELECT c.c_idFROM score AS c WHERE c.s_id='01')
AND b.s_id!='01';

#1三、查詢和"01"號的同窗學習的課程徹底相同的其餘同窗的信息
SELECT a.*
FROM student AS a
WHERE a.s_id!='01' AND a.s_id IN
(SELECT b.s_idFROM score AS b GROUP BY b.s_id HAVING COUNT(1)=(SELECT COUNT(1)FROM score WHERE score.s_id='01'));

#1五、查詢兩門及其以上不及格課程的同窗的學號,姓名及其平均成績
SELECT a.s_id,a.s_name, AVG(IFNULL(b.s_score,0))
FROM student AS a
LEFT JOIN score AS b ON a.s_id=b.s_id
WHERE IFNULL(b.s_score,0)<60
GROUP BY a.s_id
HAVING COUNT(1)>=2

#1六、檢索"01"課程分數小於60,按分數降序排列的學生信息
SELECT a.*
FROM student AS a
LEFT JOIN score AS b ON a.s_id=b.s_id
WHERE b.c_id='01'AND b.s_score<60;

#1七、按平均成績從高到低顯示全部學生的全部課程的成績以及平均成績
SELECT a.*,(SELECT score.s_scoreFROM score WHERE a.s_id=score.s_id AND score.c_id='01')AS 語文,
(SELECT score.s_scoreFROM score WHERE a.s_id=score.s_id AND score.c_id='02')AS 數學,
(SELECT score.s_scoreFROM score WHERE a.s_id=score.s_id AND score.c_id='03')AS 英語,
ROUND(AVG(b.s_score),2)AS 平均成績
FROM student AS a INNER JOIN score AS b ON a.s_id=b.s_id GROUP BY a.s_id ORDER BY 平均成績 DESC;

#18.查詢各科成績最高分、最低分和平均分:以以下形式顯示:課程ID,課程name,最高分,
#最低分,平均分,及格率,中等率,優良率,優秀率

#--及格爲>=60,中等爲:70-80,優良爲:80-90,優秀爲:>=90
SELECT b.c_id,b.c_name,MAX(a.s_score),MIN(a.s_score),AVG(a.s_score)
,(SUM(CASE WHEN a.s_score>=90 THEN 1 ELSE 0 END)/COUNT(1)) AS 優秀率
,(SUM(CASE WHEN a.s_score>=80 AND a.s_score<90 THEN 1 ELSE 0 END)/COUNT(1)) AS 優良率
,(SUM(CASE WHEN a.s_score>=70 AND a.s_score<80 THEN 1 ELSE 0 END)/COUNT(1)) AS 中等率
,(SUM(CASE WHEN a.s_score>=60 THEN 1 ELSE 0 END)/COUNT(1)) AS 及格率
FROM score AS a,course AS b WHERE a.c_id=b.c_id
GROUP BY a.c_id;

#2一、查詢不一樣老師所教不一樣課程平均分從高到低顯示
SELECT b.t_id,b.c_name,AVG(a.s_score)AS avg
FROM score AS a,course AS b WHERE b.c_id=a.c_id
GROUP BY b.c_id
ORDER BY avgDESC;

#2三、統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[0-60]及所佔百分比
SELECT b.c_id,b.c_name
,(SUM(CASE WHEN a.s_score>=85 THEN 1 ELSE 0 END)/COUNT(1))AS '[100-85]'
,(SUM(CASE WHEN a.s_score>=70 AND a.s_score<85 THEN 1 ELSE 0 END)/COUNT(1))AS '[85-70]'
,(SUM(CASE WHEN a.s_score>=60 AND a.s_score<70 THEN 1 ELSE 0 END)/COUNT(1))AS '[70-60]'
,(SUM(CASE WHEN a.s_score<60 THEN 1 ELSE 0 END)/COUNT(1))AS '[0-60]'
FROM score AS a ,course AS b WHERE b.c_id=a.c_id
GROUP BY b.c_id;

#26查詢每門課程被選修的學生數
SELECT score.c_id,COUNT(1) FROM score GROUP BY score.c_id;

#27查詢出只有兩門課程的所有學生的學號和姓名
SELECT a.*
FROM student AS a,score AS b
WHERE a.s_id=b.s_id
GROUP BY a.s_id
HAVING COUNT(1)=2;

#28查詢男生、女生人數
SELECT a.s_sex,COUNT(1) FROM student AS a GROUP BY a.s_sex;

#29查詢名字中含有"風"字的學生信息
SELECT a.* FROM student AS a WHERE a.s_name LIKE '%風%';

#30、查詢同名同性學生名單,並統計同名人數
SELECT a.s_name,COUNT(1)
FROM student AS a,student AS b
WHERE a.s_name=b.s_name AND a.s_id!=b.s_id
GROUP BY a.s_name;

#3一、查詢1990年出生的學生名單
SELECT a.*FROM student AS a WHERE YEAR(a.s_birth)='1990';

#3二、查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
SELECT a.c_id,AVG(a.s_score)AS avg FROM score AS a GROUP BY a.c_id ORDER BY avgDESC;

#3三、查詢平均成績大於等於85的全部學生的學號、姓名和平均成績
SELECT a.*,AVG(b.s_score)AS avg
FROM student AS a,score AS b
WHERE a.s_id=b.s_id
GROUP BY a.s_id
HAVING avg>=85;

#3四、查詢課程名稱爲"數學",且分數低於60的學生姓名和分數
SELECT b.s_name,c.s_score FROM student AS b ,score AS c WHERE b.s_id=c.s_id AND c.c_id=
(SELECT a.c_id FROM course AS a WHERE a.c_name='數學') AND c.s_score<60;

#3五、查詢全部學生的課程及分數狀況
SELECT a.*,(SELECT b.s_score FROM score AS b WHERE b.c_id='01' AND a.s_id=b.s_id)AS chinese
,(SELECT b.s_score FROM score AS b WHERE b.c_id='02' AND a.s_id=b.s_id)AS math
,(SELECT b.s_score FROM score AS b WHERE b.c_id='03' AND a.s_id=b.s_id)AS english
FROM student AS a;

#3六、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
SELECT a.s_name,c.c_name,b.s_score
FROM student a,score b,course c
WHERE a.s_id=b.s_id AND b.c_id=c.c_idAND b.s_score>70;

#3七、查詢不及格的課程
SELECT a.s_id,a.c_id,b.c_name,a.s_score
FROM score AS a,course AS b
WHERE a.c_id=b.c_id AND a.s_score<60;

#3八、查詢課程編號爲01且課程成績在70分以上的學生的學號和姓名;
SELECT a.s_id,a.s_name,b.s_score
FROM student AS a,score AS b
WHERE a.s_id=b.s_id AND b.c_id='01'AND b.s_score>70;

#3九、求每門課程的學生人數
SELECT COUNT(1) FROM score AS a GROUP BY a.c_id;

#40、查詢選修"張三"老師所授課程的學生中,成績最高的學生信息及其成績
SELECT a.*,b.c_id,b.s_score FROM student AS a ,score AS b WHERE a.s_id=b.s_id AND b.c_id=
(SELECT c.c_id FROM course AS c WHERE c.t_id=
(SELECT t.t_id FROM teacher AS t WHERE t.t_name='張三'))
ORDER BY b.s_scoreDESC;

#4一、查詢不一樣課程成績相同的學生的學生編號、課程編號、學生成績
SELECT DISTINCT a.s_id,b.c_id,b.s_score
FROM student AS a,score AS b,score AS c
WHERE a.s_id=b.s_id AND b.c_id!=c.c_id AND b.s_score=c.s_score;

#4三、統計每門課程的學生選修人數(超過5人的課程才統計)。要求輸出課程號和選修人數,
#查詢結果按人數降序排列,若人數相同,按課程號升序排列

SELECT a.c_id,COUNT(1)AS 人數
FROM score AS a
GROUP BY a.c_id
HAVING 人數>5
ORDER BY 人數 DESC ,a.c_id ASC;

#4四、檢索至少選修兩門課程的學生學號
SELECT a.s_id,COUNT(1)AS 選課數
FROM score AS a
GROUP BY a.s_id
HAVING 選課數>=2;

#4五、查詢選修了所有課程的學生信息
SELECT a.* FROM student AS a,score AS b
WHERE a.s_id=b.s_id
GROUP BY a.s_id
HAVING COUNT(1)=(SELECT COUNT(1)FROM course );

#4六、查詢各學生的年齡
SELECT a.*,(YEAR(NOW())-YEAR(a.s_birth))AS 年齡
FROM student AS a ;

#4七、查詢本週過生日的學生
SELECT *FROM student WHERE (WEEK(NOW())-WEEK(student.s_birth))=0;

#48查詢下週過生日的學生
SELECT * FROM student WHERE (WEEK(student.s_birth)-WEEK(NOW()))=1;

#4九、查詢本月過生日的學生
SELECT * FROM student WHERE MONTH(NOW())=MONTH(student.s_birth);

#50、查詢下月過生日的學生
SELECT * FROM student WHERE MONTH(NOW())+1=MONTH(student.s_birth);

相關文章
相關標籤/搜索