实验目的
1、掌握select语句的基本语法;
2、熟练掌握表的数据简单查询、数据排序;
3、熟练掌握表的连接查询的表示;
4、掌握等值连接与非等值连接、自身连接、外连接以及复合条件连接操作。
实验内容与步骤
在前面建立的数据库student000的表中进行如下操作:
1、查询全体学生的学号和姓名;
use 自己的数据库名字
select
sname,
sno
from
Student;
2、查询全体学生的详细记录;
use 自己的数据库名字
select
sname,
sno,
ssex,
sage,
sdept
from
Student;
3、查询全体学生的姓名、出生年份和所在系;
use 自己的数据库名字
select
sname,
2021-sage,
sdept
from
Student;
4、查询选修了课程的学生学号;
use 自己的数据库名字
SELECT
sno
FROM
sc
GROUP BY
sno;
5、查询计算机系全体学生的姓名;
use 自己的数据库名字
select
sname
from
Student
where
sdept='计算机'
6、查询考试成绩不及格的学生姓名;
use 自己的数据库名字
select
Student.sname
from
student,sc
where
sc.grade<60 and Student.sno=sc.sno;
7、查询年龄在20~23岁之间(包括20和23岁)的学生姓名、所在系和年 龄;
use 自己的数据库名字
select
sname,sdept,sage
from
student
where
sage>=20 and sage<=23;
8、查询年龄不在20~23岁之间的学生姓名、所在系和年龄;
use 自己的数据库名字
select
sname,sdept,sage
from
student
where
sage<20 or sage>23;
9、查询电子机械系和应用数学系学生的姓名和性别;
use 自己的数据库名字
select
sname,ssex
from
student
where
sdept='电子机械' or sdept='应用数学';
10、查询既不是电子机械系又不是应用数学系学生的姓名和性别;
use 自己的数据库名字
select
sname,ssex
from
student
where
sdept!='电子机械' and sdept!='应用数学';
11、查询所有姓刘的学生的学号和姓名;
use 自己的数据库名字
select
sno,sname
from
student
where
sname like'刘%';
12、求缺少成绩的学生的学号和课程号;
use 自己的数据库名字
select
sno,cno
from
sc
where
grade is NULL;
13、查询没有选课的学生的学号;
use 自己的数据库名字
select
Student.sno
from
Student
where Student.sno NOT IN (select distinct sc.sno from sc)
14、查询所有有成绩的学生的学号和课程号;
use 自己的数据库名字
select
sno,cno
from
sc
where
grade is not NULL;
15、查询计算机系年龄在20岁以下的学生姓名;
use 自己的数据库名字
select
sname
from
Student
where
sdept='计算机' and sage<20
16、查询每个学生及其选修课课程的情况;(用等值连接和自然连接两种方法查询,比较查询结果,分析等值连接和自然连接的区别);
等值连接:
use 自己的数据库名字
select Student.*,sc.*
from
Student,sc
where Student.sno=sc.sno
自然连接:
use 自己的数据库名字
select Student.sno,sname,ssex,sage,sdept,sc.cno,sc.grade from Student,sc where Student.sno=sc.sno;
17、求学生的学号、姓名、选修的课程名及成绩;
use 自己的数据库名字
select
Student.sno,Student.sname,course.cname,sc.grade
from
Student,course,sc
where Student.sno=sc.sno and course.cno=sc.cno
18、求选修了1号课程且成绩为90分以上的学生学号、姓名和成绩;
use 自己的数据库名字
select
sc.sno,sname,grade
from
sc,Student
where
sc.cno=1 and grade>90 and Student.sno=sc.sno
19、查询每一门课的间接先修课
use 自己的数据库名字
select
x.cno,
y.cpno
from
course x,
course y
where
x.cpno = y.cno