有如下四張表:
學生表Student(stuId,stuName,stuAge,stuSex);
課程表Course(courseId,courseName,teacherId);
成績表Scores(stuId,courseId,score);
教師表Teacher(teacherId,teacherName);
有如下10個問題:
-
查詢“001”課程比“002”課程成績高的所有學生的學號
SELECT tblstudent.StuId
from tblstudent
where
(SELECT TC.Score FROM tblscore TC WHERE TC.CourseId='001'and tblstudent.StuId=tc.StuId)
>
(SELECT tb.Score FROM tblscore Tb WHERE Tb.CourseId='002' and tblstudent.StuId=Tb.StuId) -
查詢平均成績大於60分的同學的學號和平均成績
select Student.stuId,avg(Scores.score)
from Student,Scroes
where Studen.stuId=Scores.stuId
Group by Student.stuId
having avg(Scores.score)>60;
聚合函數是不能連接在where子句后面的。 -
查詢所有同學的學號,姓名,選課數,總成績
Select StuId,StuName,
(Select Count(CourseId) From tblScore t1 Where t1.StuId=s1.StuId)SelCourses,
(Select Sum(Score) From tblScore t2 Where t2.StuId=s1.StuId) SumScore
From tblStudent s1 -
查詢姓李的老師的個數
select count(Teacher.TeacherId)
from Teacher
where Teacher.TeacherName like "李%"; -
查詢沒有學過葉平老師課的同學的學號,姓名
詳情參見:https://www.cnblogs.com/shenxiaoquan/p/6115347.html -
查詢學過“001”課程並且也學過“002”課程的同學的學號,姓名
詳情參見:https://www.cnblogs.com/shenxiaoquan/p/6115563.html -
查詢學過葉平老師所教的所有課程的同學的學號,姓名
詳情參見:https://www.cnblogs.com/shenxiaoquan/p/6119698.html -
查詢課程編號002的成績比課程編號001低的所有的同學的學號,姓名
詳情參見:https://www.cnblogs.com/shenxiaoquan/p/6120207.html -
查詢所有課程成績小於60分的 同學的學號,姓名
select tblstudent.StuId,tblstudent.StuName
from tblstudent
where tblstudent.StuId NOT IN
(select tblscore.StuId from tblscore where tblstudent.StuId=tblscore.StuId AND tblscore.Score>60) -
查詢沒有學全所有課程的同學的學號,姓名
詳情參見:https://www.cnblogs.com/shenxiaoquan/p/6121656.html