mysql部署练习_mysql基础练习练习

发布时间:2024-09-03 15:01

1、查询“001”课程比“002”课程成绩高的所有学生的学号

selecta.Sidfrom(selectsid,scorefromSCwhereCid='001') a,(selectsid,score

fromSCwhereCid='002') b

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

2、查询平均成绩大于60分的同学的学号和平均成绩;

selectSid,avg(score)

fromsc

groupbySidhavingavg(score) >60;

3、查询所有同学的学号、姓名、选课数、总成绩;

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

fromStudent left Outer join SConStudent.Sid=SC.Sid

groupbyStudent.Sid,Sname;

4、查询姓“李”的老师的个数;

selectcount(distinct(Tname))

fromTeacher

whereTname like '李%';

5、查询没学过“叶平”老师课的同学的学号、姓名;

selectStudent.Sid,Student.Sname

fromStudent

whereSid not in (selectdistinct( SC.Sid)fromSC,Course,Teacher

whereSC.Cid=Course.Cid and Teacher.Tid=Course.Tid and Teacher.Tname='叶平');

6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

selectStudent.Sid,Student.SnamefromStudent,SC

whereStudent.Sid=SC.Sid and SC.Cid='001'and

exists(Select*fromSCasSC_2whereSC_2.Sid=SC.Sid and SC_2.Cid='002');

7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

selectSid,Sname

fromStudent

whereSid in (selectSidfromSC ,Course ,Teacher

whereSC.Cid=Course.Cid and Teacher.Tid=Course.Tid and Teacher.Tname='叶平'

groupbySidhavingcount(SC.Cid)=(selectcount(Cid)fromCourse,Teacher

whereTeacher.Tid=Course.Tid and Tname='叶平'));

8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

26    SelectSid,Snamefrom(selectStudent.Sid,Student.Sname,score ,(selectscorefromSC SC_2

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

28    fromStudent,SCwhereStudent.Sid=SC.Sid and Cid='001') S_2wherescore2

9、查询所有课程成绩小于60分的同学的学号、姓名;

29    selectSid,Sname

30    fromStudent

31    whereSid not in (selectStudent.SidfromStudent,SCwhereS.Sid=SC.Sid and score>60);

10、查询没有学全所有课的同学的学号、姓名;

32    selectStudent.Sid,Student.Sname

33    fromStudent,SC

34    whereStudent.Sid=SC.Sid

35    groupbyStudent.Sid,Student.Snamehavingcount(Cid)

11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

36    selectSid,SnamefromStudent,SC

37    whereStudent.Sid=SC.Sid and Cid inselectCidfromSCwhereSid='1001';

12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;

38    selectdistinctSC.Sid,Sname

39    fromStudent,SC

40    whereStudent.Sid=SC.Sid and Cid in (selectCidfromSCwhereSid='001');

13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

41    updateSCsetscore=(selectavg(SC_2.score)

42    fromSC SC_2

43    whereSC_2.Cid=SC.Cid )fromCourse,Teacher

44    whereCourse.Cid=SC.Cid and Course.Tid=Teacher.Tid and Teacher.Tname='叶平');

14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;

45    selectSidfromSCwhereCid in (selectCidfromSCwhereSid='1002')

46    groupbySidhavingcount(*)=(selectcount(*)fromSCwhereSid='1002');

15、删除学习“叶平”老师课的SC表记录;

47     Delect SC

48    fromcourse ,Teacher

49    whereCourse.Cid=SC.Cid and Course.Tid= Teacher.Tid and Tname='叶平';

16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“003”课程的同学学号、2、号课的平均成绩;

50    InsertSCselectSid,'002',(Selectavg(score)

51    fromSCwhereCid='002')fromStudent

52    whereSid not in (SelectSidfromSCwhereCid='002');

17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按如下形式显示: 学生ID,,数据库,企业管理,英语,有效课程数,有效平均分

53    SELECTSidas学生ID

54     ,(SELECTscoreFROMSCWHERESC.Sid=t.Sid AND Cid='004')AS数据库

55     ,(SELECTscoreFROMSCWHERESC.Sid=t.Sid AND Cid='001')AS企业管理

56     ,(SELECTscoreFROMSCWHERESC.Sid=t.Sid AND Cid='006')AS英语

57     ,COUNT(*)AS有效课程数, AVG(t.score)AS平均成绩

58    FROMSCASt

59    GROUPBYSid

60    ORDERBYavg(t.score)

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

61    SELECTL.CidAs课程ID,L.scoreAS最高分,R.scoreAS最低分

62    FROMSC L ,SCASR

63    WHEREL.Cid = R.Cid and

64     L.score = (SELECTMAX(IL.score)

65    FROMSCASIL,StudentASIM

66    WHEREL.Cid = IL.Cid and IM.Sid=IL.Sid

67    GROUPBYIL.Cid)

68     AND

69     R.Score = (SELECTMIN(IR.score)

70    FROMSCASIR

71    WHERER.Cid = IR.Cid

72    GROUPBYIR.Cid

73     );

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序

