数据库原理实验三:使用SQL语言进行简单查询

实验目的

掌握简单数据查询操作。

实验内容

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

实验步骤

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

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

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

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

select sno,sname 
from student;

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

select * 
from student;

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

select sname,sage,sdept
from student
where sdept='MA';

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

select distinct sno 
from sc;

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

select distinct sno
from sc
where grade<60;

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

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

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

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

8、查询姓刘的学生情况

select * 
from student
where sname like '刘%';

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

select *
from student
where sname like '刘%' or sname like '李%';

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

select * 
from student
where sname like '刘_';

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

select sname 
from student 
where sage < 2019-1983

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

create table studentgrade(
    Sno char(8) ,
    mathgrade int,
    englishigrade int,
    chinesegrade int
)
select sum(mathgrade+chinesegrade+englishigrade) '学生总成绩' 
from studentgrade;

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

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

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

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

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

select *
from student 
order by sdept,sage desc;

16、查询学生总人数

select count(*) 
from student;

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

select count(distinct sno) 
from sc;

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

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

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

select max(grade) as maxgrade 
from sc
where cno='6';

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

select sdept,count(*) 
from student
group by sdept;

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

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

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

select * 
from course 
where cpno is null;

实验总结

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

猜你喜欢

转载自blog.csdn.net/dyw_666666/article/details/91359448