1. 程式人生 > >mysql基礎SQL練習

mysql基礎SQL練習

  許久收藏的練習mysql語句的,現在看來任然有學習價值!

表如下:

  • Student(Sid,Sname,Sage,Ssex) 學生表
  • Course(Cid,Cname,Tid) 課程表
  • SC(Sid,Cid,score) 成績表
  • Teacher(Tid,Tname) 教師表

問題:

1、查詢“001”課程比“002”課程成績高的所有學生的學

select a.Sid from (select sid,score from SC where Cid='001') a,(select sid,score   

       from

SC where Cid='002') b  

      where a.score>b.score and a.sid=b.sid; 

 

2、查詢平均成績大於60分的同學的學號和平均成績;

select Sid,avg(score)  

       from sc  

       group by Sid having avg(score) >60;  

 

3、查詢所有同學的學號、姓名、選課數、總成績;

select Student.Sid,Student.Sname,count(SC.Cid),sum(score)   

       from Student left Outer join SC on Student.Sid=SC.Sid  

       group by Student.Sid,Sname

 

4、查詢姓“李”的老師的個數;

select

count(distinct(Tname))  

     from Teacher  

     where Tname like '李%'; 

 

5、查詢沒學過“葉平”老師課的同學的學號、姓名;

select Student.Sid,Student.Sname  

     from Student  

     where Sid not in (select distinct( SC.Sid) from SC,Course,Teacher  

     where SC.Cid=Course.Cid and Teacher.Tid=Course.Tid and Teacher.Tname='葉平'); 

 

6、查詢學過“001”並且也學過編號“002”課程的同學的學號、姓名;

select Student.Sid,Student.Sname from Student,SC  

     where Student.Sid=SC.Sid and SC.Cid='001'and  

     exists( Select * from SC as SC_2 where SC_2.Sid=SC.Sid and SC_2.Cid='002');  

 

7、查詢學過“葉平”老師所教的所有課的同學的學號、姓名;

select Sid,Sname  

     from Student  

     where Sid in (select Sid from SC ,Course ,Teacher  

     where SC.Cid=Course.Cid and Teacher.Tid=Course.Tid and Teacher.Tname='葉平'  

     group by Sid having count(SC.Cid)=(select count(Cid) from Course,Teacher  

     where Teacher.Tid=Course.Tid and Tname='葉平'));  

 

8、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名;

26     Select Sid,Sname from (select Student.Sid,Student.Sname,score ,(select score from SC SC_2  

27     where SC_2.Sid=Student.Sid and SC_2.Cid='002') score2  

28     from Student,SC where Student.Sid=SC.Sid and Cid='001') S_2 where score2 <score; 

9、查詢所有課程成績小於60分的同學的學號、姓名;

29     select Sid,Sname  

30     from Student  

31     where Sid not in (select Student.Sid from Student,SC where S.Sid=SC.Sid and score>60); 

10、查詢沒有學全所有課的同學的學號、姓名;

32     select Student.Sid,Student.Sname  

33     from Student,SC  

34     where Student.Sid=SC.Sid  

35     group by Student.Sid,Student.Sname having count(Cid) <(select count(Cid) from Course);  

11、查詢至少有一門課與學號為“1001”的同學所學相同的同學的學號和姓名;

36     select Sid,Sname from Student,SC  

37     where Student.Sid=SC.Sid and Cid in select Cid from SC where Sid='1001';  

12、查詢至少學過學號為“001”同學所有一門課的其他同學學號和姓名;

38     select distinct SC.Sid,Sname  

39     from Student,SC  

40     where Student.Sid=SC.Sid and Cid in (select Cid from SC where Sid='001');  

13、把“SC”表中“葉平”老師教的課的成績都更改為此課程的平均成績;

41     update SC set score=(select avg(SC_2.score)  

42     from SC SC_2  

43     where SC_2.Cid=SC.Cid ) from Course,Teacher  

44     where Course.Cid=SC.Cid and Course.Tid=Teacher.Tid and Teacher.Tname='葉平');  

14、查詢和“1002”號的同學學習的課程完全相同的其他同學學號和姓名;

45     select Sid from SC where Cid in (select Cid from SC where Sid='1002')  

