数据库的单表查询
3.1实验目的及要求
掌握简单表的数据查询、数据排序和表链接查询的操作方法
3.2实验内容
简单查询操作和连接查询方式。
3.3实验步骤
- 3.3.1单表查询:
1.查询全体学生的学号和姓名
select sno,sname from student;
2.查询全体学生的所有信息
select * from student;
3.查询全体学生的姓名,出生年份,和所在系,并用小写字母表示所有系名;
select Sname,2020-Sage as csnf from Student
select sname,’出生年份为:’,year(getdate())-sage,lower(sdept)from student;
4.给上例的结果集指定列名
select name AS NAME,’Year of Birth:’CSNF,2020-sage BIRTHDAY,LOWER(sdept) DEPARTMENT from Student;
5.查询选修了课程的学生的学号;
select distinct sno from sc;
比较: select sno from sc;
6.查询年龄在20岁以下的学生的姓名及其年龄;
select sname,sage from student where sage<20;
7.查询考试成绩有不及格的学生的学号;
select distinct sno from sc where grade<60;
比较:select sno from sc where grade<60;
8.查询年龄在20-30岁之间的学生的姓名,姓名,所在系;
select sname,ssex,sdept from student where sage between 20 and 30;
9.查询IS,CS,MA系的所有学生姓名和性别;
select sname,ssex from student where sdept in(‘IS’,’MA’,’CS’);
10.查找所有性“李”的学生的姓名,学号和性别;
select sname,sno,ssex from student where sname like’李%’;
比较:将学生表中的’18180001’号学生的姓名‘李勇’改为‘李勇勇’,再执行;
select sname,sno,ssex from student where sname like ‘李’;
11.查询没有先行课的课程号cno和课程名cname;
select cno,cname from course where cpno is null;
- 2.3.2查询结果排序
12.查询了选修3号课程的学生的学号和成绩,并按分数降序排列;
select cno,cname from course where cpno is null;
13.查询全体学生的情况,查询结果按所在系号升序排列,同一系中的学生按年龄降序排列;
select * from student order by sdept ASC,sage DESC;