74    SELECTt.CidAS课程号,max(course.Cname)AS课程名,isnull(AVG(score),0)AS平均成绩

75     ,100 * SUM(CASEWHENisnull(score,0)>=60THEN1ELSE0END)/COUNT(*)AS及格百分数

76    FROMSC T,Course

77    wheret.Cid=course.Cid

78    GROUPBYt.Cid

79    ORDERBY100 * SUM(CASEWHENisnull(score,0)>=60THEN1ELSE0END)/COUNT(*)DESC

20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)

80    SELECTSUM(CASEWHENCid ='001'THENscoreELSE0END)/SUM(CASE CidWHEN'001'THEN1ELSE0END)AS企业管理平均分

81     ,100 * SUM(CASEWHENCid = '001' AND score >= 60THEN1ELSE0END)/SUM(CASEWHENCid = '001'THEN1ELSE0END)AS企业管理及格百分数

82     ,SUM(CASEWHENCid = '002'THENscoreELSE0END)/SUM(CASE CidWHEN'002'THEN1ELSE0END)AS马克思平均分

83     ,100 * SUM(CASEWHENCid = '002' AND score >= 60THEN1ELSE0END)/SUM(CASEWHENCid = '002'THEN1ELSE0END)AS马克思及格百分数

84     ,SUM(CASEWHENCid = '003'THENscoreELSE0END)/SUM(CASE CidWHEN'003'THEN1ELSE0END)ASUML平均分

85     ,100 * SUM(CASEWHENCid = '003' AND score >= 60THEN1ELSE0END)/SUM(CASEWHENCid = '003'THEN1ELSE0END)ASUML及格百分数

86     ,SUM(CASEWHENCid = '004'THENscoreELSE0END)/SUM(CASE CidWHEN'004'THEN1ELSE0END)AS数据库平均分

87     ,100 * SUM(CASEWHENCid = '004' AND score >= 60THEN1ELSE0END)/SUM(CASEWHENCid = '004'THEN1ELSE0END)AS数据库及格百分数

88    FROMSC

21、查询不同老师所教不同课程平均分从高到低显示

89    SELECTmax(Z.Tid)AS教师ID,MAX(Z.Tname)AS教师姓名,C.CidAS课程ID,MAX(C.Cname)AS课程名称,AVG(Score)AS平均成绩

90    FROMSCAST,CourseASC ,TeacherASZ

91    whereT.Cid=C.Cid and C.Tid=Z.Tid

92    GROUPBYC.Cid

93    ORDERBYAVG(Score)DESC

