zoukankan      html  css  js  c++  java
  • SQL面试题

    1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
    select *
    from score sc1,score sc2
    where sc1.c_id='01' and sc2.c_id='02'
    and sc1.s_id = sc2.s_id and sc1.score > sc2.score;

    2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
    select *
    from score sc1,score sc2
    where sc1.c_id='01' and sc2.c_id='02'
    and sc1.s_id = sc2.s_id and sc1.score < sc2.score;
    3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
    select s.s_id,stu.s_sname,avg(s.score) from score s
    inner join student stu on stu.s_id = s.s_id
    group by s.s_id having avg(s.score) >=60
    4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩
    select student.sid,student.sname,avg(sc.score)
    from student,sc
    where student.sid = sc.scid group by student.sid
    having avg(sc.score)<60


    5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
    select s.s_id '编号',s.s_sname '学生姓名',ss.countcou ' 选课总数',
    ss.sumscore '总成绩' from student s,
    (select sc.s_id, sum(sc.score) as sumscore,count(sc.c_id) as countcou from score sc group by sc.s_id) as ss where s.s_id = ss.s_id


    6、查询"李"姓老师的数量
    select count(*) from teacher t where t.t_name like '李%';
    7、查询学过"张三"老师授课的同学的信息
    select * from student stu
    where stu.s_id in(
    select sc.s_id from score sc
    where sc.c_id in(
    select c.c_id from course c
    where c.t_id = (
    select t.t_id from teacher t
    where t.t_name = '张三'
    )
    )
    )
    8、查询没学过"张三"老师授课的同学的信息
    select * from student stu
    where stu.s_id not in(
    select sc.s_id from score sc
    where sc.c_id in(
    select c.c_id from course c
    where c.t_id = (
    select t.t_id from teacher t
    where t.t_name = '张三'
    )
    )
    )

    9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
    select * from student
    where s_id in (select sc1.s_id from score sc1,score sc2
    where sc1.s_id = sc2.s_id and sc1.c_id = '01' and sc2.c_id = '02');
    10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
    select s.*, sc1.score,sc2.score from student s
    left join (select * from score where c_id = '01') sc1 on s.s_id = sc1.s_id
    left join (select * from score where c_id = '02') sc2 on s.s_id = sc2.s_id
    where sc1.c_id = '01' and sc2.c_id is null;

    11、查询没有学全所有课程的同学的信息
    select * from student stu
    where stu.s_id in(
    select sc.s_id from score sc
    group by sc.s_id
    having count(sc.c_id) < (select count(*) from course))

    12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
    select * from student stu
    where stu.s_id in(
    select sc1.s_id from score sc1
    where sc1.course in(
    select sc2.c_id from score sc2
    where sc2.s_id ='01'))
    and stu.s_id !='01';
    13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
    select * from student where s_id in
    (select s_id from score where s_id not in
    (select s_id from score where c_id not in
    (select c_id from score where s_id = '01'
    )
    ) group by s_id
    having count(c_id) =
    (select count(c_id) from score where s_id = '01')
    and s_id != '01'
    )
    14、查询没学过"张三"老师讲授的任一门课程的学生姓名
    select stu.s_name from student stu where stu.s_id not in
    (select score.s_id from score where score.c_id in
    (select course.c_id from course,teacher where course.t_id and t_name='张三'));

    15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
    select
    student.s_id,
    student.s_name,
    avg(score.score)
    from student,score
    where student.s_id = score.s_id
    and score.score < 60 group by score.s_id
    having count(*) > 1;
    16、检索"01"课程分数小于60,按分数降序排列的学生信息
    select s.*,b.score from student a
    inner join score b
    where b.c_id='01'and b.score < 60
    order by score desc;
    17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
    第一种:
    select a.*,b.sc01,c.sc02,d.sc03,avg(dd.score) as avgN
    from student a
    left join (
    select s_id,score as 'sc01' from score where c_id='01'
    ) b on a.s_id=b.s_id
    left join (
    select s_id,score as 'sc02' from score where c_id='02'
    ) c on a.s_id=c.s_id
    left join (
    select s_id,score as 'sc03' from score where c_id='03'
    ) d on a.s_id=d.s_id
    left join score dd on a.s_id=dd.s_id
    group by s_id
    order by avg(dd.score) desc
    第二种:

    SELECT a.*
    ,SUM(CASE WHEN b.c_id='01' THEN b.score ELSE 0 END) AS s01
    ,SUM(CASE WHEN b.c_id='02' THEN b.score ELSE 0 END) AS s02
    ,SUM(CASE WHEN b.c_id='03' THEN b.score ELSE 0 END) AS s03
    ,AVG(CASE WHEN b.score IS NULL THEN 0 ELSE b.score END) avs
    FROM student a
    LEFT JOIN score b
    ON a.s_id=b.s_id
    GROUP BY a.s_id,a.s_name,a.s_age,a.s_sex
    ORDER BY avs DESC;


    18、查询每门课程被选修的学生数
    select
    sc.c_id,count(*)
    from score sc
    group by sc.c_id;

    19、查询出只有两门课程的全部学生的学号和姓名
    select stu.s_id,stu.s_name,count(sc.c_id) '选修课程俩门' from
    student as stu
    inner join
    score as sc on stu.s_id = sc.s_id
    group by stu.s_id having count(sc.c_id)=2;
    20、查询男生、女生人数
    select nv.a '男',nan.b '女' from
    (select count(*) as a from student where s_sex like '女') nv,
    (select count(*) as b from student where s_sex like '男') nan

    21、查询名字中含有"风"字的学生信息
    select * from student where s_name like '%风%';

    22、查询同名同姓学生名单,并统计同名人数
    select s_name,count(*) from student
    group by s_name having count(s_name) > 1

    23、查询1990年出生的学生名单
    select * from student where s_age like '1990%';

    24、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号
    selec c_id,avg(score) from
    score grouo by c_id order by avg(score) desc,c_id desc;
    25、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩
    select * from student stu inner join score sc on stu.s_id = sc.s_id
    group by stu.s_id having
    avg(sc.score)>=85
    26、查询课程名称为"数学",且分数低于60的学生姓名和分数
    select b.sname,a.score from (select * from sc where score<60
    and cid =(select cid from course where cname='数学')) A
    left join student B
    on A.scid = b.sid;
    27、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
    select st.s_name as 姓名,co.c_name as 课程名,sc.score as 分数
    from score sc,course co,student st
    where sc.score > 70 and
    sc.s_id = st.s_id and
    sc.c_id = co.c_id;
    27-1,查询所有课程成绩在70分以上的姓名、课程名称和分数
    SELECT a.s_id,c.s_name,a.score FROM
    (SELECT s_id,score,COUNT(c_id)as NN FROM score
    WHERE score > 70 GROUP BY s_id)as a
    INNER JOIN
    (SELECT s_id,COUNT(c_id)as NN FROM score
    GROUP BY s_id)as b
    ON a.s_id=b.s_id
    INNER join
    student as c
    ON a.s_id=c.s_id

    select s.s_name '姓名',c_name '课程',a.score '分数'
    from student s,
    (select s_id,score,count(c_id)as nn from score
    where score > 70 group by s_id)as a,
    (select s_id,count(c_id)as nn from score group by s_id)as b,
    course d
    where a.nn = b.nn and a.s_id = b.s_id and s.s_id = b.s_id
    group by s.s_name,a.score,c_name

    28、查询不及格的课程
    select c_id from score where score < 60 group by c_id


    29、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名
    select stu.s_id '学号',stu.s_sname '姓名' from score sc
    inner join student stu on sc.s_id = stu.s_id = 01 and
    sc.score > 80;

    30、求每门课程的学生人数
    select c.c_name as 课程名,count(*) as 课程人数
    from course c inner join score s on c.c_id = s.c_id
    group by c.c_id,c.c_name
    31、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

    select distinct A.s_id,B.score from score A ,score B where A.score=B.score and A.c_id <>B.c_id ;

    32、查询每门功成绩最好的前两名
    select sc.s_id as 学生ID,sc.c_id as 课程ID,score as 分数
    from score sc
    where score in (select top2 score
    from score
    where sc.c_id= c_id
    order by score desc
    )
    order by sc.c_id;

    33、检索至少选修两门课程的学生学号
    select s_id
    from score
    group by s_id
    having count(*) >= 2

    34、查询选修了全部课程的学生信息
    select *
    from course
    where c_id in (select c_id from score group by c_id)

  • 相关阅读:
    使用Eclipse进行远程调试【转】
    JRE_HOME environment variable is not defined correctly This environment variableis needed to run this program
    Window 通过cmd查看端口占用、相应进程、杀死进程等的命令【转】
    A cycle was detected in the build path of project
    调用CXF工具 生成 WSDL【转】
    解决cxf+spring发布的webservice,types,portType和message以import方式导入
    Target runtime com.genuitec.runtime.generic.jee50 is not defined
    修改eclipse启动时eclipse使用的jre
    JAVA中堆栈和内存分配原理
    JVM -Xss调整Stack Space的大小 【转】
  • 原文地址:https://www.cnblogs.com/lxn521/p/13646248.html
Copyright © 2011-2022 走看看