zoukankan      html  css  js  c++  java
  • MySQL查询 45道练习题

    SQL查询45道练习题

    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 where Degree between 60 and 80
    或者
    select degree from score where Degree>60 and degree<80
    5.查询Score表中成绩为85,86或88的记录。
    select degree from score where 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 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(*) from student where class="95031"
    10.查询Score表中的最高分的学生学号和课程号。(子查询或者排序)
    排序查询:select * from score order by degree desc limit 0,1
    子查询:select sno,cno from score where degree=(select max(degree) from score)
    11.查询每门课的平均成绩。
    select cno,avg(degree) from score group by cno
    12.查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
    select avg(degree) from score where cno=( select cno from score group by cno having count(cno)>4 and cno like '3%')

    select avg(degree) from score where cno like '3%' and cno in(select cno from score group by cno having count(cno)>4)

    select avg(degree) from score group by cno having count(cno)>4 and cno like '3%'
    13.查询分数大于70,小于90的Sno列。
    select sno from score where degree>70 and degree<90;
    14.查询所有学生的Sname、Cno和Degree列。
    select student.Sname,score.Cno,score.Degree from student,score where student.Sno = score.Sno;
    15.查询所有学生的Sno、Cname和Degree列。
    select course.Cname,score.sno,score.Degree from course,score where course.cno = score.cno;
    16.查询所有学生的Sname、Cname和Degree列。
    select student.Sname,course.Cname,score.Degree from student,course,score where score.Cno=course.Cno and score.Sno=student.sno
    17.查询“95033”班学生的平均分。
    select avg(degree) from score where sno in (select sno from student where 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列。
    select score.Cno,score.Sno,grade.rank from score,grade where score.Degree < grade.upp and score.Degree>grade.low;

    没有重名的列可以不加表名,下面的写法是不加表名的。

    select sno,cno,rank from score,grade where degree between low and upp
    19.查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。
    select * from student where sno in(select sno from score where degree > (select degree from score where sno=109 and cno='3-105') and cno='3-105');

    或者

    select * from score shere cno ='3-105' and degree >(select max(degree) from score where sno='109')

    或者

    select * from score shere cno ='3-105' and degree >(select max(degree) from score where sno='109' and cno='3-105')


    20.查询score中选学多门课程的同学中分数为非最高分成绩的记录。

    全部成绩的最高分:
    select student.sname,score.sno,score.cno,score.degree from student,score where student.sno=score.sno and score.sno in(select sno from score group by sno having count(cno)>1) and score.degree not in(select max(degree) from score)

    每一门的最高分:

    select * from score a where sno in(select sno from score group by sno having count(*)>1) and degree<(select max(degree) from score b where b.cno = a.cno)

    21.查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
    select * from student where sno in(select sno from score where degree > (select degree from score where sno=109 and cno='3-105') 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 where cno= (select cno from course where tno= (select tno from teacher where tname='张旭'));
    24.查询选修某课程的同学人数多于5人的教师姓名。
    select teacher.tname, course.cname,score.cno from teacher,course,score where teacher.tno=course.tno and course.cno=score.cno and score.cno= (select cno from score group by cno having count(cno)>5)
    25.查询95033班和95031班全体学生的记录。
    select distinct student.*,score.* from student,score where student.sno=score.sno
    26.查询存在有85分以上成绩的课程Cno.
    select distinct cno from score where degree>85
    27.查询出“计算机系“教师所教课程的成绩表。
    select * from score where cno in(select cno from course where tno in(select tno from teacher where depart='计算机系'));
    28.查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof

    理解一:计算机系的教室和电子工程系的不同的。
    select depart,tname,prof from teacher where prof not in(select prof from teacher where depart='电子工程系')

    理解二:两系互不相同的。

    select tname,prof from teacher where depart='计算机系'  and prof not in(select prof from teacher where depart='电子工程系')

    相关子查询:

    select Tname,Prof from Teacher a where Prof not in(select Prof from Teacher b where a.Depart!=b.Depart)


    29.查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
    select cno,sno,degree from score a where cno='3-105' and degree>any(select degree from score b where cno='3-245' and b.sno=a.sno) order by degree desc

    any 代表括号内的任意一个
    30.查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.

    select cno,sno,degree from score a where cno='3-105' and degree>all(select degree from score b where cno='3-245' and b.sno=a.sno) order by degree desc
    all代表所有的


    31.查询所有教师和同学的name、sex和birthday.
    select teacher.Tname as name,teacher.Tsex as sex,teacher.Tbirthday as birthday from teacher
    union
    select student.Sname as name,student.Ssex as sex,student.Sbirthday as birthday from student
    32.查询所有“女”教师和“女”同学的name、sex和birthday.
    select teacher.Tname as name,teacher.Tsex as sex,teacher.Tbirthday as birthday from teacher where tsex='女'
    union
    select student.Sname as name,student.Ssex as sex,student.Sbirthday as birthday from student where ssex='女'
    33.查询成绩比该课程平均成绩低的同学的成绩表。
    select * from score a where degree<(select avg(degree) from score b where b.cno =a.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 not in (select tno from course where cno in (select distinct cno from score))
    36.查询至少有2名男生的班号。
    select distinct class from student a where (select count(ssex) from student b where ssex ='男' and a.class=b.class )>1
    37.查询Student表中不姓“王”的同学记录。
    select * from student where sname not like '王%'
    38.查询Student表中每个学生的姓名和年龄。
    select student.sname,(2016-year(sbirthday)) as sage from student where true
    网上查询的year(curdate())意思是当前年
    select student.sname,(year(curdate())-year(sbirthday)) as sage from student where true
    39.查询Student表中最大和最小的Sbirthday日期值。
    select min(sbirthday),max(sbirthday) from student
    40.以班号和年龄从大到小的顺序查询Student表中的全部记录。
    select * from student order by class desc,sbirthday asc
    41.查询“男”教师及其所上的课程。
    select teacher.Tname,course.Cname from teacher,course where course.Tno=teacher.Tno and tsex='男'
    42.查询最高分同学的Sno、Cno和Degree列。
    select sno,cno,degree from score where degree=(select max(degree) from score)
    43.查询和“李军”同性别的所有同学的Sname.
    select sname from student where ssex=(select ssex from student where sname='李军')
    44.查询和“李军”同性别并同班的同学Sname.
    select sname from student a where ssex=(select ssex from student b where sname='李军' and a.class=b.class)
    45.查询所有选修“计算机导论”课程的“男”同学的成绩表。
    select student.sno,student.sname,score.degree from student,course,score where student.ssex='男' and course.cname='计算机导论' and student.sno=score.sno and course.cno=score.cno

    做题经验总结:

    1.做题过程中感觉比较难的是12、20、24、45题。12、20、24题能想出逻辑,后面的条件的写法比较不好写,试过很多遍才试写出来。45题直接想不出来写法。

    2.对12题的理解

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

      先查出至少有5名学生选修的并以3开头的课程,用到的是分组查询select 列 from 表 group by 列 having 条件,后面有2个条件,用and连接。

    3.对20题的理解(20题和24题解法相近)

      查询score中选学多门课程的同学中分数为非最高分成绩的记录。
    select student.sname,score.sno,score.cno,score.degree from student,score where student.sno=score.sno and score.sno in(select sno from score group by sno having count(cno)>1) and score.degree not in(select max(degree) from score)

    涉及到多个表时,可以采取这种写法。先写出想查询的列,然后写关系。注意关系的写法,后面2个关系为选修课程大于1门的和分数不属于最高分的,and后面不是直接写关系,而是先写需要限制关系的列,括号里写限制的条件。关系的写法可以认为是固定写法。

    4.对45题的理解

      查询所有选修“计算机导论”课程的“男”同学的成绩表。
    select student.sno,student.sname,score.degree from student,course,score where student.ssex='男' and course.cname='计算机导论' and student.sno=score.sno and course.cno=score.cno

       这道题涉及到了3张表,其关系是student表和course表都和score表有关系,但是彼此之间没有联系。最初的想法是先写出score表的degree列,再去找限制关系,但是2个没有关系的表的限制关系无法写在一个表中。正确的查询方法是先写出这3个表的存在关系的列,让把3张表有了联系,再去写3张表的关系。

      查询最主要的是两个方面,一个是找要查询的列与表或者表与表的关系,个人感觉稍微复杂点的(2个表或者2个以上的表)就可以用45题的解法,;二是写条件,条件的写法注意以下题中的写法:

    6题:class=95031 or ssex='女'  字符串要加‘’。

    10题:limit0,1跳过0条数据取1条,意思就是取第一条数据。

    12题:where cno=( select cno from score group by cno having count(cno)>5 and cno like '3%')分组查询后又加一个条件,用and连接。

    18题:where score.Degree < grade.upp and score.Degree>grade.low 分数可以直接比较

    20题:and score.sno in(select sno from score group by sno having count(cno)>1) and score.degree not in(select max(degree) from score)   and后面加上score.sno in再写条件,否则没法加限制条件。

    22题:year(sbirthday) 生日取年,例如2016-07-12,取出来的是2016。

    28题:!=表示不等于

    38题:year(curdate())意思是当前年

  • 相关阅读:
    批量插入测试脚本
    Show Profile分析sql语句的资源消耗
    慢查询日志
    ORDER BY优化
    Join查询
    Explain(执行计划)分析
    索引
    MySQL中的DML(数据操作语言)和DQL(数据查询语言)
    MySQL中的DDL(数据定义语言)和DCL(数据控制语言)
    MySQL架构体系介绍
  • 原文地址:https://www.cnblogs.com/Strive-count/p/5977107.html
Copyright © 2011-2022 走看看