表结构:

    1. 1.学生表
    2. Student(s_id,s_name,s_birth,s_sex) –学生编号,学生姓名, 出生年月,学生性别
    3. 2.课程表
    4. Course(c_id,c_name,t_id) –课程编号, 课程名称, 教师编号
    5. 3.教师表
    6. Teacher(t_id,t_name) –教师编号,教师姓名
    7. 4.成绩表
    8. Score(s_id,c_id,s_score) –学生编号,课程编号,分数
    1. 查询”01”课程比”02”课程成绩高的学生 的信息及课程分数
      1. 从成绩表中获取学生 id 以及成绩
      2. 通过id获取到学生的信息
      3. select student.*,a.s_score as 01_score,b.s_score as 02_score
      4. from score a
      5. inner join student
      6. on a.s_id=student.s_id
      7. and a.c_id='01'
      8. left join score b
      9. on student.s_id=b.s_id
      10. and b.c_id='02'
      11. where a.s_score>b.s_score;
    1. 查询”01”课程比”02”课程成绩低的学生的信息及课程分数
      1. select student.*,a.s_score as 01_score,b.s_score as 02_score
      2. from student
      3. join score a
      4. on a.s_id=student.s_id and a.c_id='01'
      5. left join score b
      6. on b.s_id=student.s_id and b.c_id='02'
      7. where a.s_score<b.s_score;
    1. 查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

      1. --分组 having
      2. select student.s_id,student.s_name,avg(score.s_score) as avg_score
      3. from student
      4. inner join score
      5. on score.s_id=student.s_id
      6. group by score.s_id
      7. having avg(score.s_score) >= 60;


      可以使用 四舍五入 函数 round()

    2. 查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩— (包括有成绩的和无成绩的)

      1. select student.s_id,student.s_name,round(avg(score.s_score),2) as avg_score
      2. from student
      3. inner join score
      4. on score.s_id=student.s_id
      5. group by score.s_id
      6. having avg_score < 60
      7. union all
      8. select student.s_id,student.s_name,0 as avg_score
      9. from student
      10. where student.s_id
      11. not in (select distinct s_id from score);
    1. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
      1. select student.s_id,student.s_name,sum(score.s_score),count(score.c_id)
      2. from student
      3. left join score
      4. on student.s_id = score.s_id
      5. group by student.s_id;
    1. 查询”李”姓老师的数量
      1. select count(t_name)
      2. from teacher
      3. where t_name like '李%';
    1. 查询学过”张三”老师授课的同学的信息
      1. select student.*
      2. from student
      3. inner join score
      4. on student.s_id = score.s_id
      5. where score.c_id in
      6. (select course.c_id
      7. from course
      8. where course.t_id =
      9. (select teacher.t_id
      10. from teacher
      11. where teacher.t_name = '张三'
      12. )
      13. );
    1. 查询没学过”张三”老师授课的同学的信息 ```sql select student.* from student where student.s_id not in (select score.s_id from score where score.c_id in
      1. (select course.c_id
      2. from course
      3. where course.t_id =
      4. (select teacher.t_id
      5. from teacher
      6. where teacher.t_name = '张三'
      7. )
      8. )
      );

    select student.* from student where student.s_id not in( select student.s_id from student inner join score on student.s_id = score.s_id where score.c_id in (select course.c_id from course where course.t_id = ( select teacher.t_id from teacher where teacher.t_name = ‘张三’ ) ) );

    1. 9. 查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
    2. ```sql
    3. select student.*
    4. from student
    5. inner join score
    6. on student.s_id=score.s_id
    7. inner join score s2
    8. on student.s_id=s2.s_id
    9. where score.c_id='02' and s2.c_id ='01';
    1. 查询学过编号为”01”但是没有学过编号为”02”的课程的同学的信息
    1. select student.*
    2. from student
    3. where student.s_id
    4. in (
    5. select score.s_id
    6. from score
    7. where score.c_id='01'
    8. )
    9. and student.s_id
    10. not in(
    11. select score.s_id
    12. from score
    13. where score.c_id='02'
    14. );
    1. 查询没有学全所有课程的同学的信息
      1. -- 利用内交,存在不匹配则不返回
      2. select student.*
      3. from student
      4. where student.s_id
      5. not in (
      6. select a.s_id
      7. from score a
      8. inner join score b
      9. on a.s_id = b.s_id and b.c_id = '02'
      10. inner join score c
      11. on a.s_id = c.s_id and c.c_id = '03'
      12. );
    1. 查询至少有一门课与学号为”01”的同学所学相同的同学的信息
      1. select student.*
      2. from student
      3. where student.s_id
      4. in(
      5. select distinct a.s_id
      6. from score a
      7. where a.c_id
      8. in(
      9. select b.c_id
      10. from score b
      11. where b.s_id = '01'
      12. )
      13. );
    1. 查询和”01”号的同学学习的课程完全相同的其他同学的信息
      1. select student.*
      2. from student
      3. where student.s_id
      4. in(
      5. select distinct s_id
      6. from score
      7. where s_id != '01' and c_id in (
      8. select c_id
      9. from score
      10. where s_id = '01'
      11. )
      12. group by s_id
      13. having count(1) = (
      14. select count(1) from score where s_id = '01'
      15. )
      16. );
    1. 查询没学过”张三”老师讲授的任一门课程的学生姓名
      1. select student.s_name
      2. from student
      3. where student.s_id not in (
      4. select distinct s_id
      5. from score
      6. where c_id in (
      7. select c_id
      8. from course
      9. where t_id = (
      10. select t_id
      11. from teacher
      12. where t_name = '张三'
      13. )
      14. )
      15. );