您好,欢迎访问三七文档
sql实例1001查询全体男同学信息情况select*fromstudentwheresex='男'1002查询选修了1号课的学生的学号和成绩selectsno,gradefromscwherecno='1'1003查询1989年以前出生的学生学号和姓名和出生日期(提示请用year(csrq)函数来取出生年号再与1989比较)selectsno,sname,csrqfromstudentwhereyear(csrq)<19891004查询信息系所有女生的学号,姓名,及所在系selectsno,sname,sdeptfromstudentwheresdept='信息系'andsex='女'1005查询课程名是数据库的课程号和学分selectcno,ccreditfromcoursewherecname='数据库'1006查询先行课号为5的课程号和课程名selectcno,cnamefromcoursewherecpno='5'1007查询英语系90后的学生情况(注90年后出生的)select*fromstudentwheresdept='英语系'andyear(csrq)>=19901008查询计算机系或信息系中年龄超过21岁的同学情况.(设当前年为2010年,提示用当前年减去出生年再与21岁比较)select*fromstudentwhere(sdept='计算机系'orsdept='信息系')and(2010-year(csrq))>211009请将3号课及4号课学生的学号课程号及成绩显示出来selectsno,cno,gradefromscwherecno='3'orcno='4'1010查询所有先行课程号为0且学分为2的课程名,先行课程号,及学分selectcname,cpno,ccreditfromcoursewherecpno='0'andccredit=22010查询学分为3以上(不含3),且课程名中包含着'数据'二字的所有课程名及学分selectcname,ccreditfromcoursewhereccredit>3andcnamelike'数据%'2001查询年龄在20-23岁之间(含20与23岁)的学生姓名,系别,年龄(请不要用between语句提交当前年2010)selectsname,sdept,(2010-year(csrq))asagefromstudentwhere(2010-year(csrq))>=20and(2010-year(csrq))<=232002查询选修过课程的所有学生的学号(不许重复)selectdistinctsnofromsc2003请将选修了课程的同学按课程号升序,成绩降序排序select*fromscorderbycno,gradedesc2004请将选了1号课程的同学按成绩降序排序select*fromscwherecno='1'orderbygradedesc2005查询选修了1号课的成绩大于85分的学号和成绩selectsno,gradefromscwherecno='1'andgrade>852006查询所有李姓同学情况select*fromstudentwheresnamelike'李%'2007查询所有两字姓名的张姓同学的姓名,性别与系别selectsname,sex,sdeptfromstudentwheresnamelike'张_'2008查询名字中第3个字为铃的学生的姓名和学号selectsname,snofromstudentwheresnamelike'__铃'2009查询信息系所有不姓刘的同学的学号和姓名selectsno,snamefromstudentwheresdept='信息系'andsnamenotlike'刘%'3001查询所有选修过课的学生的姓名,课程名及成绩selectsname,cname,gradefromstudent,sc,coursewherestudent.sno=sc.snoandsc.cno=course.cnoand(cname='数据库'orcname='信息系统')3002查询选修了1号课且成绩大于80分的的同学的姓名,课程号及成绩selectsname,cno,gradefromstudent,scwherestudent.sno=sc.snoandcno='1'andgrade>803003查询没有选1号课的女生姓名,课程号及成绩,并将成绩按降序排序selectsname,cno,gradefromstudent,scwherestudent.sno=sc.snoandnot(cno='1')andsex='女'orderbygradedesc3004查询选修了数据库课的所有男生的姓名及该课的成绩selectsname,gradefromstudent,sc,coursewherestudent.sno=sc.snoandsc.cno=course.cnoandsex='男'andsdept='计算机系'andcname='数据库'3005查询选修了数据库课的最高成绩.最低成绩和平均成绩(注用as来表示最低,最高及平均成绩selectmax(grade)as'最高成绩',min(grade)as'最低成绩',avg(grade)as'平均成绩'fromsc,coursewheresc.cno=course.cnoandcname='数据库'3006查询选修了1号课或3号课的学生的学号,姓名,成绩(请用谓词的方法)请参考光盘课件简单查询例7selectsc.sno,sname,gradefromstudent,scwherestudent.sno=sc.snoandcnoin('1','3')3007查询李勇同学的所有选修过课程的总成绩及平均成绩并用“总成绩”及“平均成绩”表示出来selectsum(grade)as总成绩,avg(grade)as平均成绩fromstudent,scwherestudent.sno=sc.snoandsname='李勇'3008查询计算机系所有选了数据库成绩在80分以上的同学的姓名及成绩selectsname,gradefromstudent,sc,coursewherestudent.sno=sc.snoandsc.cno=course.cnoandcname='数据库'andsdept='计算机系'andgrade>803009查询选修李勇同学所学课程的学分总和(注用as总学分)selectsum(ccredit)as总学分fromstudent,sc,coursewherestudent.sno=sc.snoandsc.cno=course.cnoandsname='李勇'3010查寻选了1号课,计算机系,女生的人数.3011"selectcount(*)as人数fromstudent,scwherestudent.sno=sc.snoandcno='1'andsdept='计算机系'andsex='女';"4001查询所有选修课平均成绩大于85分的同学的学号和平均成绩。selectsno,avg(grade)fromscgroupbysnohavingavg(grade)>854002查询至少选修过2门课程的信息系学生学号selectsc.snofromsc,studentwherestudent.sno=sc.snoandsdept='信息系'groupbysc.snohavingcount(cno)>=24003查询各系女生人数,分别用系,女生人数表示出来。selectsdept,count(sex)fromstudentwheresex='女'groupbysdept4004查询各门课程所选的人数并用课程号及所选人数表示出来selectcno,count(*)as所选人数fromscgroupbycno4005查询计算机系所选课程中每个同学的平均成绩,并用学号和平均成绩表示出来selectsc.sno,avg(grade)as平均成绩fromstudent,scwherestudent.sno=sc.snoandsdept='计算机系'groupbysc.sno4006查询选修的学分数超过8个学分的同学学号及学分数"selectsno,sum(ccredit)fromsc,coursewheresc.cno=course.cnogroupbysnohavingsum(ccredit)>8"4007查询李勇,刘晨两位同学的平均成绩,并用学号和平均成绩表示出来selectsc.sno,avg(grade)as平均成绩fromsc,studentwheresc.sno=student.snoandsnamein('李勇','刘晨')groupbysc.sno4008查询信息系仅选过一门课的同学学号selectsc.snofromstudent,scwherestudent.sno=sc.snoandsdept='信息系'groupbysc.snohavingcount(*)=14009查询数据库和数学这两门课的选课人数,并用课程号和选课人数表示出来"selectsc.cnoas课程号,count(*)as选课人数fromsc,coursewheresc.cno=course.cnoandcnamein('数据库','数学')groupbysc.cno"4010查询每位同学选修课的平均成绩,并按平均成绩从高到低排序,同时要用学号和平均成绩表示出来selectsno,avg(grade)as平均成绩fromscgroupbysnoorderbyavg(grade)desc5001查询同时选修了1号课和3号课的同学的姓名及系5002"selectsname,sdeptfromstudentwheresnoin(selectsnofromscwheresnoin(selectsnofromscwherecno='1')andcno='3')"5002查询同时选修了数据库及数学的同学的学号selectsnofromscwheresnoin(selectsnofromscwherecnoin(selectcnofromcoursewherecname='数据库'))andcnoin(selectcnofromcoursewherecname='数学')5003查询选修了全部课程的同学的姓名selectsnamefromstudentwheresnoin(selectsnofromscgroupbysnohavingcount(*)=(selectcount(*)fromcourse))5004查询与李勇同在一个系,且年龄比他小的同学的姓名(提示用year(csrq)
本文标题:数据库SQL
链接地址:https://www.777doc.com/doc-3680453 .html