22、查询如下课程成绩第 3 名到第 6 名的学生成绩单:企业管理(001),马克思(002),UML (003),数据库(004) [学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩

94    SELECTDISTINCTtop3

95     SC.SidAs学生学号,

96     Student.SnameAS学生姓名 ,

97     T1.scoreAS企业管理,

98     T2.scoreAS马克思,

99     T3.scoreASUML,

100   T4.scoreAS数据库,

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

102  FROMStudent,SC LEFT JOIN SCAST1

103  ONSC.Sid = T1.Sid AND T1.Cid = '001'

104   LEFT JOIN SCAST2

105  ONSC.Sid = T2.Sid AND T2.Cid = '002'

106   LEFT JOIN SCAST3

107  ONSC.Sid = T3.Sid AND T3.Cid = '003'

108   LEFT JOIN SCAST4

109  ONSC.Sid = T4.Sid AND T4.Cid = '004'

110  WHEREstudent.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  TOP15WITHTIES

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

117  FROMsc

118   LEFT JOIN scAST1

119  ONsc.Sid = T1.Sid AND T1.Cid = 'k1'

120   LEFT JOIN scAST2

121  ONsc.Sid = T2.Sid AND T2.Cid = 'k2'

122   LEFT JOIN scAST3

123  ONsc.Sid = T3.Sid AND T3.Cid = 'k3'

124   LEFT JOIN scAST4

125  ONsc.Sid = T4.Sid AND T4.Cid = 'k4'

126  ORDERBYISNULL(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  SELECTSC.Cidas课程ID, Cnameas课程名称

128   ,SUM(CASEWHENscore BETWEEN 85 AND 100THEN1ELSE0END)AS[100 - 85]

129   ,SUM(CASEWHENscore BETWEEN 70 AND 85THEN1ELSE0END)AS[85 - 70]

130   ,SUM(CASEWHENscore BETWEEN 60 AND 70THEN1ELSE0END)AS[70 - 60]

131   ,SUM(CASEWHENscore < 60THEN1ELSE0END)AS[60 -]

132  FROMSC,Course

133  whereSC.Cid=Course.Cid

134  GROUPBYSC.Cid,Cname;

24、查询学生平均成绩及其名次

135  SELECT1+(SELECTCOUNT(distinct平均成绩)

136  FROM(SELECTSid,AVG(score)AS平均成绩

137  FROMSC

138  GROUPBYSid

139   )AST1

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

141   Sidas学生学号,平均成绩

142  FROM(SELECTSid,AVG(score) 平均成绩

143  FROMSC

144  GROUPBYSid

145   )AST2

146  ORDERBY平均成绩desc;

25、查询各科成绩前三名的记录:(不考虑成绩并列情况)

147  SELECTt1.Sidas学生ID,t1.Cidas课程ID,Scoreas分数

148  FROMSC t1

149  WHEREscore IN (SELECTTOP3 score

150  FROMSC

151  WHEREt1.Cid= Cid

152  ORDERBYscoreDESC

153   )

154  ORDERBYt1.Cid;

26、查询每门课程被选修的学生数

155  selectcid,count(Sid)fromscgroupbyCid;

27、查询出只选修了一门课程的全部学生的学号和姓名

156  selectSC.Sid,Student.Sname,count(Cid)AS选课数

157  fromSC ,Student

158  whereSC.Sid=Student.SidgroupbySC.Sid ,Student.Snamehavingcount(Cid)=1;

28、查询男生、女生人数

159  Selectcount(Ssex)as男生人数fromStudentgroupbySsexhavingSsex='男';

160  Selectcount(Ssex)as女生人数fromStudentgroupbySsexhavingSsex='女';

29、查询姓“张”的学生名单

161  SELECTSnameFROMStudentWHERESname like '张%';

30、查询同名同性学生名单,并统计同名人数

162  selectSname,count(*)fromStudentgroupbySnamehavingcount(*)>1;;

31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)

163  selectSname, CONVERT(char(11),DATEPART(year,Sage))asage

164  fromstudent

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

32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

166  SelectCid,Avg(score)fromSCgroupbyCidorderbyAvg(score),CidDESC;

33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩

167  selectSname,SC.Sid ,avg(score)

168  fromStudent,SC

169  whereStudent.Sid=SC.SidgroupbySC.Sid,Snamehavingavg(score)>85;

34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数

170  SelectSname,isnull(score,0)

171  fromStudent,SC,Course

172  whereSC.Sid=Student.Sid and SC.Cid=Course.Cid and Course.Cname='数据库'and score <60;

35、查询所有学生的选课情况;

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

174  FROMSC,Student,Course

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

36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数;

176  SELECTdistinctstudent.Sid,student.Sname,SC.Cid,SC.score

177  FROMstudent,Sc

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

37、查询不及格的课程,并按课程号从大到小排列

179  selectcidfromscwherescor e <60orderbyCid ;

38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;

180  selectSC.Sid,Student.SnamefromSC,Student

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

39、求选了课程的学生人数

182  selectcount(*)fromsc;

40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩

183  selectStudent.Sname,score

184  fromStudent,SC,Course C,Teacher

185  whereStudent.Sid=SC.Sid and SC.Cid=C.Cid and C.Tid=Teacher.Tid and Teacher.Tname='叶平'

186   and SC.score=(selectmax(score)fromSCwhereCid=C.Cid );

41、查询各个课程及相应的选修人数

187  selectcount(*)fromscgroupbyCid;

42、查询不同课程成绩相同的学生的学号、课程号、学生成绩

188  selectdistinctA.Sid,B.scorefromSC A ,SC BwhereA.Score=B.Score and A.Cid <>B.Cid ;

43、查询每门功成绩最好的前两名

189  SELECTt1.Sidas学生ID,t1.Cidas课程ID,Scoreas分数

190  FROMSC t1

191  WHEREscore IN (SELECTTOP2 score

192  FROMSC

193  WHEREt1.Cid= Cid

194  ORDERBYscoreDESC

195   )

196  ORDERBYt1.Cid;

44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列

197  selectCidas课程号,count(*)as人数

198  fromsc

199  groupbyCid

200  orderbycount(*)desc,cid

45、检索至少选修两门课程的学生学号

201  selectSid

202  fromsc

203  groupbysid

204  havingcount(*) > = 2

46、查询全部学生都选修的课程的课程号和课程名

205  selectCid,Cname

206  fromCourse

207  whereCid in (selectcidfromscgroupbycid)

47、查询没学过“叶平”老师讲授的任一门课程的学生姓名

208  selectSnamefromStudentwhereSid not in (selectSidfromCourse,Teacher,SC

209  whereCourse.Tid=Teacher.Tid and SC.Cid=course.Cid and Tname='叶平');

48、查询两门以上不及格课程的同学的学号及其平均成绩

210  selectSid,avg(isnull(score,0))fromSC

211  whereSid in (selectSidfromSCwherescore <60

212  groupbySidhavingcount(*)>2)groupbySid;

49、检索“004”课程分数小于60,按分数降序排列的同学学号

213  selectSidfromSCwhereCid='004'and score <60orderbyscoredesc;

50、删除“002”同学的“001”课程的成绩

214  deletefromScwhereSid='001'and Cid='001';

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号