46     group by Sid having count(*)=(select count(*) from SC where Sid='1002');  

15、刪除學習“葉平”老師課的SC表記錄;

47     Delect SC  

48     from course ,Teacher  

49     where Course.Cid=SC.Cid and Course.Tid= Teacher.Tid and Tname='葉平'; 

16、向SC表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“003”課程的同學學號、2、號課的平均成績;

50     Insert SC select Sid,'002',(Select avg(score)  

51     from SC where Cid='002') from Student  

52     where Sid not in (Select Sid from SC where Cid='002'); 

17、按平均成績從高到低顯示所有學生的“資料庫”、“企業管理”、“英語”三門的課程成績,按如下形式顯示: 學生ID,,資料庫,企業管理,英語,有效課程數,有效平均分

53     SELECT Sid as 學生ID  

54     ,(SELECT score FROM SC WHERE SC.Sid=t.Sid AND Cid='004') AS 資料庫  

55     ,(SELECT score FROM SC WHERE SC.Sid=t.Sid AND Cid='001') AS 企業管理  

56     ,(SELECT score FROM SC WHERE SC.Sid=t.Sid AND Cid='006') AS 英語  

57     ,COUNT(*) AS 有效課程數, AVG(t.score) AS 平均成績  

58     FROM SC AS t  

59     GROUP BY Sid  

60     ORDER BY avg(t.score) 

18、查詢各科成績最高和最低的分:以如下形式顯示:課程ID,最高分,最低分

61     SELECT L.Cid As 課程ID,L.score AS 最高分,R.score AS 最低分  

62     FROM SC L ,SC AS R  

63     WHERE L.Cid = R.Cid and  

64     L.score = (SELECT MAX(IL.score)  

65     FROM SC AS IL,Student AS IM  

66     WHERE L.Cid = IL.Cid and IM.Sid=IL.Sid  

67     GROUP BY IL.Cid)  

68     AND  

69     R.Score = (SELECT MIN(IR.score)  

70     FROM SC AS IR  

71     WHERE R.Cid = IR.Cid  

72     GROUP BY IR.Cid  

73     );  

19、按各科平均成績從低到高和及格率的百分數從高到低順序

74     SELECT t.Cid AS 課程號,max(course.Cname)AS 課程名,isnull(AVG(score),0) AS 平均成績  

75     ,100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分數  

76     FROM SC T,Course  

77     where t.Cid=course.Cid  

78     GROUP BY t.Cid  

79     ORDER BY 100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC 

20、查詢如下課程平均成績和及格率的百分數(用"1行"顯示): 企業管理(001),馬克思(002),OO&UML (003),資料庫(004)

80     SELECT SUM(CASE WHEN Cid ='001' THEN score ELSE 0 END)/SUM(CASE Cid WHEN '001' THEN 1 ELSE 0 END) AS 企業管理平均分  

81     ,100 * SUM(CASE WHEN Cid = '001' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN Cid = '001' THEN 1 ELSE 0 END) AS 企業管理及格百分數  

82     ,SUM(CASE WHEN Cid = '002' THEN score ELSE 0 END)/SUM(CASE Cid WHEN '002' THEN 1 ELSE 0 END) AS 馬克思平均分  

83     ,100 * SUM(CASE WHEN Cid = '002' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN Cid = '002' THEN 1 ELSE 0 END) AS 馬克思及格百分數  

84     ,SUM(CASE WHEN Cid = '003' THEN score ELSE 0 END)/SUM(CASE Cid WHEN '003' THEN 1 ELSE 0 END) AS UML平均分  

85     ,100 * SUM(CASE WHEN Cid = '003' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN Cid = '003' THEN 1 ELSE 0 END) AS UML及格百分數  

86     ,SUM(CASE WHEN Cid = '004' THEN score ELSE 0 END)/SUM(CASE Cid WHEN '004' THEN 1 ELSE 0 END) AS 資料庫平均分  

87     ,100 * SUM(CASE WHEN Cid = '004' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN Cid = '004' THEN 1 ELSE 0 END) AS 資料庫及格百分數  

88     FROM SC  

21、查詢不同老師所教不同課程平均分從高到低顯示

