zoukankan      html  css  js  c++  java
  • SQL server 练习查询45题(1-17)及笔记 3

    create table student
    (
    Sno int primary key  not null,--设置主键
    Sname varchar(50)not null,
    Ssex varchar(50)not null,
    Sbirthday date ,
    Class varchar(50),
    )
    insert into student values(108,'曾华','','1977-09-01',95033)--插入信息
    insert into student values(109,'王芳','','1975-02-10',95031)
    insert into student values(105,'匡明','','1975-10-02',95031)
    insert into student values(107,'王丽','','1976-01-23',95033)
    insert into student values(101,'李军','','1976-02-20',95033)
    insert into student values(103,'陆君','','1974-06-03',95031)
    select*from student
    
    create table Teacher
    (
    Tno int primary key not null,--设置主键
    Tname varchar(50) not null,
    Tsex varchar(2)not null,
    Tbirthday date,   
    Prof Char(6),
    Depart Varchar(10) not null
    )
    insert into Teacher values(831,'刘冰','','1977-08-14','助教','电子工程系')--插入信息
    insert into Teacher values(804,'李诚','','1958-12-02','副教授','计算机系')
    insert into Teacher values(856,'张旭','','1969-03-12','讲师','电子工程系')
    insert into Teacher values(825,'王萍','','1972-05-05','助教','计算机系')
    select*from Teacher
    go
    create table course
    (
    Cno char(5) primary key not null,--设置主键
    Cname Varchar(10) not null,
    Tno int references Teacher(Tno)--设置外键,主键表为Teacher,受Tno列约束
    )
    insert into course values('6-166','数字电路',856)--插入信息
    insert into course values('3-105' ,'计算机导论',825)
    insert into course values('3-245' ,'操作系统' ,804)
    insert into course values('9-888' ,'高等数学' ,831)
    select*from course
    go
    create table score
    (
    Sno int references student(Sno)not null,--设置外键,主键表为student,受Sno列约束
    Cno char(5)references course(Cno) not null,--设置外键,主键表为course,受Cno列约束
    Degree Decimal(4,1) 
    )
    insert into score values(108,'6-166',81)
    insert into score values(103,'3-245',86)
    insert into score values(105,'3-245',75)
    insert into score values(109,'3-245',68)
    insert into score values(103,'3-105',92)
    insert into score values(105,'3-105',88)
    insert into score values(109,'3-105',76)
    insert into score values(101,'3-105',64)
    insert into score values(107,'3-105',91)
    insert into score values(108,'3-105',78)
    insert into score values(101,'6-166',85)
    insert into score values(107,'6-106',79)
    select*from score
    go
    --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 between 60 and 80
    --5、 查询Score表中成绩为85,86或88的记录。
    select *from score where Degree in(85,86,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”班的学生人数。
    --聚合函数:针对数据列 计算求和或计数等一系列算术操作
    --sum(),avg(),max(),min() 括号里为列名 对某一列执行操作 列必须是数值类型的列
    --聚合函数只返回一个结果,select+聚合函数+from
    --as 起别名 
    select COUNT(*) AS renshu from student where Class=95031 
    
    --10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
    select Sno,Cno from score where Degree=(select MAX(Degree)from score)
    select top 1 *from score order by Degree desc
    --11、 查询每门课的平均成绩。
    --当分组和聚合函数结合时,先分组,然后对每一组分别进行聚合
    select  cno,AVG(Degree)as pingjunfen from score group by Cno
    
    --12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
    --having  紧跟group by 使用,在分组完成基础上在进行筛选
    select Cno,AVG(Degree)as 平均分 from score where cno like '3%' group by Cno having COUNT(*)>=5 
    --13、查询分数大于70,小于90的Sno列。
    select*from score where Degree between 70 and 90 
    --14、查询所有学生的Sname、Cno和Degree列。
    --表联结 join on
    --inner 连接符
    --left 把左表全部显示全
    --right 
    select Sname,Cno,Degree from score,student where student.Sno=score.Sno
    select Sname,Cno,Degree from score,student --笛卡尔积 显示组合的所有可能性, 需要where筛选条件来筛选结果
    select Sname,Cno,Degree from score inner join student on score.Sno=student.Sno
    --15、查询所有学生的Sno、Cname和Degree列。
    
    select Sno,Cname,Degree from score join course on course.Cno=score.Cno
    --使用join 把course和表score连接起来 执行会安on后面的条件就行筛选查询 查询的次数是加法 这种方法要比笛卡尔积
    --效率高出很多很多
    select*from course
    --16、查询所有学生的Sname、Cname和Degree列。
    select*from course
    select*from student
    --按三个表之间的关系 连接三个表 按条件查询筛选
    select Sname,Cname,Degree from score join course on score.Cno=course.Cno join student on score.Sno=student.Sno
    --17、 查询“95033”班学生的平均分。
    select *from score
    select AVG(degree) from score where Sno in(select Sno from student where Class=95033) group by Sno
    --执行的顺序 1.from score 2.where 条件 3.group by 分组 4.AVG
  • 相关阅读:
    HDU5877
    HDU5874
    HDU5875
    广西党史知识竞赛活动
    知识竞答小程序更新记录
    答题小程序批量导入时增加对图片的支持
    关注】答题赢话费,安全用妆知识竞赛小程序上线啦!
    反向代理应知应会
    抽奖助手小程序v3
    答题小程序功能列表
  • 原文地址:https://www.cnblogs.com/happinesshappy/p/4446813.html
Copyright © 2011-2022 走看看