• SQL查询


    --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 Degree from Score  group by  Degree  having Degree>60 and Degree<80
    select Degree from Score where Degree  between 60 and 80
    --5、 查询Score表中成绩为85,86或88的记录。
    select Degree from Score  group by  Degree  having Degree=85 or Degree=86 or Degree=88
    select Degree from Score   where Degree  in(85,86,88)
    --6、 查询Student表中“95031”班或性别为“女”的同学记录。
    select * from  Student where Ssex='女' or class='95031'
    --7、 以Class降序查询Student表的所有记录。

    select * from Student order by Class  desc
    --8、 以Cno升序、Degree降序查询Score表的所有记录。
    select * from Score order by Cno ,Degree desc
    --9、 查询“95031”班的学生人数。

    select COUNT (95031) from Student group by  class having class='95031'
    select COUNT (*) from Student where class='95031'
    --10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)

    --子查询
    select Sno,Cno from Score where Degree=(
    select MAX (Degree) from Score )
    --排序

    select top 1 Sno,Cno from Score order by Degree  desc
    --11、 查询每门课的平均成绩。
    select AVG (Degree ) from Score group by Cno

    select AVG (Degree ) from Score  where Cno='3-245'
    select AVG (Degree ) from Score  where  Cno='3-105'
    select AVG (Degree ) from Score  where  Cno='6-166'

    --12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。

    select AVG(Degree) from Score group by Cno  having  COUNT(*)>4
    and Cno like '3%'


    --13、查询分数大于70,小于90的Sno列。
    select * from Score where Degree>70 and  Degree<90
    --14、查询所有学生的Sname、Cno和Degree列。
    select Sname  from  Student
    select Cno ,Degree from  Score

    select student.sname,Score .Cno,score.degree from student join score on score.sno=student.sno
    select sname,Cno,degree from student,Score where score.sno=student.sno
    --15、查询所有学生的Sno、Cname和Degree列。
    select Score.Sno,course.cname,score.degree from   score join course on course.cno = score.cno

    --16、查询所有学生的Sname、Cname和Degree列。
    select student.sname,course.cname,score.degree from student join score on score.sno=student.sno join course on course.cno = score.cno

    --17、 查询“95033”班学生的平均分。
    select AVG(Degree)from Score group by Sno  having Sno in (
    select Sno from Student where class='95033')

    select AVG(Degree)from   student join score on score.sno=student.sno and class='95033'
     
    18、 假设使用如下命令建立了一个grade表:

    create table grade(low  int(3),upp  int(3),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列。

    use 作业
    go
    create table grade
    (
    low  int,
    upp  int,
    primary key(low ,upp),
    rank  char(1),
    )
    go
    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 Sno,Cno,rank from grade ,score where degree between low and upp


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

    --20、查询score中选学多门课程的同学,中分数为非最高分成绩的记录。
    --找同学
    select sno from Score group by Sno  having COUNT(*)>1
    --找最高分
    select  MAX (Degree) from Score where Sno in(
    select sno from Score group by Sno  having COUNT(*)>1
    )
    --去最高分
    select Sno  from Score where Degree != (select  MAX (Degree) from Score where Sno in(
    select sno from Score group by Sno  having COUNT(*)>1 ) )group by Sno  having COUNT(*)>1
    --
    select *  from Score where Sno in(
    select  Sno  from Score where Degree != (select  MAX (Degree) from Score where Sno in(
    select sno from Score group by Sno  having COUNT(*)>1 ) )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='101')
    --23、查询“张旭“教师任课的学生成绩。
    select*from  Teacher
    select*from  score
    select*from  Course

    select Degree from   score where Cno in(
    select Cno  from Course where  Tno in (
    select Tno from  Teacher where Tname='张旭'))

    --24、查询选修某课程的同学人数多于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 ))
    --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 Degree from   score where cno in(
     select cno from   Course where Tno in(
     select Tno from  Teacher where Depart='计算机系'))
    --28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。
    select*from  Teacher

    select Tname,Prof from Teacher t1 where Depart='计算机系' and not exists (
    select * from Teacher t2 where Depart='电子工程系'and t1.Prof=t2.Prof)
    union
    select Tname,Prof from Teacher t1 where Depart='电子工程系' and not exists (
    select * from Teacher t2 where Depart='计算机系'and t1.Prof=t2.Prof)

    select Tname,Prof from Teacher  order by Prof --职称也能排序
    select Tname from Teacher where Prof  in(
    select distinct Prof from Teacher)

    select distinct Prof from Teacher
     
    --29、查询选修编号为“3-105“课程且成绩至少高于一个选修编号为“3-245”的同学的Cno、Sno和Degree,
    --并按Degree从高到低次序排序。

    select*from  score  where Degree>any(
    select Degree from  score where Cno='3-245') and   cno='3-105' order by Degree


    --30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.
    select*from  score  where Degree>all(
    select Degree from  score where Cno='3-245') and   cno='3-105'
    --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 s1 where Degree<(
    select AVG (Degree) from score s2 group by Cno having s1.Cno=s2.Cno)
    --34、 查询所有任课教师的Tname和Depart.
    select Tname , Depart from  Teacher where Tno  in(
    select Tno from Course  where Cno  in(
     select Cno from Score ))
    --35 、 查询所有未讲课的教师的Tname和Depart.
    select Tname , Depart from  Teacher where Tno  in(
    select Tno from Course  where Cno not in(
     select Cno from Score ))
    --36、查询至少有2名男生的班号。
    select *from Student
    insert into Student  values('102','韩磊','女',1977-09-01,'95033')
    insert into Student  values('104','韩梅梅','男',1977-09-01,'95033')
    delete from Student  where Sname='韩磊'
    delete from Student  where Sname='韩梅梅'

    select class  from Student where Ssex='男'  group by class  having COUNT (*)>1

    --37、查询Student表中不姓“王”的同学记录。
    select *from Student where Sname not like '王%'
    select Sname  from Student
    --38、查询Student表中每个学生的姓名和年龄。
    select Sname,( datepart(yy,GETDATE()) -datepart(yy,Sbirthday)) as'年龄'from Student

    select datepart(yy,Sbirthday)from Student
    --39、查询Student表中最大和最小的Sbirthday日期值。
    select MAX(Sbirthday),min (Sbirthday)from Student
    --40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
    select *from Student order by class desc,( datepart(yy,GETDATE()) -datepart(yy,Sbirthday))desc
    --41、查询“男”教师及其所上的课程。
    select Tname,Cname from Teacher join Course on Course.Tno=Teacher.Tno where Tsex='男'

    select Tno from Teacher where Tsex='男'


    --42、查询最高分同学的Sno、Cno和Degree列。
    select Sno,Cno,Degree from Score  where Degree  in (

    select MAX(Degree)from Score  group by Cno)

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

    --44、查询和“李军”同性别并同班的同学Sname.
    select Sname from Student where Sname!='李军' and Ssex =(
    select Ssex from Student where Sname='李军') and class=(
    select class from Student where Sname='李军')
    --45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
    select*from  Teacher
    select*from  score
    select*from  Course
    select*from  Student
     
     select  Degree from score where cno in(

     select cno from Course where Cname='计算机导论'   )and Sno in  (
     select  sno from Student where Ssex='男' )
     
    /*横表变纵表或纵表变横表
    成绩表如下
         Name Course Grade
         张三  语文   95
         张三  数学   90
         张三  英语   89
         李四  语文   92
         李四  数学   88
         李四  英语   97
    变成
       
        姓名  语文  数学  英语
        张三   95    90    89
        李四   92    88    97
        */
    create table biao
    (
    PName varchar(10),
    PCourse  varchar (10),
    PGrade int
    )
    go
     insert into biao values('张三','语文','95') 
     insert into biao values('张三','数学','90') 
     insert into biao values('张三','英语','89') 
     insert into biao values('李四','语文','92') 
     insert into biao values('李四','数学','88') 
     insert into biao values('李四','英语','97') 
     
     --Name Course Grade
     select PName ,
     SUM(case PCourse when '语文'then PGrade else 0 end)as 语文,
     SUM(case PCourse when '数学'then PGrade else 0 end)as 数学,
     SUM(case PCourse when '英语'then PGrade else 0 end)as 英语
     from  biao
     group by PName
     
     
    /*
     select 姓名,'语文'as 科目,语文 as 成绩
     from  biao union all
     select 姓名,'数学'as 科目,数学 as 成绩
     from  biao union all
     select 姓名,'英语'as 科目,数学 as 成绩
     from  biao
     order by 姓名,科目 desc
     */
     
     create table biao1
    (
    姓名 varchar(20),
    语文 int,
    数学 int,
    英语 int
    )
    go
     insert into biao1 values('张三',95,90,89) 
     insert into biao1 values('李四',92,88,97)
     
     
     
     select 姓名 ,'语文' as 课程,语文 as 成绩 from biao1 union all
     select 姓名,'数学'as 课程,数学 as 成绩 from biao1 union all
     select 姓名,'英语'as 课程,英语 as 成绩 from biao1
     order by 姓名 desc
     
     
     select 姓名 ,'语文' as 课程,语文 as 成绩 from biao1
     
     
     
     
     
     
     
     
     
     select *from 表格转换 where Name like case

  • 相关阅读:
    第三次实验总结
    实验总结
    自我介绍
    BGP学习笔记
    source insight用于C语言编程的工具脚本
    LevelDB源码剖析
    LevelDB源码之五Current文件Manifest文件版本信息
    LevelDB源码之六缓存机制
    LevelDB源码之四LOG文件
    jQuery向动态生成的内容添加事件响应
  • 原文地址:https://www.cnblogs.com/L6KE/p/4665479.html
Copyright © 2020-2023  润新知