sql语句练习题

此篇博文提供最基础的sql语句部分题目,答案仅供参考。交流可csdn私信或评论本人。

前期准备:数据库导入三张表TEACHER、STUDENT、SCORE。
调整格式:col 列名 for a字符数;

题目

  1. 查询Student表中的所有记录的Sname、Ssex和Class列。
SQL> select Sname,Ssex,Class from Student;

SNAME                SSEX                      CLASS
-------------------- -------------------- ----------
曾华                 男                        95033
匡明                 男                        95031
王丽                 女                        95033
李军                 男                        95033
王芳                 女                        95031
陆君                 男                        95031

已选择6行。
  1. 查询教师所有的单位即不重复的Depart列。
SQL> select distinct Depart from Teacher;

DEPART
----------------------------------------
电子工程系
计算机系
  1. 查询Student表的所有记录。
SQL> select * from Student;
SNO            SNAME          SSEX           SBIRTHDAY      CLASS
-------------- -------------- -------------- -------------- ----------
108            曾华           男              01-9月 -77     95033
105            匡明           男              02-10月-75     95031
107            王丽           女              23-1月 -76     95033

SNO            SNAME          SSEX           SBIRTHDAY      CLASS
-------------- -------------- -------------- -------------- ----------
101            李军           男              20-2月 -76     95033
109            王芳           女              10-2月 -75     95031
103            陆君           男              03-6月 -74     95031

已选择6行。
  1. 查询Score表中成绩在60到80之间的所有记录。
SQL> select * from Score where degree between 60 and 80;

SNO                  CNO                      DEGREE
-------------------- -------------------- ----------
105                  3-245                        75
109                  3-245                        68
109                  3-105                        76
101                  3-105                        64
108                  3-105                        78
107                  6-106                        79

已选择6行。
  1. 查询Score表中成绩为85,86或88的记录。
SQL> select * from Score where degree in(85,86,88);

SNO                  CNO                      DEGREE
-------------------- -------------------- ----------
103                  3-245                        86
105                  3-105                        88
101                  6-166                        85
6. 查询Student表中“95031”班或性别为“女”的同学记录。
SQL> select * from Student where Class=95031 or Ssex='女';

SNO            SNAME          SSEX          SBIRTHDAY      CLASS
-------------- -------------- ------------- -------------- ----------
105            匡明           男             02-10月-75     95031
107            王丽           女             23-1月 -76     95033
109            王芳           女             10-2月 -75     95031

SNO            SNAME          SSEX          SBIRTHDAY      CLASS
-------------- -------------- ------------- -------------- ----------
103            陆君           男             03-6月 -74     95031
  1. 以Class降序查询Student表的所有记录。
SQL> select * from Student order by class desc;

SNO            SNAME          SSEX           SBIRTHDAY      CLASS
-------------- -------------- -------------- -------------- ----------
108            曾华           男              01-9月 -77     95033
107            王丽           女              23-1月 -76     95033
101            李军           男              20-2月 -76     95033

SNO            SNAME          SSEX           SBIRTHDAY      CLASS
-------------- -------------- -------------- -------------- ----------
109            王芳           女              10-2月 -75     95031
103            陆君           男              03-6月 -74     95031
105            匡明           男              02-10月-75     95031

已选择6行。
  1. 以Cno升序、Degree降序查询Score表的所有记录。
SQL> select * from score order by Cno,degree desc;

SNO                  CNO                      DEGREE
-------------------- -------------------- ----------
103                  3-105                        92
107                  3-105                        91
105                  3-105                        88
108                  3-105                        78
109                  3-105                        76
101                  3-105                        64
103                  3-245                        86
105                  3-245                        75
109                  3-245                        68
107                  6-106                        79
101                  6-166                        85

SNO                  CNO                      DEGREE
-------------------- -------------------- ----------
108                  6-166                        81

已选择12行。
  1. 查询“95031”班的学生人数。
SQL> select count(class) from student
  2  where class=95031;

COUNT(CLASS)
------------
           3
  1. 查询Score表中的最高分的学生学号和课程号。
