zoukankan      html  css  js  c++  java
  • SQL 练习题

    --1.   查询学生的基本信息;
    select * from student
    go
    --2.   查询“CS”系学生的基本信息;
    select * from student
    where sdept='cs'
    go
     
    --3.   查询“CS”系学生年龄不在19到21之间的学生的学号、姓名;
    select sno,sname from student
    where sdept='cs' and sage not between 19 and 21
    go
     
    --4.   找出最大年龄;
    select max(sage) from student
    go
     
    --5.   找出“CS”系年龄最大的学生,显示其学号、姓名;
    select sno,sname from student
    where sdept='cs' and sage=(select max(sage)                           
    from student                           
    where sdept='cs'                       
        )
    go
    --6.   找出各系年龄最大的学生,显示其学号、姓名;
    select sno,sname,sdept from student s1
    where sage=(select max(sage)            
    from student         
        here sdept=s1.sdept       
         )
    go
    --7.   统计“CS”系学生的人数;
    select count(*) from student
    where sdept='cs'
    go
     
    --8.   统计各系学生的人数,结果按升序排列;
    select sdept,count(sno) as sm
    from student
    group by sdept
    order by sm
    go
    --9.   按系统计各系学生的平均年龄,结果按降序排列;
    select sdept,count(sno) as sm
    from student
    group by sdept
    order by sm desc
    go
     
    --10.  查询每门课程的课程名;
    select cno,cname from course
    go
    --11.  查询无先修课的课程的课程名和学分;
    select cname,ccredit
    from course
    where cpno is null
    go
     
    --12.  统计无先修课的课程的学分总数;
    select * from course select sum(ccredit)
    from course
    where cpno is null
    go
     
    --13.  统计每位学生选修课程的门数、学分及其平均成绩;
    select sno,count(sc.cno),count(ccredit),avg(scores)
    from sc,course
    where sc.cno=course.cno
    group by sno
    go
     
    --14.  统计选修每门课程的学生人数及各门课程的平均成绩;
    select cno,count(sno),avg(grade)
    from sc
    group by cno
    go
     
    --15.  找出平均成绩在85分以上的学生,结果按系分组,并按平均成绩的升序排列;
    select sdept,sc.sno,avg(scores)
    from sc,student
    where sc.sno=s.sno
    group by sc.sno,sdept having avg(scores)>85
     
    go
     
    --16.  查询选修了“1”或“2”号课程的学生学号和姓名;
     
    select sc.sno,sname
    from sc,student
    where sc.sno=student.sno and (sc.cno='1' or sc.cno='2')
    go
     
    --17.  查询选修了“1”和“2”号课程的学生学号和姓名;
    select sc.sno,sname from sc,student
    where sc.sno=student.sno and sc.cno='1' and sc.sno in(select sno                                                      from sc                                                   
    where cno='2')
    go
     
    --18.  查询选修了课程名为“数据库系统”且成绩在60分以下的学生的学号、姓名和成绩;
    select sc.sno,sname,grade
    from sc,student,course
    where sc.sno=student.sno and sc.cno=course.cno and
    course.cname='数据库系统' and grade<60
    go
     
    --19.  查询每位学生选修了课程的学生信息(显示:学号,姓名,课程号,课程名,成绩);
    --20 查询没有选修课程的学生的基本信息;
    select sno,sname,sage
    from s
    where sno not in(select distinct sno from sc)
    go
     
    --21.  查询选修了3门及以上课程的学生学号;
    select sno
    from sc
    group by sno having count(cno)>=3
    go
     
    --22.  查询选修课程成绩至少有一门在80分以上的学生学号;
    select distinct sno
    from sc where grade>80
    go
     
    --23.  查询选修课程成绩均在80分以上的学生学号;
    select distinct sno
    from sc
    where sno not in(select distinct sno        
             from sc             
        where grade<80
    )
    go
     
    --24.  查询选修课程平均成绩在80分以上的学生学号;
    select sno
    from sc
    group by sno having avg(grade)>80
    go
       
    --1.   查询学生的基本信息;
    select * from student
    go
    --2.   查询“CS”系学生的基本信息;
    select * from student
    where sdept='cs'
    go
     
    --3.   查询“CS”系学生年龄不在19到21之间的学生的学号、姓名;
    select sno,sname from student
    where sdept='cs' and sage not between 19 and 21
    go
     
    --4.   找出最大年龄;
    select max(sage) from student
    go
     
    --5.   找出“CS”系年龄最大的学生,显示其学号、姓名;
    select sno,sname from student
    where sdept='cs' and sage=(select max(sage)                           
    from student                           
    where sdept='cs'                       
        )
    go
    --6.   找出各系年龄最大的学生,显示其学号、姓名;
    select sno,sname,sdept from student s1
    where sage=(select max(sage)            
    from student         
        here sdept=s1.sdept       
         )
    go
    --7.   统计“CS”系学生的人数;
    select count(*) from student
    where sdept='cs'
    go
     
    --8.   统计各系学生的人数,结果按升序排列;
    select sdept,count(sno) as sm
    from student
    group by sdept
    order by sm
    go
    --9.   按系统计各系学生的平均年龄,结果按降序排列;
    select sdept,count(sno) as sm
    from student
    group by sdept
    order by sm desc
    go
     
    --10.  查询每门课程的课程名;
    select cno,cname from course
    go
    --11.  查询无先修课的课程的课程名和学分;
    select cname,ccredit
    from course
    where cpno is null
    go
     
    --12.  统计无先修课的课程的学分总数;
    select * from course select sum(ccredit)
    from course
    where cpno is null
    go
     
    --13.  统计每位学生选修课程的门数、学分及其平均成绩;
    select sno,count(sc.cno),count(ccredit),avg(scores)
    from sc,course
    where sc.cno=course.cno
    group by sno
    go
     
    --14.  统计选修每门课程的学生人数及各门课程的平均成绩;
    select cno,count(sno),avg(grade)
    from sc
    group by cno
    go
     
    --15.  找出平均成绩在85分以上的学生,结果按系分组,并按平均成绩的升序排列;
    select sdept,sc.sno,avg(scores)
    from sc,student
    where sc.sno=s.sno
    group by sc.sno,sdept having avg(scores)>85
     
    go
     
    --16.  查询选修了“1”或“2”号课程的学生学号和姓名;
     
    select sc.sno,sname
    from sc,student
    where sc.sno=student.sno and (sc.cno='1' or sc.cno='2')
    go
     
    --17.  查询选修了“1”和“2”号课程的学生学号和姓名;
    select sc.sno,sname from sc,student
    where sc.sno=student.sno and sc.cno='1' and sc.sno in(select sno                                                      from sc                                                   
    where cno='2')
    go
     
    --18.  查询选修了课程名为“数据库系统”且成绩在60分以下的学生的学号、姓名和成绩;
    select sc.sno,sname,grade
    from sc,student,course
    where sc.sno=student.sno and sc.cno=course.cno and
    course.cname='数据库系统' and grade<60
    go
     
    --19.  查询每位学生选修了课程的学生信息(显示:学号,姓名,课程号,课程名,成绩);
    --20 查询没有选修课程的学生的基本信息;
    select sno,sname,sage
    from s
    where sno not in(select distinct sno from sc)
    go
     
    --21.  查询选修了3门及以上课程的学生学号;
    select sno
    from sc
    group by sno having count(cno)>=3
    go
     
    --22.  查询选修课程成绩至少有一门在80分以上的学生学号;
    select distinct sno
    from sc where grade>80
    go
     
    --23.  查询选修课程成绩均在80分以上的学生学号;
    select distinct sno
    from sc
    where sno not in(select distinct sno        
             from sc             
        where grade<80
    )
    go
     
    --24.  查询选修课程平均成绩在80分以上的学生学号;
    select sno
    from sc
    group by sno having avg(grade)>80
    go
       
  • 相关阅读:
    如何成为一个合格的数据架构师?
    证道:零售企业如何借助数据智能提升人效?| 数智加速度10课回顾
    终于,数据中台成为3000万企业的增长引擎
    凯德中国 × 阿里云 × 奇点云 | 沉淀数据资产,遇见数智未来
    追风:数据中台如何驱动全域消费者运营?| 数智加速度09课回顾
    何夕:跟上趋势,拥抱全域数据中台 | 数智加速度08课回顾
    南弈:释放数据价值的「三个关键点」 | 数智加速度07课回顾
    百然:智能算法如何落地商业化?| 数智加速度06课回顾
    星魁:管理数据资产的「五步骤」与「六要素」 | 数智加速度05课回顾
    pytest系列(四)- pytest+allure+jenkins
  • 原文地址:https://www.cnblogs.com/wangprince2017/p/7677890.html
Copyright © 2011-2022 走看看