正在进行安全检测...
发布时间:2024-03-08 05:35:10
50个常用的sql语句2011-06-2015:20:21
标签:sql数据库语言休闲职场
Student(S#,Sname,Sage,Ssex学生表Course(C#,Cname,T#课程表SC(S#,C#,score成绩表Teacher(T#,Tname教师表
问题:
1、查询“001”课程比“002”课程成绩高的所有学生的学号;
selecta.S#from(selects#,scorefromSCwhereC#='001'a,(selects#,scorefromSCwhereC#='002'b
wherea.score>b.scoreanda.s#=b.s#;
2、查询平均成绩大于60分的同学的学号和平均成绩;selectS#,avg(scorefromsc
groupbyS#havingavg(score>60;
3、查询所有同学的学号、姓名、选课数、总成绩;selectStudent.S#,Student.Sname,count(SC.C#,sum(scorefromStudentleftOuterjoinSConStudent.S#=SC.S#groupbyStudent.S#,Sname4、查询姓“李”的老师的个数;selectcount(distinct(TnamefromTeacher
whereTnamelike'李%';
5、查询没学过“叶平”老师课的同学的学号、姓名;selectStudent.S#,Student.SnamefromStudent
whereS#notin(selectdistinct(SC.S#fromSC,Course,TeacherwhereSC.C#=Course.C#andTeacher.T#=Course.T#andTeacher.Tname='叶平';
6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;
selectStudent.S#,Student.SnamefromStudent,SCwhereStudent.S#=SC.S#andSC.C#='001'andexists(Select*fromSCasSC_2whereSC_2.S#=SC.S#andSC_2.C#='002';7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;selectS#,SnamefromStudent
whereS#in(selectS#fromSC,Course,TeacherwhereSC.C#=Course.C#andTeacher.T#=Course.T#andTeacher.Tname='叶平'groupbyS#havingcount(SC.C#=(selectcount(C#fromCourse,TeacherwhereTeacher.T#=Course.T#andTname='叶平';
8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;SelectS#,Snamefrom(selectStudent.S#,Student.Sname,score,(selectscorefromSCSC_2whereSC_2.S#=Student.S#andSC_2.C#='002'score2
fromStudent,SCwhereStudent.S#=SC.S#andC#='001'S_2wherescore2
9、查询所有课程成绩小于60分的同学的学号、姓名;selectS#,SnamefromStudent
whereS#notin(selectStudent.S#fromStudent,SCwhereS.S#=SC.S#andscore>60;
10、查询没有学全所有课的同学的学号、姓名;selectStudent.S#,Student.SnamefromStudent,SC
whereStudent.S#=SC.S#groupbyStudent.S#,Student.Snamehavingcount(C#<(selectcount(C#fromCourse;
11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;
selectS#,SnamefromStudent,SCwhereStudent.S#=SC.S#andC#inselectC#fromSCwhereS#='1001';
12、查询至少学过学号为“001”同学所有一门课的其他同学学号和姓名;selectdistinctSC.S#,SnamefromStudent,SC
whereStudent.S#=SC.S#andC#in(selectC#fromSCwhereS#='001';
13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;