SQLSERVER教师学生成绩课程四表联合各种SQL考题

时间:2022-05-06 03:36:21

--CREATE DATABASE EXAM_1
--GO

USE EXAM_1
--判断并删除表
IF OBJECT_ID(Scores) IS NOT NULL
DROP TABLE Scores;
GO

IF OBJECT_ID(Courses) IS NOT NULL
DROP TABLE Courses;
GO

IF OBJECT_ID(Students) IS NOT NULL
DROP TABLE Students;
GO

IF OBJECT_ID(Teachers) IS NOT NULL
DROP TABLE Teachers;
GO

CREATE TABLE Students
(
Id INT CONSTRAINT PK_SId PRIMARY KEY IDENTITY,
Name NVARCHAR(20) NOT NULL,
Age INT NOT NULL,
Sex BIT DEFAULT 1 NOT NULL
)


CREATE TABLE Teachers
(
Id INT CONSTRAINT PK_TId PRIMARY KEY IDENTITY,
Name NVARCHAR(20) NOT NULL,
CradId CHAR(18) NOT NULL,
)


CREATE TABLE Courses
(
Id INT CONSTRAINT PK_CId PRIMARY KEY IDENTITY,
Name NVARCHAR(20) NOT NULL,
TeacherId INT REFERENCES Teachers(Id)
)

CREATE TABLE Scores
(
Id INT CONSTRAINT PK_ScoreId PRIMARY KEY IDENTITY,
Score NUMERIC NOT NULL CHECK(Score >= 0 AND Score <= 100.00),
StudentId INT REFERENCES Students(Id),
CourseId INT REFERENCES Courses(Id)
)

GO

--学生
INSERT INTO Students VALUES(张三,20,0)
INSERT INTO Students VALUES(李四,21,1)
INSERT INTO Students VALUES(王五,22,1)
INSERT INTO Students VALUES(赵六,23,0)
INSERT INTO Students VALUES(李七,24,1)
INSERT INTO Students VALUES(孙八,25,1)

--老师
INSERT INTO Teachers VALUES(李响,130603391909203)
INSERT INTO Teachers VALUES(梁茜,130604391909203)
INSERT INTO Teachers VALUES(刘彬,130605391909203)
INSERT INTO Teachers VALUES(刘青,130606391909203)
INSERT INTO Teachers VALUES(魏能,130607391909203)
INSERT INTO Teachers VALUES(段玉,130608391909203)