89     SELECT max(Z.Tid) AS 教師ID,MAX(Z.Tname) AS 教師姓名,C.Cid AS 課程ID,MAX(C.Cname) AS 課程名稱,AVG(Score) AS 平均成績  

90     FROM SC AS T,Course AS C ,Teacher AS Z  

91     where T.Cid=C.Cid and C.Tid=Z.Tid  

92     GROUP BY C.Cid  

93     ORDER BY AVG(Score) DESC 

22、查詢如下課程成績第 3 名到第 6 名的學生成績單:企業管理(001),馬克思(002),UML (003),資料庫(004) [學生ID],[學生姓名],企業管理,馬克思,UML,資料庫,平均成績

94     SELECT DISTINCT top 3  

95     SC.Sid As 學生學號,  

96     Student.Sname AS 學生姓名 ,  

97     T1.score AS 企業管理,  

98     T2.score AS 馬克思,  

99     T3.score AS UML,  

100   T4.score AS 資料庫,  

101   ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as 總分  

102   FROM Student,SC LEFT JOIN SC AS T1  

103   ON SC.Sid = T1.Sid AND T1.Cid = '001'  

104   LEFT JOIN SC AS T2  

105   ON SC.Sid = T2.Sid AND T2.Cid = '002'  

106   LEFT JOIN SC AS T3  

107   ON SC.Sid = T3.Sid AND T3.Cid = '003'  

108   LEFT JOIN SC AS T4  

109   ON SC.Sid = T4.Sid AND T4.Cid = '004'  

110   WHERE student.Sid=SC.Sid and  

111   ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)  

112   NOT IN  

113   (SELECT  

114   DISTINCT  

115   TOP 15 WITH TIES  

116   ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)  

117   FROM sc  

118   LEFT JOIN sc AS T1  

119   ON sc.Sid = T1.Sid AND T1.Cid = 'k1'  

120   LEFT JOIN sc AS T2  

121   ON sc.Sid = T2.Sid AND T2.Cid = 'k2'  

122   LEFT JOIN sc AS T3  

123   ON sc.Sid = T3.Sid AND T3.Cid = 'k3'  

124   LEFT JOIN sc AS T4  

125   ON sc.Sid = T4.Sid AND T4.Cid = 'k4'   

126   ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC);  

23、統計列印各科成績,各分數段人數:課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60]

127   SELECT SC.Cid as 課程ID, Cname as 課程名稱  

128   ,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100 - 85]  

129   ,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85 - 70]  

130   ,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70 - 60]  

131   ,SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS [60 -]  

132   FROM SC,Course  

133   where SC.Cid=Course.Cid  

134   GROUP BY SC.Cid,Cname; 

24、查詢學生平均成績及其名次

135   SELECT 1+(SELECT COUNT( distinct 平均成績)  

136   FROM (SELECT Sid,AVG(score) AS 平均成績  

137   FROM SC  

138   GROUP BY Sid  

139   ) AS T1  

140   WHERE 平均成績 > T2.平均成績) as 名次,  

141   Sid as 學生學號,平均成績  

142   FROM (SELECT Sid,AVG(score) 平均成績  

143   FROM SC  

144   GROUP BY Sid  

145   ) AS T2  

146   ORDER BY 平均成績 desc;  

25、查詢各科成績前三名的記錄:(不考慮成績並列情況)

147   SELECT t1.Sid as 學生ID,t1.Cid as 課程ID,Score as 分數  

148   FROM SC t1  

149   WHERE score IN (SELECT TOP 3 score  

150   FROM SC  

151   WHERE t1.Cid= Cid  

152   ORDER BY score DESC   

153   )  

154   ORDER BY t1.Cid; 

26、查詢每門課程被選修的學生數

155   select cid,count(Sid) from sc group by Cid;  

27、查詢出只選修了一門課程的全部學生的學號和姓名

156   select SC.Sid,Student.Sname,count(Cid) AS 選課數  

157   from SC ,Student  

158   where SC.Sid=Student.Sid group by SC.Sid ,Student.Sname having count(Cid)=1;  

28、查詢男生、女生人數

159   Select count(Ssex) as 男生人數 from Student group by Ssex having Ssex='男';  

160   Select count(Ssex) as 女生人數 from Student group by Ssex having Ssex='女'; 

29、查詢姓“張”的學生名單

