当前位置:   article > 正文

数据库原理实验三:使用SQL语言进行简单查询_在student、sc、course表输出软件工程系学生的学号、姓名、系别、课程名及成

在student、sc、course表输出软件工程系学生的学号、姓名、系别、课程名及成

实验目的

掌握简单数据查询操作。

实验内容

使用各种查询条件完成指定的查询操作

实验步骤

1、创建学生表student、课程表course和选课表SC,并输入数据(注意数据的完整性);

2、对各表中的数据进行不同条件的查询;

实验过程(还是使用实验一的表)

1、查询全体学生的学号和姓名

  1. select sno,sname
  2. from student;

2、查询全体学生的详细记录

  1. select *
  2. from student;

3、查询软件学院的学生姓名、年龄、系别

  1. select sname,sage,sdept
  2. from student
  3. where sdept='MA';

4、查询所有选修过课程的学生学号(不重复)

  1. select distinct sno
  2. from sc;

5、查询考试不及格的学生学号(不重复)

  1. select distinct sno
  2. from sc
  3. where grade<60;

6、查询不是软件学院、计算机系的学生性别、年龄、系别

  1. select ssex,sage,sdept
  2. from student
  3. where sdept not in('CS','MA');

7、查询年龄18-20岁的学生学号、姓名、系别、年龄

  1. select sno,sname,sdept,sage
  2. from student
  3. where sage>=18 and sage<=20;

8、查询姓刘的学生情况

  1. select *
  2. from student
  3. where sname like '刘%';

9、查询姓刘或姓李的学生情况

  1. select *
  2. from student
  3. where sname like '刘%' or sname like '李%';

10、查询姓刘且名字为两个字的学生情况

  1. select *
  2. from student
  3. where sname like '刘_';

11、查询1983年以后出生的学生姓名

  1. select sname
  2. from student
  3. where sage < 2019-1983

12、创建表 studentgrad(sno,mathgrade,englishigrade,chinesegrade)计算学生各科总成绩并赋予别名

  1. create table studentgrade(
  2. Sno char(8) ,
  3. mathgrade int,
  4. englishigrade int,
  5. chinesegrade int
  6. )
  1. select sum(mathgrade+chinesegrade+englishigrade) '学生总成绩'
  2. from studentgrade;

 13、利用内部函数 year()查找软件学院学生的出生年份

  1. select (year(getdate())-student.sage+1)
  2. from student
  3. where sdept='MA';

14、利用字符转换函数实现字符联接。select sname+‘年龄为’+cast(sage as char(2))+’岁’ from student

  1. select sname+'年龄为'+cast(sage as char(2))+'岁'
  2. from student;

15、查询全体学生情况,查询结果按所在系升序排列,对同一系中的学生按年龄降序排列

  1. select *
  2. from student
  3. order by sdept,sage desc;

16、查询学生总人数

  1. select count(*)
  2. from student;

17、查询选修了课程的学生人数

  1. select count(distinct sno)
  2. from sc;

18、查询选修了7号课程的学生总人数和平均成绩

  1. select count(*),avg(grade)as avggrade
  2. from student,sc
  3. where student.sno=sc.sno and sc.cno='7';

19、查询选修6号课程学生的最好成绩

  1. select max(grade) as maxgrade
  2. from sc
  3. where cno='6';

20、查询每个系的系名及学生人数

  1. select sdept,count(*)
  2. from student
  3. group by sdept;

21、查找每门课的选修人数及平均成绩

  1. select cno,count(*),avg(grade) as avggrade
  2. from sc
  3. group by cno;

22、查找没有先修课的课程情况

  1. select *
  2. from course
  3. where cpno is null;

实验总结

模糊查询用%  如like '刘%'
或者确定仅两个字的用_  如like '刘_'

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/40047
推荐阅读
相关标签
  

闽ICP备14008679号