面试题一SQL语句

面试题一SQL语句

学生表:

Student(sid,sname,sage,ssex) --学生编号,学生姓名,出生日期,学生性别

课程表:

Course(cid,cname,tid) --课程编号,课程名称,教师编号

教师表

Teacher(tid,tname) --教师编号,教师姓名

成绩表

Sc(sid,cid,score) --学生编号,课程编号,分数

SQL语句:

  1. 查询“01”课程比“02”课程成绩高的学生的信息及课程分数

    select a.*,b.score,c.score
    from student a
    left join sc b on a.sid=b.sid and b.cid='01'
    left join sc c on a.sid=c.sid and c.cid='02'
    where b.score>c.score
    
  2. 查询平均成绩大于等于60分的同学的学生编号,学生姓名和平均成绩

    select a.sid,a.sname,avg(sc.score) avg_score
    from student a,sc
    where a.sid = sc.sid
    group by a.sid,a.sname
    having avg_score>=60
    order by a.sid;
    
  3. 查询所有学生的学生编号,学生姓名,选课总数,所有课程订单总成绩

    select a.sid,a.sname,count(b.cid),sum(b.score)
    from student a
    left join sc b
    on a.sid=b.sid
    group by a.sid,a.sname
    order by a.sid
    
  4. 统计课程分01最高的前三名学生信息

    select a.sname,sc.score,sc.cid
    from student a
    join sc 
    on a.sid=sc.sid
    where sc.cid='01'
    order by sc.score desc limit 3;
    
  5. 统计每个学生的选课总数

    select a.sid,a.sname,count(b.cid)
    from student a
    left join sc b
    on a.sid=b.sid
    group by a.sid,a.sname
    
  6. 查询没学过“张三”老师授课的学生信息

    select a.*
    from student a
    left join sc b
    where not exists(
        select *
        from course c
        inner join teacher d
        on c.tid=d.tid
        inner join sc e
        on c.cid=e.cid
        where d.tname='张三'
        and e.sid = a.sid
    )
    group by 1,2,3,4;
    
  7. 查询学过编号“01”并且也学过编号“02”的课程的学生信息

    select a.*
    from student a
    inner join sc b
    on a.sid = b.sid and b.cid ='01'
    inner join sc c
    on a.sid = c.sid on c.cid='02';
    
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章