zoukankan      html  css  js  c++  java
  • SQL 查询语句×45

    把题记录一下吧,虽然以后碰到的不一样,但用法还是一样的嘛

    还有用到的一些方法

    count(*)   计数 

    group by  分组

    join 连接   后面要加 on 连接条件

    between  and    什么什么之间 

    in   not in  子查询

    like 模糊查询   %表示其他内容

    ----------------------------------------------------------------------------------------------------------------------

    --1、 查询Student表中的所有记录的Sname、Ssex和Class列。
    select Sname,Ssex,Class from Student

    --2、 查询教师所有的单位即不重复的Depart列。
    select distinct depart from teacher

    --3、 查询Student表的所有记录。
    select * from Student

    --4、 查询Score表中成绩在60到80之间的所有记录。
    select * from Score where Degree>=60 and Degree<=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

    --8、 以Cno升序、Degree降序查询Score表的所有记录。
    select * from score order by Cno asc , Degree desc

    --9、 查询“95031”班的学生人数。
    select COUNT(1) from student where class = 95031

    --10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
    select Sno,Cno from Score where Degree in (select MAX(degree) from score)

    --11、 查询每门课的平均成绩。
    select Cno,avg(degree) from score group by cno

    --12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
    select cno,avg(degree) from score group by cno having count(cno)>=5 and cno like '3%'

    --13、查询分数大于70,小于90的Sno列。
    select sno from Score where Degree>70 and Degree<90

    --14、查询所有学生的Sname、Cno和Degree列。
    select a.Sname,b.Cno,b.Degree from Student a join Score b on a.Sno=b.sno

    --15、查询所有学生的Sno、Cname和Degree列。
    select a.Sno,b.Cname,a.Degree from Score a join Course b on a.Cno=b.cno

    --16、查询所有学生的Sname、Cname和Degree列。
    select a.Sname,c.cname,b.Degree from Student a join Score b on a.Sno=b.sno
    join Course c on b.Cno=c.cno

    --17、 查询“95033”班学生的平均分。
    select AVG(degree) from Student a join Score b on a.sno=b.sno 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')
    select * from grade
    --现查询所有同学的Sno、Cno和rank列。
    select a.Sno,b.cno,c.[rank] from Student a join Score b on a.Sno=b.sno
    join grade c on b.Degree between c.low and c.upp

    --19、  查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
    select * from score where cno='3-105' and Degree>
    (select Degree from Score where cno='3-105' and Sno=109)


    --20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
    select * from Score where degree not in(select MAX(degree) from Score group by sno)

    --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 a join Course b on a.Cno=b.cno
    join Teacher c on b.Tno=c.tno where c.Tno=856

    --24、查询选修某课程的同学人数多于5人的教师姓名。
    select distinct c.tname from Score a join Course b on a.Cno=b.Cno join Teacher c on
    b.Tno=c.tno where a.cno in (select cno from Score group by Cno having COUNT(cno)>5)

    --25、查询95033班和95031班全体学生的记录。
    select * from Student a join Score b on a.Sno=b.Sno join Course c on b.Cno=c.Cno
    join Teacher d on c.Tno=d.tno

    --26、  查询存在有85分以上成绩的课程Cno.
    select distinct cno from Score where Degree>85

    --27、查询出“计算机系“教师所教课程的成绩表。
    select a.degree from Score a join Course b on a.Cno=b.Cno join
    Teacher c on b.tno=c.tno where Depart='计算机系'

    --28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
    select Tname,prof from Teacher where Prof not in
    (select prof from Teacher where Depart='计算机系') or Prof not in
    (select prof from Teacher where Depart='电子工程系')

    --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

    --30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
    select Cno,Sno,degree from Score a where Cno='3-105' and Degree >
    (select degree from Score b where Cno='3-245' and a.sno=b.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 * from Score a where degree <
    (select AVG(degree) from Score b group by Cno having a.cno=b.cno)

    --34、 查询所有任课教师的Tname和Depart.
    select distinct tname,depart from Score a join Course b on a.Cno=b.Cno join
    Teacher c on b.Tno=c.Tno where a.Cno in (b.cno)

    --35 、 查询所有未讲课的教师的Tname和Depart.
    select Tname,depart from Teacher where Tno not in
    (select c.tno from Score a join Course b on a.Cno=b.Cno join
    Teacher c on b.Tno=c.tno where a.Cno in (b.cno) )

    --36、查询至少有2名男生的班号。
    select class from student where Ssex='男' group by class having count(class)>=2

    --37、查询Student表中不姓“王”的同学记录。
    select * from Student where Sname not like '王%'

    --38、查询Student表中每个学生的姓名和年龄。
    select sname,2017-YEAR(sbirthday) from student

    --39、查询Student表中最大和最小的Sbirthday日期值。
    select MAX(sbirthday),MIN(sbirthday) from student

    --40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
    select * from Student order by Class desc,sbirthday asc

    --41、查询“男”教师及其所上的课程。
    select a.cname,b.tname from Course a join teacher b on a.tno=b.Tno where Tsex='男'

    --42、查询最高分同学的Sno、Cno和Degree列。
    select a.sno,b.Cno,b.degree from Student a join Score b on a.Sno=b.Sno where Degree in
    (select MAX(degree) from score)

    --43、查询和“李军”同性别的所有同学的Sname.
    select sname from Student where Ssex in (select ssex from Student where Sname='李军')

    --44、查询和“李军”同性别并同班的同学Sname.
    select sname from Student where Ssex in (select ssex from Student where Sname='李军')
    and Class in (select Class from Student where Sname='李军')

    --45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
    select * from Student a join Score b on a.Sno=b.Sno join Course c on b.Cno=c.Cno
    where c.cname ='计算机导论' and a.Ssex='男'

  • 相关阅读:
    Glide优化
    Java多线程知识点
    Android知识点
    Gradle的一些技巧和遇到的问题
    Python用Django写restful api接口
    Python写爬虫爬妹子
    用最简单的例子说明设计模式(三)之责任链、建造者、适配器、代理模式、享元模式
    【Python】扫描指定文件夹下特定后缀的文件
    【Python】生成多级树结构的xml文件
    【转】【Linux】安装pyinstaller
  • 原文地址:https://www.cnblogs.com/m110/p/7744105.html
Copyright © 2011-2022 走看看