SQL> select sno,cno
  2  from score
  3  where degree=(select max(degree)from score);

SNO        CNO
---------- --------------------
103        3-105	
  1. 查询‘3-105’号课程的平均分。
    统计课程号平均成绩
    select 课程号,avg(成绩) from 表名1,表名2,······ WHERE 条件
    group by 课程号
SQL> select cno,avg(degree) from score where cno='3-105' group by cno;

CNO                  AVG(DEGREE)
-------------------- -----------
3-105                       81.5
  1. 查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
SQL> select avg(degree)
  2  from score
  3  where cno = (
  4  select cno
  5  from score
  6  group by cno
  7  having count(cno)>=5
  8  )
  9  and
 10  cno like '3%';

AVG(DEGREE)
-----------
       81.5
  1. 查询最低分大于70,最高分小于90的Sno列。
SQL> select sno
  2  from score
  3  group by sno
  4  having
  5  min(degree)>70
  6  and
  7  max(degree)<90
 SNO
--------------------
105
108
  1. 查询所有学生的Sname、Cno和Degree列。
SQL> select sname,s.cno,s.degree
  2  from student,score s
  3  where
  4  student.sno = s.sno;

SNAME                CNO                      DEGREE
-------------------- -------------------- ----------
陆君                 3-245                        86
匡明                 3-245                        75
王芳                 3-245                        68
陆君                 3-105                        92
匡明                 3-105                        88
王芳                 3-105                        76
李军                 3-105                        64
王丽                 3-105                        91
曾华                 3-105                        78
李军                 6-166                        85
王丽                 6-106                        79

SNAME                CNO                      DEGREE
-------------------- -------------------- ----------
曾华                 6-166                        81

已选择12行。
  1. 查询所有学生的Sno、Cname和Degree列。
SQL> select sno,cname,degree
  2  from course,score
  3  where course.cno = score.cno;

SNO                  CNAME                                        DEGREE
-------------------- ---------------------------------------- ----------
103                  操作系统                                         86
105                  操作系统                                         75
109                  操作系统                                         68
103                  计算机导论                                       92
105                  计算机导论                                       88
109                  计算机导论                                       76
101                  计算机导论                                       64
107                  计算机导论                                       91
108                  计算机导论                                       78
101                  数据电路                                         85
108                  数据电路                                         81

已选择11行。
  1. 查询所有学生的Sname、Cname和Degree列。
SQL> select sname,cname,degree
  2  from student,course,score
  3  where student.sno = score.sno
  4  and
  5  score.cno = course.cno;

SNAME                CNAME                                        DEGREE
-------------------- ---------------------------------------- ----------
曾华                 数据电路                                         81
曾华                 计算机导论                                       78
匡明                 计算机导论                                       88
匡明                 操作系统                                         75
王丽                 计算机导论                                       91
李军                 数据电路                                         85
李军                 计算机导论                                       64
王芳                 计算机导论                                       76
王芳                 操作系统                                         68
陆君                 计算机导论                                       92
陆君                 操作系统                                         86

已选择11行。
  1. 查询“95033”班所选课程的平均分。
SQL> select avg(degree)
  2  from student,score
  3  where student.sno = score.sno
  4  and
  5  class = '95033';

AVG(DEGREE)
-----------
 79.6666667	
  1. 假设使用如下命令建立了一个grade表:
    –现查询所有同学的Sno、Cno和rank列。
    create table grade
    (low number,upp number,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’);
    commit;
SQL> select sno,cno,rank
  2  from score,grade
  3  where score.degree
  4  between grade.low
  5  and grade.upp;

SNO                  CNO                  RA
-------------------- -------------------- --
101                  3-105                D
109                  3-245                D
105                  3-245                C
109                  3-105                C
108                  3-105                C
107                  6-106                C
108                  6-166                B
101                  6-166                B
103                  3-245                B
105                  3-105                B
107                  3-105                A

SNO                  CNO                  RA
-------------------- -------------------- --
103                  3-105                A

已选择12行。
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