前言
- 所有数据和
sql
都是博主本人所写,仅供大家参考。 - 本实验不算难,建议先独自动手写
sql
,遇到困难时可以将我的sql
作为参考。
实验内容

运行环境
SQL Server 2022
SQL Server Management Studio Management Studio 19
本实验的全部SQL脚本
insert into S values('412109002126','xiaojing','男','2000-05-21','cs','上海');
insert into S values('131312321111','张三','男','2000-02-08','cs','上海');
insert into S values('256345611138','留香','女','2000-10-21','计算机学院','上海');
insert into C values('100002', '高等数学1','0000',6);
insert into C values('500001', '大学物理','0000',5);
insert into SC values('123131231111','100002', 100);
insert into SC values('131312321111','100002', 90);
insert into SC values('256345611138','100002', 80);
insert into SC values('412109002126','100002', 70);
insert into SC values('41210900286','100002', 60);
insert into SC values('419109070212','100002', 50);
insert into SC values('419109070226','100002', 40);
insert into SC values('420109070226','100002', 30);
insert into SC values('420109070426','100002', 20);
insert into SC values('421109070126','100002', 10);
insert into SC values('420109070226','500001', 80);
insert into SC values('420109070226','0000', 80);
insert into SC values('420109070226','0001', 80);
insert into SC values('420109070226','500012', 80);
select c2.* from c c1, c c2 where c1.cpno = c2.cno and c1.cpno is not null;
select s.sname, sc.cno from sc full join s on sc.sno = s.sno;
select s.sno, s.sname, s.saddress from s, sc
where s.sno = sc.sno and sc.cno = '500012' and sc.score between 80 and 90;
select s.sno, s.sname, c.cname, sc.score from sc, s, c where sc.sno = s.sno and sc.cno = c.cno;
select top 2 dept, persons from (
select sdept as dept, count(s.sno) as persons from s group by sdept
) tmp order by persons desc;
select s.sno, s.sname from sc, s where s.sno = sc.sno and sc.score < 50;
select s1.sno, s1.sname from s s1
where s1.sdept = '计算机学院' and datediff(yyyy, s1.Sbirthday,getdate()) = (
select datediff(yyyy,s2.Sbirthday,getdate()) from s s2 where s2.sname = '张三'
);
select top 10 sno, sname, score from (
select s.sno, s.sname, sc.score from sc, c, s
where sc.cno = c.cno and c.cname = '高等数学1' and s.sno = sc.sno
) tmp order by score desc;
select sc.sno, cno from sc,(
select sno, avg(score) avg_score from sc group by sno
) tmp where score > avg_score and sc.sno = tmp.sno;
select sno, sname, sdept from s where s.sdept != '计算机学院' and s.Sbirthday <= ALL(
select tmp.sbirthday from s tmp where tmp.Sdept = '计算机学院'
);
select * from s where s.sno not in (
select distinct sno from sc where cno is not null
);
select * from s, (
select sno, sum(case when score > 85 then 1 else 0 end) as cnt from sc group by sno
) tmp where s.sno = tmp.sno and cnt >= 2;
select sc.cno from sc, (
select count(sno) cnt, cno from sc group by cno
) tmp where 1 = tmp.cnt and sc.cno = tmp.cno;
select * from s where not exists (
select * from sc where sc.sno = s.sno and sc.cno = 500001
);
select * from s where not exists (
select * from c where not exists(
select * from sc where sc.cno = c.cno and sc.sno = s.sno
)
)
select * from sc x where not exists (
select * from sc y where y.sno = (
select sno from s where s.sname = '李勇'
) and not exists (
select * from sc z where z.cno = y.cno and z.sno = x.sno
)
)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)