--课程
INSERT INTO Courses VALUES(C#,1)
INSERT INTO Courses VALUES(SQLSERVER,1)
INSERT INTO Courses VALUES(HTML,2)
INSERT INTO Courses VALUES(CSS,3)
INSERT INTO Courses VALUES(JAVASCRIPT,3)
INSERT INTO Courses VALUES(VUE,3)

--分数
INSERT INTO Scores VALUES(90,1,1)
INSERT INTO Scores VALUES(96,1,2)
INSERT INTO Scores VALUES(95,1,3)
INSERT INTO Scores VALUES(70,2,4)
INSERT INTO Scores VALUES(50,2,5)
INSERT INTO Scores VALUES(66,3,5)
INSERT INTO Scores VALUES(88,3,4)
INSERT INTO Scores VALUES(66,4,4)

--(1)    查询平均成绩大于90分的同学的学生编号和平均成绩。
SELECT StudentId,AVG(Score) AS 平均分 FROM Scores GROUP BY StudentId HAVING AVG(Score) > 90

--(2)    查询所有学生的学生编号、姓名、选课数、总成绩。
SELECT Students.Id AS 学生编号,Students.Name AS 姓名,COUNT(Scores.CourseId) AS 选课数,SUM(Score) AS 总成绩 FROM Students
 INNER JOIN Scores ON Students.Id = Scores.StudentId
 INNER JOIN Courses ON Scores.CourseId = Courses.Id
 GROUP BY Students.Id,Students.Name

 --(3)    查询姓“李”的老师的个数。(3分)
 SELECT COUNT(*) AS 李姓老师个数 FROM
 (
 SELECT * FROM Teachers WHERE Teachers.Name LIKE 李%
 )
 AS T1

 --(4)    查询没学过“梁茜”老师课的同学的学号、姓名。
 SELECT Students.Id AS 学号,Students.Name AS 姓名 FROM Students
 INNER JOIN Scores ON Students.Id = Scores.StudentId
 WHERE Scores.CourseId NOT IN
 (
 SELECT Courses.Id AS CID FROM Courses
 INNER JOIN Teachers ON Courses.TeacherId = Teachers.Id
 WHERE Teachers.Name = 梁茜
 )

 --(5)    查询学过“C#”并且也学过“SQL Server”课程的同学的学号、姓名。(3分)
 SELECT Students.Id AS 学号,Students.Name AS 姓名 FROM Students
 INNER JOIN Scores ON Students.Id = Scores.StudentId
 INNER JOIN Courses ON Scores.CourseId = Courses.Id
 WHERE Courses.Name = C# OR  Courses.Name = SQLSERVER 
 GROUP BY Students.Id,Students.Name
 HAVING COUNT(Courses.Name) > 1


 --(6)    查询学过“刘彬”老师所教的所有课的同学的学号、姓名。(3分)
 SELECT Students.Id AS 学号,Students.Name AS 姓名 FROM Students
 INNER JOIN Scores ON Students.Id = Scores.StudentId
 WHERE Scores.CourseId IN
 (
 SELECT Courses.Id AS CID FROM Courses
 INNER JOIN Teachers ON Courses.TeacherId = Teachers.Id
 WHERE Teachers.Name = 刘彬
 )
 
 --(7)    查询课程“C#”的成绩比课程“SQL Server”课程低的所有同学的学号、姓名。
SELECT Students.Id AS 学号,Students.Name AS 姓名 FROM 
(
SELECT StudentId,
SUM(CASE NAME WHEN C# THEN Score END) AS C#,
SUM(CASE NAME WHEN SQLSERVER THEN Score END) AS SQLSERVER
 FROM
(
SELECT StudentId,Name,Score FROM Courses INNER JOIN Scores ON Courses.Id = Scores.CourseId
WHERE Name = C# OR Name = SQLSERVER
)
AS T1
GROUP BY T1.StudentId
)
AS T2
INNER JOIN Students
ON T2.StudentId = Students.Id
WHERE T2.C# < T2.SQLSERVER

--(8)    查询所有课程成绩小于90分的同学的学号、姓名。(3分)
SELECT Students.Id AS 学号,Students.Name FROM Students
WHERE Students.Id IN
(
SELECT StudentId FROM Scores WHERE Score < 90
)

--(9)    查询没有学全所有课的同学的学号、姓名。(3分)
SELECT Students.Id AS 学生编号,Students.Name AS 姓名 FROM Students
 INNER JOIN Scores ON Students.Id = Scores.StudentId
 INNER JOIN Courses ON Scores.CourseId = Courses.Id
 GROUP BY Students.Id,Students.Name
 HAVING COUNT(Scores.CourseId) < (SELECT COUNT(*) FROM Courses)

 --(10)    查询至少有一门课与学生编号为“2”的学生所学相同的学生的学号和姓名。
  SELECT Students.Id AS 学生编号,Students.Name AS 姓名 FROM Scores
 INNER JOIN Students ON Scores.StudentId = Students.Id
  WHERE CourseId IN
 (SELECT CourseId FROM Scores WHERE StudentId = 2)
 GROUP BY Students.Id,Students.Name
 HAVING COUNT(Students.Name) > 0

 --(11)    查询至少学过学生编号为“1”的学生的其中一门课的其他学生的姓名和性别。
  SELECT Students.Id AS 学生编号,Students.Name AS 姓名 FROM Scores
 INNER JOIN Students ON Scores.StudentId = Students.Id
  WHERE CourseId IN
 (SELECT CourseId FROM Scores WHERE StudentId = 1)
 AND StudentId != 1
 GROUP BY Students.Id,Students.Name
 HAVING COUNT(Students.Name) > 0

 --(12)    把成绩表中“梁茜”老师教的课的成绩都更改为此课程的平均成绩。
 SELECT Teachers.Name,Courses.Name AS 课程名称,AVG(Scores.Score) AS 平均成绩 FROM Teachers
 INNER JOIN Courses ON Teachers.Id = Courses.TeacherId
 INNER JOIN Scores ON Courses.Id = Scores.CourseId
 WHERE Teachers.Name = 梁茜
 GROUP BY Teachers.Name,Courses.Name

-- (13)    查询和学生编号为“2”的学生学习的课程完全相同的其他学生的姓名和性别。
select * from Students where id in
(
    select studentId from Scores where StudentId not in
    (
        select StudentId from Scores s where CourseId not in 
        ( 
            select CourseId from Scores where StudentId=2 
        )
    )
    group by StudentId having count(StudentId) =
    ( 
        select Count(0)  from Scores where StudentId=2
    )
) and Id!=2


--(14)    删除学习“刘彬”老师课的成绩表记录。(4分)
DELETE FROM Scores WHERE CourseId IN
(
SELECT Courses.Id FROM Teachers INNER JOIN Courses
ON Teachers.Id = Courses.TeacherId WHERE Teachers.Name = 刘彬
)

--(15)    按平均成绩从高到低显示所有学生的“C#”、“SQL Server”、“ASP.NET面向对象编程”三门的课程成绩。(4分)
SELECT * FROM 
(
    SELECT 
    ROW_NUMBER() OVER(PARTITION BY CourseId ORDER BY Score DESC) AS RANK,
    Students.Id,Students.Name FROM Scores 
    INNER JOIN Courses ON Scores.CourseId = Courses.Id
    INNER JOIN Students ON Scores.StudentId = Students.Id
)
AS T1
WHERE RANK <= 1

--(16)    查询不同老师所教不同课程平均分从高到低显示。
SELECT * FROM
(
SELECT Teachers.Name AS 老师姓名,Courses.Name AS 课程名称,AVG(Scores.Score) AS 平均分 FROM Teachers 
INNER JOIN Courses ON Teachers.Id = Courses.TeacherId
INNER JOIN Scores ON Scores.CourseId = Courses.Id
GROUP BY Courses.Name,Teachers.Name
)
AS T1
ORDER BY T1.平均分

--(19)    查询出只选修了一门课程的全部学生的学号和姓名。
SELECT Students.Id AS 学生编号,Students.Name AS 姓名 FROM Students WHERE Id IN
(
SELECT StudentId FROM Scores
GROUP BY StudentId
HAVING COUNT(StudentId) = 1
)