1、实验目的
(1) 掌握自连接的形式和应用场合
(2) 掌握表连接的外连接用法和应用场合
2、实验预习与准备
(1) 了解自连接的用法
(2) 了解外连接中左连接、右连接和全连接的作用
(3) 了解内外连接的区别
3、实验内容及步骤
(1) 查询所有姓“王”的学生姓名及其所选的课程号,课程名和期末考试成绩
SELECT student.sname, course.cno, course.cname, score.score
FROM student
JOIN score ON student.sno = score.sno
JOIN course ON score.cno = course.cno
WHERE student.sname LIKE '王%';
(2) 查询和“张丹丹”老师同一个院系的学生的姓名和院系名称
SELECT student.sname, class.department
FROM student
JOIN class ON student.clno = class.clno
WHERE class.department = (
SELECT department
FROM teacher
WHERE tname = '张丹丹');
(3) 查询和“朱凡”在同一个班的所有男同学的信息。
SELECT * FROM student
WHERE clno = (
SELECT clno
FROM student
WHERE sname = '朱凡')
AND sex = '男';
(4) 查询与“耿娇”同年出生的学生学号、姓名和电话(求出生年份:year(birth))
SELECT sno, sname, tel FROM student WHERE YEAR(birth) = (SELECT YEAR(birth) FROM student WHERE sname = '耿娇' );
(5) 查询计算机学院的女生姓名,及其所选的课程名以及该课程的平时成绩,期末成绩
SELECT s.sname, c.cname, sc.usual, sc.score
FROM student s
JOIN class cl ON s.clno = cl.clno
JOIN score sc ON s.sno = sc.sno
JOIN course c ON sc.cno = c.cno
WHERE cl.department = '计算机学院' AND s.sex = '女';
(6) 查询邮箱不为空并且 “数据库原理”课程期末成绩在80分以上的学生学号、姓名和院系,并按学生院系升序排列,同一院系的学生按出生日期降序排列
SELECT s.sno, s.sname, cl.department
FROM student s
JOIN class cl ON s.clno = cl.clno
JOIN score sc ON s.sno = sc.sno
JOIN course c ON sc.cno = c.cno
WHERE s.email IS NOT NULL AND c.cname = '数据库原理' AND sc.score >= 80
ORDER BY cl.department ASC, s.birth DESC;
(7) 查询“100101”课程比“200101”课程成绩高的学生学号
SELECT s.sno
FROM student s
JOIN score sc1 ON s.sno = sc1.sno
JOIN score sc2 ON s.sno = sc2.sno
WHERE sc1.cno = '100101' AND sc2.cno = '200101' AND sc1.score > sc2.score;
(8) 查询同时教授“010002”和“010003”号课程的教师所有信息。
SELECT *
FROM teacher t
WHERE EXISTS (
SELECT 1
FROM course_class cc
WHERE cc.tno = t.tno
AND cc.cno = '010002'
) AND EXISTS (
SELECT 1
FROM course_class cc
WHERE cc.tno = t.tno
AND cc.cno = '010003'
);
(9) 查询没有任何学生选学的课程编号和课程名称及学分
SELECT c.cno, c.cname, c.credit
FROM course c
WHERE NOT EXISTS (
SELECT 1
FROM score sc
WHERE sc.cno = c.cno
);
(10) “朱凡”同学没有选的课程名(选做)