javaSql面试题(10题)

有如下四张表:学生表Student(stuId,stuName,stuAge,stuSex);课程表Course(courseId,courseName,teacherId);成绩表Scores(stuId,courseId,score);教师表Teacher(teacherId,teacherName);有...

有如下四张表:
学生表Student(stuId,stuName,stuAge,stuSex);
课程表Course(courseId,courseName,teacherId);
成绩表Scores(stuId,courseId,score);
教师表Teacher(teacherId,teacherName);
有如下10个问题:

  1. 查询“001”课程比“002”课程成绩高的所有学生的学号
    select stuId
    from Scores s1,Scores s2
    where
    s1.stuId=s2.stuId and s1.courseId="001" and s2.courseId="002" and s1.score>s2.score;
    此题是一个自连接查询,也就是两个表都是同一张表。

  2. 查询平均成绩大于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子句后面的。

  3. 查询所有同学的学号,姓名,选课数,总成绩
    select Student.stuId,Student.stuName,count(Course.courseId),sum(Scores.score)
    from Student,Course,Scores
    where Studend.stuId=Scores.stuId and Scores.courseId=Course.courseId
  4. 查询姓李的老师的个数
    select count(Teacher.TeacherId)
    from Teacher
    where Teacher.TeacherName like "李%";

本文标题为:javaSql面试题(10题)

基础教程推荐