zoukankan      html  css  js  c++  java
  • sql语言复习:讨论在学生-学习-课程表上

    一.在依然的比较简单熟稔的学生-学习-课程表中讨论:

    1.检索至少选修1门课程的女学生的姓名。

    分析:很明显可以用统计函数count(),有选修,有姓名,涉及到学习表和学生表。

    检索至少选修1门课程的学生的姓名

    select sname
    from student,sc
    where student.sno=sc.sno and ssex='女'
    group by student.sno
    having count(*)>=1;
    

      

    2.检索龙同学不学的课程的课程号。

    分析:字符串模糊匹配用like

    /*检索龙同学不学的课程的课程号*/
    select cno
    from c
    where cno not in
    (select cno 
    from sc,student
    where sc.sno=student.sno and sname like '龙%');
    

      

    3.检索至少选修两门课程的学生学号。

    分析:where语句中万万不能加上统计函数(count(),sum())哦。集合函数要么是出现在最开头的select语句中,要么是出现在having语句中的。

    /*检索至少选修两门课程的学生学号*/
    select distinct sno
    from sc
    group by sno
    having count(*)>=2;
    

    4.检索全部学生都选修的课程的课程号与课程名。

    分析:基本上吧,考试的时候一定会考一道除法查询题------>"全部","所有","至少同时包含"(所有后面紧跟着的是名词!)的十有八九。顺序呀可能就是5道题的正中间第3道题目哦2333!

    /*检索全部学生都选修的课程的课程号与课程名*/
    select cno,cname
    from c
    where not exists
    (select * from student where not exists
    (select * from sc where sno=student.sno and cno=c.cno));
    

      

    5.统计有学生选修的课程门数。

    分析:统计类问题是会考的,一般是在五道小题的第4小题吧2333

    /*统计有学生选修的课程门数*/
    select count(distinct cno)
    from sc;
    

    6.求学分为2的每门课程的学生平均成绩。

    分析:求学分为2的每门课程的学生平均成绩。"每门","各门"这两个字要圈起来划重点这是要考的嘤嘤嘤。每门各门,当即放下一个group by 2333.

    /*求学分为2的每门课程的学生平均成绩*/
    select avg(grade)
    from sc,c
    where sc.cno=c.cno and ccredit='2'
    group by sc.cno
    

     7.

    统计每门课程的学生选修人数,要求超过3人的课程才统计,要求输出课程号和选课人数,查询结果按人数降序排列,若人数相同,按课程号
    升序排列

    分析:每门每门,继续group by哦,同时呀,超过就是大于就是'>'。

    /*统计每门课程的学生选修人数,要求超过3人的课程才统计,要求输出课程号和选课人数,查询结果按人数降序排列,若人数相同,按课程号
    升序排列*/
    select cno,count(sno)
    from sc
    group by cno
    having count(sno)>3#超过就是大于嘤嘤嘤
    order by count(sno) desc,cno;
    

      

    8.检索学号比‘思思’同学大而年龄比她小的学生姓名。

    分析:嵌套循环走起来

    /*检索学号比‘思思’同学大而年龄比她小的学生姓名*/
    select sname
    from student 
    where sno>(select sno from student where sname='思思')
    and sage<(select sage from student where sname='思思');
    

      

    9.检索姓名以'龙'开头的所有学生的姓名和年龄。

    分析:like

    /*检索姓名以'龙'开头的所有学生的姓名和年龄*/
    select sname,sage
    from student
    where sname like '龙%';
    

      10.在sc中检索成绩为空值的学生学号和课程号。

    /*在sc中检索成绩为空值的学生学号和课程号*/
    select sno,cno
    from sc
    where grade is null;
    

      11.求年龄大于女学生平均年龄的男学生姓名和成绩。

    /*求年龄大于女学生平均年龄的男学生姓名和成绩*/
    select sname,grade
    from sc,student
    where sc.sno=student.sno and ssex='男'
    and sage>(select avg(sage) from student where ssex='女');
    

      12.

    求年龄大于所有女学生年龄的男学生姓名和成绩
    /*求年龄大于所有女学生年龄的男学生姓名和成绩*/
    select sname,grade
    from sc,student
    where sc.sno=student.sno and ssex='男'
    and sage>(select max(sage) from student where ssex='女');
    

      13.检索选修2号课程的学生中成绩最高的学生的学号

    分析:拒绝无线套娃嘿嘿

    /*检索选修2号课程的学生中成绩最高的学生的学号*/
    select sno
    from sc
    where cno='2' and grade=(select max(grade) from sc where cno='2');
    

      

    14.

  • 相关阅读:
    VBA宏 合并EXCEL
    Opensource开源精神
    Redisd VS Memcached
    google-analytics.com
    Sybase PowerDesign 导入数据库结构formSqlserver
    Session Sticky About Nginx
    网站应用程式架构
    MapReduce形象总结
    Ubuntu升级内核
    ASP.NET5 MVC6入门教学之一(自己动手)
  • 原文地址:https://www.cnblogs.com/dragondragon/p/12517936.html
Copyright © 2011-2022 走看看