zoukankan      html  css  js  c++  java
  • SQL数据库—<10>--查询练习题

    待整理····微笑大笑眨眼惊讶吐舌笑脸热烈的笑脸生气尴尬困惑悲伤哭泣的脸失望恶魔天使吸血蝙蝠保守秘密咬牙切齿书呆子讽刺悄悄话生病马上回来我不知道正在思考聚会笑脸转动眼睛困了狡猾恼怒开玩笑忍者妒忌大声笑捧腹大笑吐舌鬼脸外星人夏娃亚当冰冻相爱公主幽灵猛击朋克愤怒疑惑飞吻


    45题分页查询


    学生选课数据库SQL语句练习题(45个题)

    练习题网盘地址:点我

     1 create database xxb
     2 go
     3 use xxb
     4 go
     5 --表(一)Student (学生表)
     6 create table Student 
     7 (
     8 sno char(3) primary key,
     9 sname char(8) not null,
    10 ssex char(2) not null,
    11 sbirthday datetime,
    12 class char(5)
    13 )
    14 go
    15 --表(四)Teacher(教师表)
    16 create table teacher
    17 (
    18 tno char(3) primary key,
    19 tname char(4) not null,
    20 tsex char(2) not null,
    21 tbirthday datetime not null,
    22 prof char(6) not null,
    23 depart varchar(10) not null
    24 )
    25 select * from teacher
    26 go
    27 --表(二)Course(课程表)
    28 create table course
    29 (
    30 cno char(5)primary key,
    31 cname varchar(10) not null,
    32 tno char(3) references teacher(tno) --foreign key(tno) references teacher(tno)
    33 )--references
    34 go
    35 --表(三)Score(成绩表)
    36 create table score
    37 (
    38 sno char(3) references Student(sno) not null,
    39 cno char(5) references course(cno) not null,
    40 degree decimal(4,1)
    41 )
    42 use xxb
    43 go
    44 --向表中插入数据,顺序不能变
    45 insert into Student values('108','曾华','','1977-09-01','95033')
    46 insert into Student values('105','匡明','','1975-10-02','95031')
    47 insert into Student values('107','王丽','','1976-01-23','95033')
    48 insert into Student values('101','李军','','1976-02-20','95033')
    49 insert into Student values('109','王芳','','1975-02-10','95031')
    50 insert into Student values('103','陆军','','1974-06-03','95031')
    51 
    52 
    53 
    54 insert into teacher  values('804','李诚','','1958-12-02','副教授','计算机系')
    55 insert into teacher  values('856','张旭','','1969-03-12','讲师','电子工程系')
    56 insert into teacher  values('825','王萍','','1972-05-05','助教','计算机系')
    57 insert into teacher values('831','刘冰','','1977-08-14','助教','电子工程系')
    58 
    59 
    60 insert into  course values('3-105','计算机导论','825')
    61 insert into course values('3-245','操作系统','804')
    62 insert into  course values('6-166','数字电路','856')
    63 insert into  course values('9-888','高等数学','831')
    64 
    65 insert into score values('103','3-245','86')
    66 insert into  score values('105','3-245','75')
    67 insert into  score values('109','3-245','68')
    68 insert into  score values('103','3-105','92')
    69 insert into score values('105','3-105','88')
    70 insert into  score values('109','3-105','76')
    71 insert into  score values('101','3-105','64')
    72 insert into  score values('107','3-105','91')
    73 insert into  score values('108','3-105','78')
    74 insert into  score values('101','6-166','85')
    75 insert into  score values('107','6-166','79')
    76 insert into  score values('108','6-166','81')
    建库、建表

    1、 查询Student表中的所有记录的Sname、Ssex和Class列。
    select sname,ssex,class from Student 
    2、 查询教师所有的单位即不重复的Depart列。   消除重复的行 用distinct
    select distinct depart from teacher 
    3、 查询Student表的所有记录。
    select * from Student
    4、 查询Score表中成绩在60到80之间的所有记录。
    select * from score where degree>60 and degree<80
    select * from score where degree between 60 and 80
    5、 查询Score表中成绩为85,86或88的记录。
    select * from score where degree='85'or degree='86'or degree='88'
    6、 查询Student表中“95031”班或性别为“女”的同学记录。
    select * from Student where class ='95031' or ssex='女'
    7、 以Class降序查询Student表的所有记录。
    select * from Student order by class desc --asc是升序排列 默认可不写
    8、 以Cno升序、Degree降序查询Score表的所有记录。
    select * from score order by cno ,degree desc  --先按照cno升序再degree降序显示
    9、 查询“95031”班的学生人数。
    select COUNT(*) from Student where class='95031'
    10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
    select top 1  sno,cno,degree from score  order by  degree desc
    11、 查询每门课的平均成绩。(select Brand from Car group by brand )
    select AVG(degree) as 每个平均成绩 from score group by cno 
    12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
    select cno ,AVG(degree) ,count(*)from score  where cno like '3%' group by cno having COUNT (*) >=5 
    13、查询分数大于70,小于90的Sno列。
    select sno,degree from score where degree between 70 and 90
    14、查询所有学生的Sname、Cno和Degree列。
    select sname,cno,degree from student join score on score.sno=student.sno
    --先将student表和score表join起来,然后查询需要的列
    select sname,cno,degree from student join score on student.sno = score.sno
    15、查询所有学生的Sno、Cname和Degree列。
    select sno,cname,degree from score join course on course.cno=score.cno
    
    select sno,cname,degree from score
    join course on  score.cno=course.cno
    

      

    16、查询所有学生的Sname、Cname和Degree列。
    第一步写三个表,第二步join起来,第三步筛选需要的列
    select sname,cname,degree from Student
    join score on score.sno=Student.sno 
    join course on course.cno=score.cno
    
    17、 查询“95033”班学生的平均分。
    select avg(degree) from score 
    join Student on Student.sno=score.sno
    where class='95033'
    --或者:
    select * from score
    select * from Student
    select sno from  Student  where class='95033'
    select AVG(degree) from score where sno in ('101','107','108')
    select AVG(degree) from score where sno in (select sno from  Student  where class='95033'
    )
    
    18、 假设使用如下命令建立了一个grade表:
    create table grade(low  int,upp  int,rank  char(1))
    insert into grade values(90,100,'A')
    insert into grade values(80,89,'B')
    insert into grade values(70,79,'C')
    insert into grade values(60,69,'D')
    insert into grade values(0,59,'E')
    现查询所有同学的Sno、Cno和rank列。
    select * from grade
    
    select sno,cno,[rank] from score
    join grade on degree between low and upp
    
    select sno,cno,[rank] from score
    --select * from grade
    join grade on score.degree >=low and score.degree<=upp
    
    
    
    19、  查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。--无关子查询
    select distinct * from Student  join score on score.sno=Student.sno 
    select * from Student where sno in(
    select  sno from score where cno ='3-105'and degree>(select degree from score where sno='109'and cno='3-105'))
    
    先用子查询查询出3-105课程和是109同学的成绩 在拿这个成绩加上3-105条件判断那个同学符合条件 并且筛选出他的学号 在学生表中查找符号条件的学号 输出学生所有信息
    
    20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
    
    外层条件拿到里层进行比较,符合条件的查询返回
    找出选修多门课程的同学的sno;里层查询找出每门课程的最高分,前天条件是里层的cno和外层的cno一样
    
    select sno from score group by sno having COUNT(*)>1
    select MAX(degree) from score  where 
    
    
    select *from score a where sno in
    (
      select sno from score group by sno having COUNT(*)>1 
    )
    and degree not in
    (
    select MAX(degree) from score b where a.cno=b.cno group by cno
    )
    
    select sno from score where sno!=103  group by sno having COUNT(*)>1
    select degree from score where sno in ('101','103','105','107','108','109')
    select max(degree) from score 
    select sno from score where degree=92
    
    select degree from score where sno in (select sno from score where sno!=(select sno from score where degree=(select max(degree) from score) )
      group by sno having COUNT(*)>1
    )
    
    21、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
    select * from  score where degree>(select degree from score  where sno='109'and cno='3-105')
    22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
     select sno,sname,sbirthday from Student where year(sbirthday)=(select year(sbirthday)from Student where sno='108')
    23、查询“张旭“教师任课的学生成绩。
    select degree from score where cno=(select cno from course where tno=(select tno from teacher where tname = '张旭'))
    
    select * from score 
    join course on score.cno =course.cno
    join teacher on course.tno=teacher.tno
    where teacher.tname='张旭'
    
    24、查询选修某课程的同学人数多于5人的教师姓名。
    select tname from teacher 
    join course on teacher.tno=course.tno
    where course.cno in(select cno from score group by cno having COUNT(*)>5 )
    --或者
    select tname from teacher where tno in(select tno from course where cno in(select cno from score group by cno having COUNT (*)>=5)
    )
    --步骤
    select * from teacher
    select * from score
    select tno from course where cno in(select cno from score group by cno having COUNT (*)>=5)
    select * from teacher where tno in(select tno from course where cno in(select cno from score group by cno having COUNT (*)>=5)
    )
     
    25、查询95033班和95031班全体学生的记录。
    select * from Student where class='95033'or class='95031'
    26、  查询存在有85分以上成绩的课程Cno.
    select distinct cno from score where degree>85
    27、查询出“计算机系“教师所教课程的成绩表。
    select * from score 
    join course on score.cno=course.cno
    join teacher on course.tno =teacher.tno
    where depart='计算机系'
    多余的 where course.tno in(select tno from teacher where depart='计算机系')
    28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
    
    不用相关子查询
    SELECT * FROM teacher where depart='计算机系' and prof NOT IN(SELECT prof from teacher where depart='电子工程系')
    UNION
    SELECT * FROM teacher where depart='电子工程系' and prof NOT IN(SELECT prof from teacher where depart='计算机系')
    
    --相关子查询
    SELECT * FROM teacher t1 where depart='计算机系' AND NOT EXISTS(
    SELECT * FROM teacher t2 WHERE t2.depart='电子工程系' AND t1.prof = t2.prof
    )
    UNION
    SELECT * FROM teacher t1 where depart='电子工程系' AND NOT EXISTS(
    SELECT * FROM teacher t2 WHERE t2.depart='计算机系' AND t1.prof = t2.prof)
    
    
    29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
    select cno,sno,degree from score
    where cno='3-105' and degree >
    (select max(degree) from score where cno='3-245')  order by degree desc
    --步骤:
    select * from  score 
    select MAX(degree) from score where cno='3-245'
    select * from score where cno='3-105' and degree>(select MAX(degree) from score where cno='3-245') order by degree desc
    30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
    
    select * from score
    select sno from score where cno in('3-105','3-245')  group by sno having COUNT(*)>1 
    select * from score where sno in(select sno from score where cno in('3-105','3-245')  group by sno having COUNT(*)>1
    )
    
    select degree as a from score where  sno in('103','105','109')and cno in ('3-105')
    select degree as b from score where sno in('103','105','109')and cno in ('3-245')
    
    select * from score a  where degree in(select degree as a from score where  sno in('103','105','109')and cno in ('3-105'))  AND  EXISTS(select degree as b from score where sno in('103','105','109')and cno in ('3-245')
    )
    
    
     答案
    select *from score s1 where sno in
    (
    select sno from score where cno in('3-105','3-245')group by sno having COUNT(*)>1
    )
    and cno='3-105' and degree>
    (
    select degree from score s2 where sno in
    (
    select sno from score where cno in('3-105','3-245')group by sno having COUNT(*)>1
    )and cno='3-245' and s2.sno=s1.sno
    )
    
    
    
    31、 查询所有教师和同学的name、sex和birthday.
    select sname,ssex,sbirthday from student
    union
    select tname,tsex,tbirthday from teacher
    32、查询所有“女”教师和“女”同学的name、sex和birthday.
    select sname,ssex,sbirthday from student where ssex='女'
    union
    select tname,tsex,tbirthday from teacher where tsex='女'
    33、 查询成绩比该课程平均成绩低的同学的成绩表。
    
    select AVG(degree) from score group by cno  --对课程分类 取每类课程的平均成绩
    select * from score as a  where  exists (select cno,AVG(degree) from score b  group by cno having (a.cno=b.cno and a.degree<AVG(degree)))
    
    
    select *from score s1 where degree<
    (
    select AVG(degree) from score s2 where s1.cno=s2.cno group by cno
    )
    
    select * from score a1 where degree<(
    select AVG(degree) from score a2 where a1.cno=a2.cno group by cno )
    
    
    select * from  score  a where  exists ( select cno,AVG(degree) from score b  group by cno  having (a.sno=b.sno) and (a.degree<avg(degree)))
    
    34、 查询所有任课教师的Tname和Depart.
    select tname,depart from teacher
    
    select * from teacher a where exists (select * from course b  where a.tno=b.tno )
    或者
    select * from teacher where tno in(select distinct tno  from course 
    )
    或者
    --------select * from teacher a left join course b on  a.tno=b.tno  where course.tno is null
    
    
    35 、 查询所有未讲课的教师的Tname和Depart. 
    
    答案之一:1、先把score表中cno分组出来(实际学生上了那些课);2、拿course表中的cno给步骤1的结果做比较 not in  (course表中cno是老师要讲的课,跟学生实际上了那些课做比较,得出哪门课程是学生没有上的)3、已经获得未讲的课的编号,把teacher表和course表join起来 从中找到这门课是哪个老师的课程
    select tname,depart from teacher
    join course on teacher.tno=course.tno
    where course.cno=(select cno from course where cno not in(select cno from score group by cno))
    
    答案2:
    select tname,depart from teacher
    left join course on teacher.tno=course.tno
    left join score on course.cno=score.cno
    where score.sno is null
    
    36、查询至少有2名男生的班号。
    select class from Student where ssex='男' group by class having COUNT(*)>1--行数>1 说明 
    select class from student where ssex='男' group by class having COUNT(*)>1
    37、查询Student表中不姓“王”的同学记录。
    select * from Student where sname     like '王%'
    select * from student where sname not like '王%'
    38、查询Student表中每个学生的姓名和年龄。
    select sname, year(getdate())-year(sbirthday) as 年龄 from Student --getdate()是获得当前时间的函数
    
    39、查询Student表中最大和最小的Sbirthday日期值。
    
    select MAX(Sbirthday),MIN(Sbirthday) from Student
    
    40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
    
    select *,year(getdate())-year(sbirthday) as 年龄 from Student order by class desc, sbirthday 
    41、查询“男”教师及其所上的课程。
    select tname,tsex,course.cname from teacher 
    join course on teacher.tno=course.tno where tsex='男'
    42、查询最高分同学的Sno、Cno和Degree列。
    select sno,cno,degree from score where degree=(select MAX(degree)from score)
    43、查询和“李军”同性别的所有同学的Sname.
    select sname,ssex from Student where ssex=(select ssex from Student where sname='李军')
    44、查询和“李军”同性别并同班的同学Sname.
    select sname,ssex,class from Student where ssex=(select ssex from Student where sname='李军') and class=(select class from Student where sname='李军')
    45、查询所有选修“计算机导论”课程的“男”同学的成绩表
    select student.sno,course.cno,degree,sname,ssex,class,cname,tno from score 
    join Student on score.sno=Student.sno
    join course on score.cno=course.cno
    where cname='计算机导论' and ssex ='男'
    

      


    分页查询

     

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    博文来源广泛,如原作者认为我侵犯知识产权,请尽快给我发邮件 664507902@qq.com联系,我将以第一时间删除相关内容。

查看全文
  • 相关阅读:
    halcon算子翻译——append_channel
    halcon算子翻译——access_channel
    halcon算子翻译——set_framegrabber_param
    halcon算子翻译——set_framegrabber_lut
    halcon算子翻译——set_framegrabber_callback
    halcon算子翻译——open_framegrabber大恒相机
    在循环中使用break案例
    for、while、do while 3种循环异同点
    do while循环
    while循环案例
  • 原文地址:https://www.cnblogs.com/ooip/p/4547881.html
  • Copyright © 2011-2022 走看看