161   SELECT Sname FROM Student WHERE Sname like '張%';  

30、查詢同名同性學生名單,並統計同名人數

162   select Sname,count(*) from Student group by Sname having count(*)>1;;  

31、1981年出生的學生名單(注:Student表中Sage列的型別是datetime)

163   select Sname, CONVERT(char (11),DATEPART(year,Sage)) as age  

164   from student  

165   where CONVERT(char(11),DATEPART(year,Sage))='1981';  

32、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列

166   Select Cid,Avg(score) from SC group by Cid order by Avg(score),Cid DESC ;  

33、查詢平均成績大於85的所有學生的學號、姓名和平均成績

167   select Sname,SC.Sid ,avg(score)  

168   from Student,SC  

169   where Student.Sid=SC.Sid group by SC.Sid,Sname having avg(score)>85;  

34、查詢課程名稱為“資料庫”,且分數低於60的學生姓名和分數

170   Select Sname,isnull(score,0)  

171   from Student,SC,Course  

172   where SC.Sid=Student.Sid and SC.Cid=Course.Cid and Course.Cname='資料庫'and score <60;  

35、查詢所有學生的選課情況;

173   SELECT SC.Sid,SC.Cid,Sname,Cname  

174   FROM SC,Student,Course  

175   where SC.Sid=Student.Sid and SC.Cid=Course.Cid ; 

36、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;

176   SELECT distinct student.Sid,student.Sname,SC.Cid,SC.score  

177   FROM student,Sc  

178   WHERE SC.score>=70 AND SC.Sid=student.Sid;  

37、查詢不及格的課程,並按課程號從大到小排列

179   select cid from sc where scor e <60 order by Cid ; 

38、查詢課程編號為003且課程成績在80分以上的學生的學號和姓名;

180   select SC.Sid,Student.Sname from SC,Student  

181   where SC.Sid=Student.Sid and Score>80 and Cid='003'; 

39、求選了課程的學生人數

182   select count(*) from sc;  

40、查詢選修“葉平”老師所授課程的學生中,成績最高的學生姓名及其成績

183   select Student.Sname,score  

184   from Student,SC,Course C,Teacher  

185   where Student.Sid=SC.Sid and SC.Cid=C.Cid and C.Tid=Teacher.Tid and Teacher.Tname='葉平'  

186   and SC.score=(select max(score)from SC where Cid=C.Cid );  

41、查詢各個課程及相應的選修人數

187   select count(*) from sc group by Cid;  

42、查詢不同課程成績相同的學生的學號、課程號、學生成績

188   select distinct A.Sid,B.score from SC A ,SC B where A.Score=B.Score and A.Cid <>B.Cid ; 

43、查詢每門功成績最好的前兩名

189   SELECT t1.Sid as 學生ID,t1.Cid as 課程ID,Score as 分數  

190   FROM SC t1  

191   WHERE score IN (SELECT TOP 2 score  

192   FROM SC  

193   WHERE t1.Cid= Cid  

194   ORDER BY score DESC  

195   )  

196   ORDER BY t1.Cid; 

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

197   select Cid as 課程號,count(*) as 人數  

198   from sc  

199   group by Cid  

200   order by count(*) desc,cid  

45、檢索至少選修兩門課程的學生學號

201   select Sid  

202   from sc  

203   group by sid  

204   having count(*) > = 2  

46、查詢全部學生都選修的課程的課程號和課程名

205   select Cid,Cname  

206   from Course  

207   where Cid in (select cid from sc group by cid)  

47、查詢沒學過“葉平”老師講授的任一門課程的學生姓名

208   select Sname from Student where Sid not in (select Sid from Course,Teacher,SC  

209   where Course.Tid=Teacher.Tid and SC.Cid=course.Cid and Tname='葉平');  

48、查詢兩門以上不及格課程的同學的學號及其平均成績

210   select Sid,avg(isnull(score,0)) from SC  

211   where Sid in (select Sid from SC where score <60  

212   group by Sid having count(*)>2)group by Sid; 

49、檢索“004”課程分數小於60,按分數降序排列的同學學號

213   select Sid from SC where Cid='004'and score <60 order by score desc;   

50、刪除“002”同學的“001”課程的成績

214   delete from Sc where Sid='001'and Cid='001';