SQL Server内查询、联合查询、外查询
# 题目
14、查询所有学生的Sname、Cno和Degree列。
15、查询所有学生的Sno、Cname和Degree列。
17、查询“95033”班学生的平均分。
18、现查询所有同学的Sno、Cno、Degree和rank列(其中rank为成绩的等级,成绩转换成为等级的规则是:大于等于90分为A、小于90且大于等于80分为B、小于80且大于等于70分为C、小于70且大于等于60为D,小于60分为E)。
23、查询“张旭”老师听上课程的学生成绩。
29、查询选修编号为3-105课程,且成绩至少高于选修编号为3-245课程的同学的Cno、Sno、Degree,并按Degree从高到低次序排序。
41、查询“男”教师及其所上的课程。
43、查询和“李军”同性别的所有同学的Sname。
44、查询和“李军”同性别并同班的同学Sname。
45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
# 表结构如下
Student(学生表)结构 属性名 数据类型 可否为空 含义 Sno Char (3) 否 学号(主码) Sname Char (8) 否 学生姓名 Ssex Char (2) 否 学生性别 Sbirthday datetime 可 学生出生年月 Class Char (5) 可 学生所在班级
Course(课程表) 属性名 数据类型 可否为空 含义 Cno Char (5) 否 课程号(主键) Cname Varchar(10) 否 课程名称 Tno Char (3) 否 教工编号(外码)
Score(成绩表) 属性名 数据类型 可否为空 含义 Sno Char (3) 否 学号(外码) Cno Char (5) 否 课程号(外码) Degree Decimal(4,1) 可 成绩
Teacher(教师表) 属性名 数据类型 可否为空 含义 Tno Char (3) 否 教工编号(主码) Tname Char (4) 否 教工姓名 Tsex Char (2) 否 教工性别 Thirthday datetime 可 教工出生年月 Prof Char (6) 可 职称 Depart Varchar (10) 否 教工所在部门
# 创建数据库
请看上一章:SQLServer-创建表、查询、条件查询语句
# 创建数据表
CREATE TABLE Student (
Sno CHAR(3) NOT NULL PRIMARY KEY,
Sname CHAR(8) NOT NULL,
Ssex CHAR(2) NOT NULL,
Sbirthday DATETIME,
Class CHAR(5)
);
CREATE TABLE Teacher (
Tno CHAR(3) NOT NULL PRIMARY KEY,
Tname CHAR(4) NOT NULL,
Tsex CHAR(2) NOT NULL,
Tbirthday DATETIME,
Prof CHAR(6),
Depart VARCHAR(10) NOT NULL
);
CREATE TABLE Course (
Cno CHAR(5) NOT NULL PRIMARY KEY,
Cname VARCHAR(10) NOT NULL,
Tno CHAR(3) NOT NULL,
FOREIGN KEY (Tno) REFERENCES Teacher(Tno)
);
CREATE TABLE Score (
Sno CHAR(3) NOT NULL,
Cno CHAR(5) NOT NULL,
Degree DECIMAL(4,1),
PRIMARY KEY (Sno, Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),
FOREIGN KEY (Cno) REFERENCES Course(Cno)
);
#写入数据
INSERT INTO Student (Sno, Sname, Ssex, Sbirthday, Class) VALUES
('101', '王一', '男', '1997-01-01', '95031'),
('102', '李军', '男', '1997-10-01', '95032'),
('103', '陈帆', '女', '1997-12-12', '95033'),
('104', '吴地', '男', '1996-11-15', '95034'),
('105', '张天天', '女', '1997-12-10', '95035'),
('106', '杨虹', '女', '1996-05-12', '95036'),
('107', '罗硅', '男', '1998-01-02', '95031'),
('108', '曾国', '男', '1997-10-12', '95033');
INSERT INTO Teacher (Tno, Tname, Tsex, Tbirthday, Prof, Depart) VALUES
('A01', '张旭', '男', '1977-12-01', '教授', '计算机系'),
('A02', '韩趣', '女', '1980-01-01', '副教授', '计算机系'),
('A03', '杨辉', '男', '1985-01-02', '讲师', '电子工程系'),
('A04', '辛全', '男', '1986-11-12', '助教', '电子工程系'),
('A05', '唐艳', '女', '1988-09-03', '助教', '信息管理系');
INSERT INTO Course (Cno, Cname, Tno) VALUES
('3-105', '计算机导论', 'A01'),
('3-205', '数据结构', 'A02'),
('3-245', '程序设计', 'A04'),
('3-305', '数据库', 'A03');
INSERT INTO Score (Sno, Cno, Degree) VALUES
('101', '3-105', 80.0),
('101', '3-205', 88.0),
('101', '3-245', 90.5),
('102', '3-105', 85.0),
('102', '3-245', 68.0),
('103', '3-105', 75.0),
('103', '3-245', 58.0),
('103', '3-305', 80.5),
('104', '3-105', 87.0),
('104', '3-205', 95.0),
('104', '3-245', 89.0),
('105', '3-245', NULL),
('106', '3-105', NULL),
('107', '3-305', NULL),
('108', '3-205', NULL);
# 查询命令
--14、查询所有学生的Sname、Cno和Degree列。
SELECT Sname, Cno, Degree FROM Student JOIN Score ON Student.Sno = Score.Sno;
--15、查询所有学生的Sno、Cname和Degree列。SELECT Student.Sno, Course.Cname, Score.Degree FROM Student JOIN Score ON Student.Sno = Score.Sno JOIN Course ON Score.Cno = Course.Cno;
--17、查询“95033”班学生的平均分。SELECT AVG(Degree) AS AvgDegree FROM Score JOIN Student ON Score.Sno = Student.Sno WHERE Student.Class = '95033';
--18、现查询所有同学的Sno、Cno、Degree和rank列(其中rank为成绩的等级,成绩转换成为等级的规则是:大于等于90分为A、小于90且大于等于80分为B、小于80且大于等于70分为C、小于70且大于等于60为D,小于60分为E)。WITH RankedScores AS ( SELECT Student.Sno, Score.Cno, Score.Degree, CASE WHEN Score.Degree >= 90 THEN 'A' WHEN Score.Degree >= 80 THEN 'B' WHEN Score.Degree >= 70 THEN 'C' WHEN Score.Degree >= 60 THEN 'D' ELSE 'E' END AS rank FROM Student JOIN Score ON Student.Sno = Score.Sno)SELECT * FROM RankedScores;
--23、查询“张旭”老师听上课程的学生成绩。SELECT Student.Sno, Score.Degree FROM Student JOIN Score ON Student.Sno = Score.Sno JOIN Course ON Score.Cno = Course.Cno WHERE Course.Tno = (SELECT Tno FROM Teacher WHERE Tname = '张旭');
--29、查询选修编号为3-105课程,且成绩至少高于选修编号为3-245课程的同学的Cno、Sno、Degree,并按Degree从高到低次序排序。SELECT s1.Cno, s1.Sno, s1.Degree FROM Score s1 JOIN Score s2 ON s1.Sno = s2.Sno WHERE s1.Cno = '3-105' AND s2.Cno = '3-245' AND s1.Degree > s2.Degree ORDER BY s1.Degree DESC;
--41、查询“男”教师及其所上的课程。SELECT Teacher.Tname, Course.Cname FROM Teacher JOIN Course ON Teacher.Tno = Course.Tno WHERE Teacher.Tsex = '男';
--43、查询和“李军”同性别的所有同学的Sname。SELECT Sname FROM Student WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname = '李军');
--44、查询和“李军”同性别并同班的同学Sname。SELECT Sname FROM Student WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname = '李军') AND Class = (SELECT Class FROM Student WHERE Sname = '李军');
--45、查询所有选修“计算机导论”课程的“男”同学的成绩表。SELECT Student.Sno, Student.Sname, Score.Degree FROM Student JOIN Score ON Student.Sno = Score.Sno JOIN Course ON Score.Cno = Course.Cno WHERE Course.Cname = '计算机导论' AND Student.Ssex = '男';
# 查询命令(直接使用)
--14、查询所有学生的Sname、Cno和Degree列。
SELECT Sname, Cno, Degree
FROM Student
JOIN Score ON Student.Sno = Score.Sno;
--15、查询所有学生的Sno、Cname和Degree列。
SELECT Student.Sno, Course.Cname, Score.Degree
FROM Student
JOIN Score ON Student.Sno = Score.Sno
JOIN Course ON Score.Cno = Course.Cno;
--17、查询“95033”班学生的平均分。
SELECT AVG(Degree) AS AvgDegree
FROM Score
JOIN Student ON Score.Sno = Student.Sno
WHERE Student.Class = '95033';
--18、现查询所有同学的Sno、Cno、Degree和rank列(其中rank为成绩的等级,成绩转换成为等级的规则是:大于等于90分为A、小于90且大于等于80分为B、小于80且大于等于70分为C、小于70且大于等于60为D,小于60分为E)。
WITH RankedScores AS (
SELECT Student.Sno, Score.Cno, Score.Degree,
CASE
WHEN Score.Degree >= 90 THEN 'A'
WHEN Score.Degree >= 80 THEN 'B'
WHEN Score.Degree >= 70 THEN 'C'
WHEN Score.Degree >= 60 THEN 'D'
ELSE 'E'
END AS rank
FROM Student
JOIN Score ON Student.Sno = Score.Sno
)
SELECT * FROM RankedScores;
--23、查询“张旭”老师听上课程的学生成绩。
SELECT Student.Sno, Score.Degree
FROM Student
JOIN Score ON Student.Sno = Score.Sno
JOIN Course ON Score.Cno = Course.Cno
WHERE Course.Tno = (SELECT Tno FROM Teacher WHERE Tname = '张旭');
--29、查询选修编号为3-105课程,且成绩至少高于选修编号为3-245课程的同学的Cno、Sno、Degree,并按Degree从高到低次序排序。
SELECT s1.Cno, s1.Sno, s1.Degree
FROM Score s1
JOIN Score s2 ON s1.Sno = s2.Sno
WHERE s1.Cno = '3-105'
AND s2.Cno = '3-245'
AND s1.Degree > s2.Degree
ORDER BY s1.Degree DESC;
--41、查询“男”教师及其所上的课程。
SELECT Teacher.Tname, Course.Cname
FROM Teacher
JOIN Course ON Teacher.Tno = Course.Tno
WHERE Teacher.Tsex = '男';
--43、查询和“李军”同性别的所有同学的Sname。
SELECT Sname
FROM Student
WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname = '李军');
--44、查询和“李军”同性别并同班的同学Sname。
SELECT Sname
FROM Student
WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname = '李军')
AND Class = (SELECT Class FROM Student WHERE Sname = '李军');
--45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
SELECT Student.Sno, Student.Sname, Score.Degree
FROM Student
JOIN Score ON Student.Sno = Score.Sno
JOIN Course ON Score.Cno = Course.Cno
WHERE Course.Cname = '计算机导论'
AND Student.Ssex = '男';
# 全部命令
CREATE TABLE Student (
Sno CHAR(3) NOT NULL PRIMARY KEY,
Sname CHAR(8) NOT NULL,
Ssex CHAR(2) NOT NULL,
Sbirthday DATETIME,
Class CHAR(5)
);
CREATE TABLE Teacher (
Tno CHAR(3) NOT NULL PRIMARY KEY,
Tname CHAR(4) NOT NULL,
Tsex CHAR(2) NOT NULL,
Tbirthday DATETIME,
Prof CHAR(6),
Depart VARCHAR(10) NOT NULL
);
CREATE TABLE Course (
Cno CHAR(5) NOT NULL PRIMARY KEY,
Cname VARCHAR(10) NOT NULL,
Tno CHAR(3) NOT NULL,
FOREIGN KEY (Tno) REFERENCES Teacher(Tno)
);
CREATE TABLE Score (
Sno CHAR(3) NOT NULL,
Cno CHAR(5) NOT NULL,
Degree DECIMAL(4,1),
PRIMARY KEY (Sno, Cno),
FOREIGN KEY (Sno) REFERENCES Student(Sno),
FOREIGN KEY (Cno) REFERENCES Course(Cno)
);
INSERT INTO Student (Sno, Sname, Ssex, Sbirthday, Class) VALUES
('101', '王一', '男', '1997-01-01', '95031'),
('102', '李军', '男', '1997-10-01', '95032'),
('103', '陈帆', '女', '1997-12-12', '95033'),
('104', '吴地', '男', '1996-11-15', '95034'),
('105', '张天天', '女', '1997-12-10', '95035'),
('106', '杨虹', '女', '1996-05-12', '95036'),
('107', '罗硅', '男', '1998-01-02', '95031'),
('108', '曾国', '男', '1997-10-12', '95033');
INSERT INTO Teacher (Tno, Tname, Tsex, Tbirthday, Prof, Depart) VALUES
('A01', '张旭', '男', '1977-12-01', '教授', '计算机系'),
('A02', '韩趣', '女', '1980-01-01', '副教授', '计算机系'),
('A03', '杨辉', '男', '1985-01-02', '讲师', '电子工程系'),
('A04', '辛全', '男', '1986-11-12', '助教', '电子工程系'),
('A05', '唐艳', '女', '1988-09-03', '助教', '信息管理系');
INSERT INTO Course (Cno, Cname, Tno) VALUES
('3-105', '计算机导论', 'A01'),
('3-205', '数据结构', 'A02'),
('3-245', '程序设计', 'A04'),
('3-305', '数据库', 'A03');
INSERT INTO Score (Sno, Cno, Degree) VALUES
('101', '3-105', 80.0),
('101', '3-205', 88.0),
('101', '3-245', 90.5),
('102', '3-105', 85.0),
('102', '3-245', 68.0),
('103', '3-105', 75.0),
('103', '3-245', 58.0),
('103', '3-305', 80.5),
('104', '3-105', 87.0),
('104', '3-205', 95.0),
('104', '3-245', 89.0),
('105', '3-245', NULL),
('106', '3-105', NULL),
('107', '3-305', NULL),
('108', '3-205', NULL);
--14、查询所有学生的Sname、Cno和Degree列。
SELECT Sname, Cno, Degree
FROM Student
JOIN Score ON Student.Sno = Score.Sno;
--15、查询所有学生的Sno、Cname和Degree列。
SELECT Student.Sno, Course.Cname, Score.Degree
FROM Student
JOIN Score ON Student.Sno = Score.Sno
JOIN Course ON Score.Cno = Course.Cno;
--17、查询“95033”班学生的平均分。
SELECT AVG(Degree) AS AvgDegree
FROM Score
JOIN Student ON Score.Sno = Student.Sno
WHERE Student.Class = '95033';
--18、现查询所有同学的Sno、Cno、Degree和rank列(其中rank为成绩的等级,成绩转换成为等级的规则是:大于等于90分为A、小于90且大于等于80分为B、小于80且大于等于70分为C、小于70且大于等于60为D,小于60分为E)。
WITH RankedScores AS (
SELECT Student.Sno, Score.Cno, Score.Degree,
CASE
WHEN Score.Degree >= 90 THEN 'A'
WHEN Score.Degree >= 80 THEN 'B'
WHEN Score.Degree >= 70 THEN 'C'
WHEN Score.Degree >= 60 THEN 'D'
ELSE 'E'
END AS rank
FROM Student
JOIN Score ON Student.Sno = Score.Sno
)
SELECT * FROM RankedScores;
--23、查询“张旭”老师听上课程的学生成绩。
SELECT Student.Sno, Score.Degree
FROM Student
JOIN Score ON Student.Sno = Score.Sno
JOIN Course ON Score.Cno = Course.Cno
WHERE Course.Tno = (SELECT Tno FROM Teacher WHERE Tname = '张旭');
--29、查询选修编号为3-105课程,且成绩至少高于选修编号为3-245课程的同学的Cno、Sno、Degree,并按Degree从高到低次序排序。
SELECT s1.Cno, s1.Sno, s1.Degree
FROM Score s1
JOIN Score s2 ON s1.Sno = s2.Sno
WHERE s1.Cno = '3-105'
AND s2.Cno = '3-245'
AND s1.Degree > s2.Degree
ORDER BY s1.Degree DESC;
--41、查询“男”教师及其所上的课程。
SELECT Teacher.Tname, Course.Cname
FROM Teacher
JOIN Course ON Teacher.Tno = Course.Tno
WHERE Teacher.Tsex = '男';
--43、查询和“李军”同性别的所有同学的Sname。
SELECT Sname
FROM Student
WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname = '李军');
--44、查询和“李军”同性别并同班的同学Sname。
SELECT Sname
FROM Student
WHERE Ssex = (SELECT Ssex FROM Student WHERE Sname = '李军')
AND Class = (SELECT Class FROM Student WHERE Sname = '李军');
--45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
SELECT Student.Sno, Student.Sname, Score.Degree
FROM Student
JOIN Score ON Student.Sno = Score.Sno
JOIN Course ON Score.Cno = Course.Cno
WHERE Course.Cname = '计算机导论'
AND Student.Ssex = '男';