记录网上看到的sql面试题,自己填写答案

表的结构
    Student(S#,Sname,Sage,Ssex)    学生表
    Course(C#,Cname,T#)        课程表
    SC(S#,C#,score)           成绩表
    Teacher(T#,Tname)         教师表

    1.创建对应的表
    create table student(
            `s#` int,
            sname varchar(32),
            sage int,
            ssex varchar
            )以下省略
    2.插入数据
    insert into student select 1,'张三',18,‘男’ union all 
    select 2,'李四',18,‘男’ union all ....
    3.查询“001”课程比“002”课程成绩高的所有学生的学号;
        select a.`s#' from sc a,sc b where a.`c#`=001 and b.`c#`=002 and a.score>b.score;

![](http://i2.51cto.com/images/blog/201805/22/696290ad72878304ef70251fbdec2afb.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

4.查询平均成绩大于60分的同学的学号和平均成绩; 
select `s#`,avg(score) from sc group by `s#` having avg(score)>60
![](http://i2.51cto.com/images/blog/201805/22/985a3a24041520bae9be634704eb611c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)![](http://i2.51cto.com/images/blog/201805/22/985a3a24041520bae9be634704eb611c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

猜你喜欢

转载自blog.51cto.com/12390959/2119089