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

    1、学生表                                                                                            2、教师表

                

    3、课程表                                                                   4、分数表

                          

    二、练习题

    1、查询Student表中的所有记录的Sname、Ssex和Class列。
    mysql> select Sname,Ssex,Class from Student;
    2、 查询教师所有的单位即不重复的Depart列
    
    mysql> Select Depart from teacher group by Depart;
    
    
    mysql> select distinct depart from teacher;
     
    3、 查询Student表的所有记录。
    mysql> select * from student;
    4、 查询Score表中成绩在60到80之间的所有记录
    mysql> select * from Score where Degree > 60 and Degree<80;
    
    
    mysql> select * from score where degree between 60 and 80;
    5、 查询Score表中成绩为85,86或88的记录。
    mysql> select * from Score where Degree = 85 or Degree=86 or Degree = 88;
    
    
    mysql> select * from Score where Degree in (85,86,88);
    
    

     6、 查询Student表中“95031”班或性别为“女”的同学记录。

    mysql> Select * from Student where Class=95031 or Ssex = '';
    7、 以Class降序查询Student表的所有记录。
    
    
    
    mysql> Select * from Student order by Class desc;
    
    

       8、 以Cno升序、Degree降序查询Score表的所有记录。

    
    
    mysql> Select * from (Select * from Score order by Cno) as s order by Degree desc;
    
    
    mysql> select * from score ORDER BY cno asc,degree desc;
    9、 查询“95031”班的学生人数。
    mysql> Select count(1) from Student where Class = 95031;

    10、 查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
    mysql> Select Sno,Cno from Score where Degree = (Select max(Degree) from Score);
    
    

    10.1查询Score表中除了每门课程最高分的学生学号和课程号。(子查询或者排序)

    mysql> Select Sno,Cno from Score where Degree not in (Select max(Degree) from Score group by Cno);

    11、 查询每门课的平均成绩。

    mysql> Select Cno,avg(Degree) from Score group by Cno;

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

    mysql> Select Cno,avg(Degree) from group by Cno having count(1)>=5 and Cno like "3%";
    mysql> Select Cno,avg(Degree) from Score group by Cno having count(1)>=5 and left(Cno,1)= "3";
    • 增加: 查询Score表中至少有3名学生选修的不以3开头的课程的平均分数
    mysql> Select Cno,avg(Degree) from Score group by Cno having count(Sno)>=3 and left(Cno,1)<>"3";


    13、查询分数大于等于70,小于等于90的Sno列。

    • 注意:between and包含边界值
    mysql> Select Sno from Score where Degree between 70 and 90;
    • 增加:查询分数小于70,大于90的Sno列
    • not between and不包含边界值
    mysql> Select Sno from Score where Degree not between 70 and 90;

    14、查询所有学生的Sname、Cno和Degree列。

    mysql> Select Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno;

    15、查询所有学生的Sno、Cname和Degree列。

    mysql> Select Sno,Cname,Degree from Score join Course on Course.Cno=Score.Cno;

     16、查询所有学生的Sname、Cname和Degree列。

    mysql> Select Sname,Cname,Degree from Course join (Select Sname,Cno,Degree from Student join Score on Student.Sno=Score.Sno) as s on Course.Cno=s.Cno;

    17、 查询“95033”班学生的平均分。

    • mysql> Select avg(Degree) from (Select Degree from Score join (Select Sno from Student where Class=95033) as s on Score.Sno = s.Sno) as d;
    • mysql> Select avg(Degree) from Score where Sno in (Select Sno from Student where Class = 95033);

    18、现查询所有同学的Sno、Cno和rank列

    19、查询选修“3-105”课程的,并且成绩高于“109”号同学成绩的所有同学的记录。

    mysql> Select * from Score where Degree > (Select Degree from Score where Cno = "3-105" and Sno = 109) and Cno ="3-105";

    21、 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。

    mysql> Select * from Score where Degree > (Select Degree from Score where Cno = "3-105" and Sno = 109);


    22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。

    mysql> Select Sno,Sname,Sbirthday from Student where year(Sbirthday) = (Select year(Sbirthday) from Student where Sno = 108);

    23、查询“张旭“教师任课的学生成绩。

    mysql> Select Sno,Degree,Cno from Score where Cno in (Select Cno from Course where Tno in (Select Tno from Teacher where Tname = "张旭"));


    24、查询选修某课程的同学人数多于5人的教师姓名。

    mysql> Select Tname from Teacher where Tno in (Select Tno from Course where Cno in (Select Cno from Score group by Cno having count(1) > 5));


     25、查询95033班和95031班全体学生的记录。

    mysql> Select * from Student where Class in (95033,95031);

    26、  查询存在有85分以上成绩的课程Cno.

    mysql> Select distinct Cno from Score where Degree > 85;

    27、查询出“计算机系“教师所教课程的成绩表。

    mysql> select * from score where cno in (select cno from course where Tno in (select tno from teacher where depart = "计算机系"));

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

    mysql> Select Sno,Cno,Degree from Score where Cno = "3-105" and Degree > any(Select Degree from Score where Cno="3-245");

    30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

    mysql> Select Sno,Cno,Degree from Score where Cno = "3-105" and Degree > all(Select Degree from Score where Cno="3-245");

    31、 查询所有教师和同学的name、sex和birthday.

    select sname,ssex,sbirthday from student union all select tname,tsex,tbirthday from teacher;

    32、查询所有“女”教师和“女”同学的name、sex和birthday.

    mysql> Select s.Sname,s.ssex,s.sbirthday from (select sname,ssex,sbirthday from student union all select tname,tsex,tbirthday from teacher) as s where s.Ssex = "女";
    (select sname,ssex,sbirthday from student where ssex = "女") union all (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

    mysql> Select tname,depart from teacher where tno in (select distinct tno from course where cno in (select distinct cno from score group by cno));

    36、查询至少有2名男生的班号 

    错误:

    mysql> select s.class from (select * from student where ssex = "男") as s group by s.class having count(1);

    正确:

    select class from student where ssex='' group by class having count(1) >=2

    37、查询Student表中不姓“王”的同学记录

    mysql> select * from student where left(sname,1) <>"王";
    mysql> select * from student where sname not like '王%';

    38、查询Student表中每个学生的姓名和年龄

    mysql> select sname,year(now())-year(Sbirthday) as age from student;

    39、查询Student表中最大和最小的Sbirthday日期值。

    mysql> select max(Sbirthday),min(Sbirthday) from student;

    40、以班号和年龄从大到小的顺序查询Student表中的全部记录。

    mysql> select * from student order by class desc,Sbirthday;
    mysql> select * from student ORDER BY class,year(now())-year(sbirthday);

    41、查询“男”教师及其所上的课程。

    mysql> select * from course where tno in (select tno from teacher where tsex = "男");

    42、查询最高分同学的Sno、Cno和Degree列。

    mysql> select sno,cno,degree from score where degree = (select max(degree) from score);

    43、查询和“李军”同性别的所有同学的Sname.

    mysql> Select sname from student where ssex = (select ssex from student where sname ="李军");

    44、查询和“李军”同性别并同班的同学Sname.

    mysql> Select sname from student where (ssex,class) in  (select ssex,class from student where sname ="李军");

    45、查询所有选修“计算机导论”课程的“男”同学的成绩表.

    mysql> select * from score where cno = (select cno from course where cname = "计算机导论") and sno in (select sno from student where ssex = "男")

    47、查询各科选课总人数和及格人数,以如下形式显示:课程编号,课程名称,总人数,及格人数,按课程编号升序排列

    select s.cno,cname,allnum,passnum from (select s1.cno,allnum,passnum from (select Cno,count(*) as allnum from score group by cno) as s1 join (select Cno,count(*) as passnum from score where degree >= 80 group by cno) as s2 on s1.cno=s2.cno) as s join course on s.cno=course.cno order by s.cno;
  • 相关阅读:
    Java常用的7大排序算法汇总
    swift 内存管理,WEAK 和 UNOWNED
    Java关键字final、static使用总结
    Swift对面向对象提供了良好的支持,下面介绍几个其独有的特性。
    如何自己动手实现 KVO(转)
    Method Swizzling 和 AOP 实践(转)
    Objective-C Runtime(转)
    在多线程中进行UI操作
    iOS 详解NSXMLParser方法解析XML数据方法
    用一张日落照片估算出地球的半径
  • 原文地址:https://www.cnblogs.com/yinqanne/p/9614493.html
Copyright © 2011-2022 走看看