mysql查询练习(二)

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

    查询3-105课程的平均分:
    mysql> select avg(degree) from score where cno='3-105';
        +-------------+
        | avg(degree) |
        +-------------+
        |     81.7500 |
        +-------------+
        1 row in set (0.00 sec)
        
    这种方法一次只能查询一个班级的平均值
    
    mysql> select cno,avg(degree) from score group by cno;
        +-------+-------------+
        | cno   | avg(degree) |
        +-------+-------------+
        | 3-105 |     81.7500 |
        | 3-106 |     80.2500 |
        | 3-107 |     67.0000 |
        | 3-108 |     50.3333 |
        +-------+-------------+
        4 rows in set (0.03 sec)
        
        解释:group by cno,先按照cno分组,在求每组的平均数
        
        
12、查询score表中至少有2名学生选修的并以3开头的课程的平均数

    mysql> select cno,avg(degree),count(*) from score group by cno having count(cno)>=2 and cno like '3%';
    +-------+-------------+----------+
    | cno   | avg(degree) | count(*) |
    +-------+-------------+----------+
    | 3-105 |     81.7500 |        4 |
    | 3-106 |     80.2500 |        4 |
    | 3-107 |     67.0000 |        3 |
    | 3-108 |     50.3333 |        3 |
    +-------+-------------+----------+
    4 rows in set (0.00 sec)
    
    解析:select cno,avg(degree),count(*) from score              #输出列为cno,degree平均数,总人数
          group by cno                                             #按照cno分组
          having count(cno)>=2 and cno like '3%'                #一个班的人数必须大于等于2且开头为3
          

13、查询分数大于70,小于90的sno列

    mysql> select sno,cno,degree from score where degree>70 and degree < 90;
    
    或者 where degree between 70 and 90
    
    +-----+-------+--------+
    | sno | cno   | degree |
    +-----+-------+--------+
    | 101 | 3-105 |     86 |
    | 102 | 3-107 |     75 |
    | 104 | 3-108 |     81 |
    | 105 | 3-105 |     82 |
    | 105 | 3-106 |     84 |
    | 106 | 3-107 |     78 |
    +-----+-------+--------+
    6 rows in set (0.00 sec)
    
    
14、查询所有学生的sname,cno,和degree列

    mysql> select sname,cno,degree from score,student where score.sno = student.sno;  #sno都在两表里面
    +-------+-------+--------+
    | sname | cno   | degree |
    +-------+-------+--------+
    | a1    | 3-105 |     86 |
    | a1    | 3-106 |     70 |
    | a3    | 3-107 |     75 |
    | a3    | 3-108 |     60 |
    | a4    | 3-105 |     59 |
    | a4    | 3-106 |     99 |
    | a5    | 3-107 |     48 |
    | a5    | 3-108 |     81 |
    | a6    | 3-105 |     82 |
    | a6    | 3-106 |     84 |
    | a7    | 3-107 |     78 |
    | a7    | 3-108 |     10 |
    | a2    | 3-105 |    100 |
    | a2    | 3-106 |     68 |
    +-------+-------+--------+
    14 rows in set (0.04 sec)
    

15、查询所有学生的sno,cname,degree列

    mysql> select sno,cname,degree from course,score where course.cno = score.cno; 
    +-----+----------+--------+
    | sno | cname    | degree |
    +-----+----------+--------+
    | 101 | english  |     86 |
    | 103 | english  |     59 |
    | 105 | english  |     82 |
    | 107 | english  |    100 |
    | 101 | english1 |     70 |
    | 103 | english1 |     99 |
    | 105 | english1 |     84 |
    | 107 | english1 |     68 |
    | 102 | english2 |     75 |
    | 104 | english2 |     48 |
    | 106 | english2 |     78 |
    | 102 | english3 |     60 |
    | 104 | english3 |     81 |
    | 106 | english3 |     10 |
    +-----+----------+--------+
    14 rows in set (0.00 sec)


16、查询所有学生的sname、cname和degree列(三个表)
    mysql> select sname,cname,degree from student,score,course where student.sno = score.sno and course.cno = score.cno;
    +-------+----------+--------+
    | sname | cname    | degree |
    +-------+----------+--------+
    | a1    | english  |     86 |
    | a4    | english  |     59 |
    | a6    | english  |     82 |
    | a2    | english  |    100 |
    | a1    | english1 |     70 |
    | a4    | english1 |     99 |
    | a6    | english1 |     84 |
    | a2    | english1 |     68 |
    | a3    | english2 |     75 |
    | a5    | english2 |     48 |
    | a7    | english2 |     78 |
    | a3    | english3 |     60 |
    | a5    | english3 |     81 |
    | a7    | english3 |     10 |
    +-------+----------+--------+
    14 rows in set (0.00 sec)
    

