预览加载中,请您耐心等待几秒...
在线预览结束,喜欢就下载吧,查找使用更方便
如果您无法下载资料,请参考说明:
1、部分资料下载需要金币,请确保您的账户上有足够的金币
2、已购买过的文档,再次下载不重复扣费
3、资料包下载后请先用软件解压,在使用对应软件打开
数据库的分组查询和统计查询在groupby之后不能使用where,只能使用having,在groupby之前可以使用where,即表示对过滤后的结果分组selectsname,sum(score)froms_kgroupbysnameselectcount(distinctsname)froms_kselectsname,avg(score)froms_kgroupbysnameselectkname,avg(score),max(score),min(score)froms_kgroupbyknameselectavg(score),max(score),min(score)froms_kwherekname='语文'selectsname,sum(score),avg(score)froms_kgroupbysnameselect*froms_k;selectkname,avg(score)avgsfroms_kgroupbykname一.使用聚集函数:1.查询学生总人数:SelectCount(*)as学生总数fromstudent2.查询选修了课程的学生总数:selectcount(distinctsno)as选课学生总数fromsc3.查询所有课程的总学分数和平均学分数,以及最高学分和最低学分:selectsum(credit)as总credit,avg(credit)as课程平均学分,max(credit)as最高学分,min(credit)as最低学分fromcourse4.计算1号课程的学生的平均成绩,最高分和最低分:selectavg(grade)as平均成绩,max(grade)as最高分,min(grade)as最低分fromscwherecno='1'5.查询’信息系’(IS)学生”数据结构”课程的平均成绩:selectavg(grade)fromstudent,course,scwherestudent.sno=sc.snoand(selectmax(grade)fromscwheresno=A.sno)7*.求成绩低于该门课程平均成绩的学生的成绩信息(sno,cno,grade)select*fromgradeAwheregrade=(selectavg(grade)fromscwherecno=A.cno)二.分组查询8.查询各系的学生的人数并按人数从多到少排序:selectsdept,Count(*)as人数fromstudentgroupbysdeptorderby人数desc9.查询各系的男女生学生总数,并按系别,升序排列,女生排在前:selectsdept,ssex,Count(*)as人数fromstudentgroupbysdept,ssexorderbysdept,ssexdesc10.查询选修了3门课程已上的学生的学号和姓名:selectsno,snamefromstudentwheresnoin(selectsnofromscgroupby(sno)havingcount(*)>3)1选课门数:selectsno,avg(grade)as平均成绩,max(grade)as最高分,min(grade)as最低分,count(*)as选课门数fromscgroupbysno12.查询至少选修了2门课程的学生的平均成绩:selectsno,avg(grade)as平均成绩,fromscgroupbysnohavingcount(*)>=213.查询平均分超过80分的学生的学号和平均分:Selectsno,avg(grade)as平均成绩fromscgroupbysnohavingavg(*)>=80比较:求各学生的60分以上课程的平均分:selectsno,avg(grade)as平均成绩fromscwheregrade>=60groupbysno14.查询”信息系”(IS)中选修了5门课程以上的学生的学号:selectsnofromscwheresnoin(selectsnofromstudentwheresdept='IS')groupbysnohavingcount(*)>=2三.集合查询15.查询数学系和信息系的学生的信息;select*fromstudentwheresdept=’MA’unionselect*fromstudentwheresdept='IS'16.查询选修了1号课程或2号课程的学生的学号:selectsnofromscwherecno='1'Unionselectsnofromscwherecno='2'比较实验三之3.