【实验内容】
1.自然连接
2.自身连接
3.外连接
【实验步骤】
1.自然连接
(1)查询所有选课学生的学号、姓名、选课名称及成绩
USE xhjk
GO
SELECT S.SNO,SN,CN,SCORE FROM SC,S,C
WHERE S.SNO=SC.SNO AND C.CNO=SC.CNO
GO
image.png
(2)查询每门课程的课程号、任课教师姓名及其选课人数
USE xhjk
GO
SELECT C.CNO,TN,COUNT(SC.SNO)AS 学生人数 FROM T,TC,C,SC
WHERE T.TNO=TC.TNO AND C.CNO=TC.CNO AND C.CNO=SC.CNO
GROUP BY C.CNO,T.TN
GO
image.png
2.自身连接
(1)查询所有比刘伟工资高的教师姓名、工资和刘伟的工资
USE xhjk
GO
SELECT X.TN AS 姓名,X.SAL AS 教师工资,Y.SAL AS 刘伟工资 FROM T AS X,T AS Y
WHERE X.SAL>Y.SAL AND Y.TN='刘伟'
GO
image.png
(2)查询同时选修“高数”和“英语”的学生姓名、系名
USE xhjk
GO
SELECT DISTINCT(SN) AS 姓名,DEPT AS 系名 FROM C AS C1,C AS C2,SC AS SC1,SC AS SC2,S
WHERE C1.CNO=SC1.CNO AND C2.CNO=SC2.CNO AND
C1.CN='高数' AND C2.CN='英语' AND
SC1.SNO=SC2.SNO AND SC1.SNO=S.SNO
GO
image.png
3.外连接
(1)查询所有选课学生的学号、姓名、选课名称及成绩(没有选课的同学选课信息显示空)
USE xhjk
GO
SELECT S.SNO,SN,CN,SCORE FROM S
LEFT OUTER JOIN SC ON S.SNO=SC.SNO
LEFT OUTER JOIN C ON C.CNO=SC.CNO
GO
image.png
网友评论