17、查询“95033”班学生每门课的平均分

    mysql> select sno,cno,avg(degree) from score where sno in (select sno from student where class='95033') group by cno; 
    +-----+-------+-------------+
    | sno | cno   | avg(degree) |
    +-----+-------+-------------+
    | 101 | 3-105 |     86.0000 |
    | 101 | 3-106 |     70.0000 |
    | 102 | 3-107 |     67.0000 |
    | 102 | 3-108 |     50.3333 |
    +-----+-------+-------------+
    4 rows in set (0.00 sec)
    
    先提取出95033的所有学生及成绩
    
18、查询选修“3-105”课程的成绩高于“105”号同学“3-105”成绩的所有同学的记录

    mysql> select * from score where cno='3-105' and degree > (select degree from score where sno='105' and cno ='3-105');
    +-----+-------+--------+
    | sno | cno   | degree |
    +-----+-------+--------+
    | 101 | 3-105 |     86 |
    | 107 | 3-105 |    100 |
    +-----+-------+--------+
    2 rows in set (0.05 sec)
    
    
19、查询成绩高于学号为“105”、课程号位“3-105”的成绩的所有记录

    mysql> select * from score where degree > (select degree from score where sno='105' and cno = '3-105');
    +-----+-------+--------+
    | sno | cno   | degree |
    +-----+-------+--------+
    | 101 | 3-105 |     86 |
    | 103 | 3-106 |     99 |
    | 105 | 3-106 |     84 |
    | 107 | 3-105 |    100 |
    +-----+-------+--------+
    4 rows in set (0.00 sec)
    
20、查询和学号为103、101的同学同年出生的所有学生的sno、sname和sbirthday列

    mysql> select * from student where year(sbirthday) in (select year(sbirthday) from student where sno in (101,103)); 
    +-----+-------+------+---------------------+-------+
    | sno | sname | ssex | sbirthday           | class |
    +-----+-------+------+---------------------+-------+
    | 101 | a1    | boy  | 1977-10-01 00:00:00 | 95033 |
    | 102 | a3    | boy  | 1977-08-12 00:00:00 | 95033 |
    | 103 | a4    | girl | 1978-10-01 00:00:00 | 95034 |
    | 104 | a5    | boy  | 1977-10-11 00:00:00 | 95033 |
    | 105 | a6    | girl | 1977-09-12 00:00:00 | 95034 |
    | 106 | a7    | boy  | 1977-08-01 00:00:00 | 95033 |
    | 107 | a2    | girl | 1978-09-11 00:00:00 | 95034 |
    +-----+-------+------+---------------------+-------+
    7 rows in set (0.09 sec)

21、查询“b3”教师任课的学生成绩

    mysql> select * from score where cno = ( select cno from course where tno in (select tno from teacher where tname='b2'));
    +-----+-------+--------+
    | sno | cno   | degree |
    +-----+-------+--------+
    | 101 | 3-105 |     86 |
    | 103 | 3-105 |     59 |
    | 105 | 3-105 |     82 |
    | 107 | 3-105 |    100 |
    +-----+-------+--------+
    4 rows in set (0.00 sec)
    
    
22、查询选修某课程的同学人数多于5人的教师姓名

    mysql> select tname from teacher where tno = (select tno from course where cno = (select cno from score having count(cno)>= 5));
    +-------+
    | tname |
    +-------+
    | b2    |
    +-------+
    1 row in set (0.00 sec)
    
    mysql> select tname from teacher where tno = (select tno from course where cno = (select cno from score group by cno having count(*)>= 5));
    +-------+
    | tname |
    +-------+
    | b2    |
    +-------+
    1 row in set (0.00 sec)
    

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

    mysql> select * from student where class in ('95033','95034');
    +-----+-------+------+---------------------+-------+
    | sno | sname | ssex | sbirthday           | class |
    +-----+-------+------+---------------------+-------+
    | 101 | a1    | boy  | 1977-10-01 00:00:00 | 95033 |
    | 102 | a3    | boy  | 1977-08-12 00:00:00 | 95033 |
    | 103 | a4    | girl | 1978-10-01 00:00:00 | 95034 |
    | 104 | a5    | boy  | 1977-10-11 00:00:00 | 95033 |
    | 105 | a6    | girl | 1977-09-12 00:00:00 | 95034 |
    | 106 | a7    | boy  | 1977-08-01 00:00:00 | 95033 |
    | 107 | a2    | girl | 1978-09-11 00:00:00 | 95034 |
    +-----+-------+------+---------------------+-------+
    7 rows in set (0.00 sec)
    

24、查询存在有85分以上成绩的课程cno
    mysql> select cno from score where degree > 85 group by cno; 
    +-------+
    | cno   |
    +-------+
    | 3-105 |
    | 3-106 |
    +-------+
    2 rows in set (0.00 sec)
    

