资源描述:
《SQL SERVER查询练习习题.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、..设教学数据库Education有三个关系:学生关系s(student_id,student_name,birthday,sex,major);学习关系sc(student_id,course_id,score);课程关系course(course_id,course_name,term,period,credit)查询问题:单表查询1:查所有年龄在20岁以下的学生及年龄。selectstudent_name,year(getdate())-year(birthday)as年龄fromswhereyea
2、r(getdate())-year(birthday)<302:查考试成绩有不及格的学生的学号selectdistinctstudent_idfromscwherescore<603:查所年龄在20至23岁之间的学生、系别及年龄。selectstudent_name,major,year(getdate())-year(birthday)as年龄fromswhereyear(getdate())-year(birthday)between20and244:查舞蹈编导、英语、通信技术专业的学生、性别。sel
3、ectstudent_name,sexfromswheremajorin('舞蹈编导','英语','通信技术')5:查不是舞蹈编导、英语、通信技术的学生、性别selectstudent_name,sexfromswheremajornotin('舞蹈编导','英语','通信技术')6:查所有姓“”的学生的、学号和性别。selectstudent_name,student_id,sexfromswherestudent_namelike'%'8:查所有不姓“”且单名的学生的。selectstudent_na
4、mefromswherestudent_namenotlike'_'9:查C++课程的课程号。selectcourse_idfromcoursewherecourse_name='C++'10:查缺考的学生的学号和课程号。selectstudent_id,course_idfromscwherescoreisnull11:查开课学期为空值的课程编号和名称。selectcourse_id,course_namefromcourse.....wheretermisnull12:查信息管理专业20岁以下的学生的
5、学号和。selectstudent_id,student_namefromswheremajor='信息管理'andyear(getdate())-year(birthday)<2013:查舞蹈编导专业性别为女的学生、性别。selectstudent_name,sexfromswheremajor='舞蹈编导'andsex='女'14:查询选修了110010课程的学生的学号和成绩,其结果按分数的降序排列。selectstudent_id,scorefromscwherecourse_id='110010'
6、orderbyscoredesc15:查询全体学生的情况,查询结果按所在专业升序排列,对同一系中的学生按年龄降序排列。select*fromsorderbymajorasc,year(getdate())-year(birthday)desc16:查询学生总人数。selectcount(*)as人数froms17:查询选修了课程的学生人数。selectcount(*)as选修课程人数fromsc18:计算选修了110010课程的学生平均成绩。(改为C++)selectavg(score)as平均成绩fro
7、mscwherecourse_id=(selectcourse_idfromcoursewherecourse_name='C++')19:查询学习110010课程的学生最高分数。selectmax(score)as最高分fromscwherecourse_id='110010'20:查询各个课程号与相应的选课人数。(改为课程名称)selectcourse_name,count(*)as人数fromsc,coursewheresc.course_id=course.course_idgroupbycour
8、se_name.....21:查询至少选修两门课程的学号。selectstudent_idfromscgroupbystudent_idhavingcount(*)>222:查询选修课程超过3门的学生及课程门数selectstudent_name,count(*)as课程门数froms,scwheres.student_id=sc.student_idgroupbystudent_namehavingcount(*)>3多表查