--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('李响','')
INSERT INTO Teachers VALUES('梁茜','')
INSERT INTO Teachers VALUES('刘彬','')
INSERT INTO Teachers VALUES('刘青','')
INSERT INTO Teachers VALUES('魏能','')
INSERT INTO Teachers VALUES('段玉','') --课程
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
)

SQLSERVER教师学生成绩课程四表联合各种SQL考题的更多相关文章

  1. 学生成绩管理系统(SSM+MySQL+JSP)

    开发工具:Eclipse前端技术:基础:html+css+JavaScript框架:JQuery+H-ui后端技术:Spring+SpringMVC+mybatis模板引擎:JSP数据库:mysql ...

  2. [LINQ2Dapper]最完整Dapper To Linq框架(六)---多表联合与匿名类型返回

    目录 [LINQ2Dapper]最完整Dapper To Linq框架(一)---基础查询 [LINQ2Dapper]最完整Dapper To Linq框架(二)---动态化查询 [LINQ2Dapp ...

  3. 学生表sid,sname,结果表cid,cname,学生成绩表sid,cid,cscore,最高要求的分数输出候补课程专门命名

    --1.建表SQL: --学生表: -- Createtable createtable STUDENT ( SID   NUMBERnotnull, SNAME NVARCHAR2) ) table ...

  4. Oracle数据库作业-6 查询“张旭“教师任课的学生成绩。

    23.查询"张旭"教师任课的学生成绩. select * from score s where cno in ( select cno from course where tno ...

  5. Java课程设计—学生成绩管理系统(201521123005 杨雪莹)

    一.团队课程设计博客链接 学生成绩管理系统 二.个人负责模块或任务说明 学生成绩录入 显示所有学生信息 显示各科平均成绩 显示学生成绩(按降序排序) 三.自己的代码提交记录截图 四.自己负责模块或任务 ...

  6. Java课程设计—学生成绩管理系统(201521123004-林艺如)

    1.团队课程设计博客 团队课程设计博客链接 2.个人负责模块或任务说明 ①.Menu Menu.jsp 在页面中给出提示,用HTML的 MenuTeacher.jsp 利用Menu.jsp进行具体化完 ...

  7. Java课程设计报告——学生成绩管理系统

    一.需求分析 1.数据存储在数据库和文件中 2.分为"教师"模块和"学生"模块. 3.学生模块提供登陆功能,登陆成功后可查询数学.Java.体育成绩 (学生学号 ...

  8. Java课程设计--学生成绩管理系统

    一.团队名称: 团队成员 林艺薇 201721123032 网络1712 黄毓颖 201721123033 网络1712 唐川 201721123034 网络1712 梁才玉 201721123038 ...

  9. mysql经典面试题之学生成绩表

    需要数据库表1.学生表 Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 2.课程表 Course(CID, ...

随机推荐

  1. Java 之 框架概述

    一.什么是框架 框架(Framework)是整个或部分系统的可重用设计,表现为一组抽象构件及构件实例间交互的方法;另一种定义认为,框架是可被应用开发者定制的应用骨架.前者是从应用方面而后者是从目的方面 ...

  2. 一行Python代码画心型

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yanlaifan/article/deta ...

  3. iview input绑定enter事件

    在使用iview Input组件是,需要enter粗发相关的请求事件,但是在Input组件内不起效果: <Input placeholder="请输入查询信息" style= ...

  4. 复盘一篇讲sklearn库学习文章(上)

    认识 sklearn 官网地址: https://scikit-learn.gor/stable/ 从2007年发布以来, scikit-learn已成为重要的Python机器学习库, 简称sklea ...

  5. linux文件常用操作

    建立目录:mkdir mkdir -p [目录名] -p 递归创建 命令英文原意: make directories 切换所在目录:cd cd [目录] cd ~    进入当前用户的家目录 cd c ...

  6. 网页代码编辑器Blocs for mac如何进行行的控制?

    行是Blocs应用程序中的关键结构元素之一,它们是列和Bloc容器,它们构成了构建站点布局的基础.在本篇文章中,我们介绍的是网页代码编辑器Blocs for mac如何进行行的控制? ] 网页代码编辑 ...

  7. 191017 虚拟机centos修改IP

    1. 虚拟机设置 1.1 编辑-->虚拟机网络编辑器-->VMnet8-->更改设置-->DHCP设置取消打勾 -->选择NAT模式,查看网关IP 2. 本地网络设置 更 ...

  8. 持久化JS存储

    <script src="../../lib/persist-min.js"></script> //测试一下本地化存储器 var store = new ...

  9. test20190909 Gluttony

    0+0+0+0+0+0=0.毒瘤出题人. BJOI2019 勘破神机 地灾军团的军师黑袍从潜伏在精灵高层的密探手中得知了神杖的情报,他对奥术宝石中蕴含的远古神秘力量十分感兴趣.他设计夺取了数块奥术宝石 ...

  10. The Business Of Open Source

    http://oss-watch.ac.uk/resources/businessofopensource by Matthew Langham, Indiginox on 3 February 20 ...