25、查询出“computer”教师所有课程的成绩表

    mysql> select * from score where cno in (select cno from course where tno in (select tno from teacher where depart='computer'));
    +-----+-------+--------+
    | sno | cno   | degree |
    +-----+-------+--------+
    | 101 | 3-106 |     70 |
    | 103 | 3-106 |     99 |
    | 105 | 3-106 |     84 |
    | 107 | 3-106 |     68 |
    | 102 | 3-108 |     60 |
    | 104 | 3-108 |     81 |
    | 106 | 3-108 |     10 |
    +-----+-------+--------+
    7 rows in set (0.00 sec)

26、查询“computer”与“computer1”不同职称的教师的tname和prof

    mysql> select * from teacher where depart in ('computer1','computer') and prof not in (select prof from teacher where depart = 'computer')
        -> union
        -> select * from teacher where depart in ('computer1','computer') and prof not in (select prof from teacher where depart = 'computer1');
    +-----+-------+-------+---------------------+------+-----------+
    | tno | tname | tsex  | tbirthday           | prof | depart    |
    +-----+-------+-------+---------------------+------+-----------+
    | 806 | b3    | man   | 1958-12-02 00:00:00 | t2   | computer1 |
    | 807 | b4    | woman | 1958-12-12 00:00:00 | t1   | computer  |
    +-----+-------+-------+---------------------+------+-----------+
    2 rows in set (0.04 sec)
    
    
27、查询选修编号为“3-105”课程且成绩至少高于选修编号“3-108”的同学的cno、sno和degree,并按degree从高到低次序排序

    mysql> select * from score where degree > (select min(degree) from score where cno = '3-108') and cno = '3-105' order by degree desc;
    +-----+-------+--------+
    | sno | cno   | degree |
    +-----+-------+--------+
    | 107 | 3-105 |    100 |
    | 102 | 3-105 |     88 |
    | 101 | 3-105 |     86 |
    | 105 | 3-105 |     82 |
    | 103 | 3-105 |     59 |
    +-----+-------+--------+
    5 rows in set (0.01 sec)

    mysql> select * from score where cno='3-105' and degree > any(select degree from score where cno='3-108') order by degree desc;
    +-----+-------+--------+
    | sno | cno   | degree |
    +-----+-------+--------+
    | 107 | 3-105 |    100 |
    | 102 | 3-105 |     88 |
    | 101 | 3-105 |     86 |
    | 105 | 3-105 |     82 |
    | 103 | 3-105 |     59 |
    +-----+-------+--------+
    5 rows in set (0.00 sec)
    
    
28、查询选修编号为“3-105”且成绩高于选修编号为“3-106”课程的同学的成绩

    mysql> select * from score where cno='3-105' and degree > (select max(degree) from score where cno='3-106');
    +-----+-------+--------+
    | sno | cno   | degree |
    +-----+-------+--------+
    | 107 | 3-105 |    100 |
    +-----+-------+--------+
    1 row in set (0.16 sec)
    

    mysql> select * from score where cno ='3-105' and degree > all(select degree from score where cno = '3-106');  
    +-----+-------+--------+
    | sno | cno   | degree |
    +-----+-------+--------+
    | 107 | 3-105 |    100 |
    +-----+-------+--------+
    1 row in set (0.00 sec)
    


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

    mysql> select tname as name,tsex as sex,tbirthday as birthday from teacher
        -> union
        -> select sname,ssex,sbirthday from student;
    +------+-------+---------------------+
    | name | sex   | birthday            |
    +------+-------+---------------------+
    | b1   | man   | 1958-12-02 00:00:00 |
    | b2   | woman | 1958-12-12 00:00:00 |
    | b3   | man   | 1958-12-02 00:00:00 |
    | b4   | woman | 1958-12-12 00:00:00 |
    | a1   | boy   | 1977-10-01 00:00:00 |
    | a3   | boy   | 1977-08-12 00:00:00 |
    | a4   | girl  | 1978-10-01 00:00:00 |
    | a5   | boy   | 1977-10-11 00:00:00 |
    | a6   | girl  | 1977-09-12 00:00:00 |
    | a7   | boy   | 1977-08-01 00:00:00 |
    | a2   | girl  | 1978-09-11 00:00:00 |
    +------+-------+---------------------+
    11 rows in set (0.00 sec)
    
    
30、查询所有“女”老师和“女”同学的name,sex和birthday

    mysql> select tname as name ,tsex as sex,tbirthday as birthday from teacher where tsex = 'woman'
        -> union
        -> select sname,ssex,sbirthday from student where ssex = 'girl';
    +------+-------+---------------------+
    | name | sex   | birthday            |
    +------+-------+---------------------+
    | b2   | woman | 1958-12-12 00:00:00 |
    | b4   | woman | 1958-12-12 00:00:00 |
    | a4   | girl  | 1978-10-01 00:00:00 |
    | a6   | girl  | 1977-09-12 00:00:00 |
    | a2   | girl  | 1978-09-11 00:00:00 |
    +------+-------+---------------------+
    5 rows in set (0.00 sec)
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值