这篇博文的均是以 SQL server 数据库为基础的基本查询语句的实现。涉及到学生表选课表和课程表,基本操作都附加在查询语句后,还请读者留意。

查询分析器下的语句实现:

create database scs;
use SCS;
create table student
(sno char(7)  primary key,sname char(10)  not null,ssex char(2)  ,sage tinyint,sdept char(20));
create table course
(cno char(6) primary key,cname char(20) not null,credit tinyint ,semster tinyint
);create table sc
(sno char(7)  ,cno char(6)  ,grade smallint,primary key (sno,cno),      ---主键为两个的时候,不能用列级约束foreign key(sno) references student(sno),     ---此时约束应该为表级约束,且foreign key(sno/cno),(sno/cno)在列级约束的时候可以省略,在表级约束不行foreign key(cno) references course(cno)
);
select *from student
insert into student
values('1512101','李勇','男',19,'计算机系'),
('1512102','刘晨','男',20,'计算机系'),
('1512103','王敏','女',18,'计算机系'),
('1512104','李小玲','女',19,'计算机系'),
('1521101','张立','男',22,'信息系'),
('1521102','吴宾','女',21,'信息系'),
('1521103','张海','男',20,'信息系'),
('1531101','钱小平','女',18,'数学系'),
('1531102','王大力','男',19,'数学系')select * from course
insert into course
values('c001','计算机文化学',3,1),
('c002','高等数学',6,1),
('c003','高等数学',3,2),
('c004','大学英语',6,2),
('c005','java',2,3),
('c006','程序设计',3,3),
('c007','数据结构',5,4),
('c008','操作系统',4,4),
('c009','数据库基础',4,5)select *from sc
insert into sc
values ('1512101','c001',90),
('1512101','c002',86),
('1512101','c003',92),
('1512101','c005',88),
('1512101','c006',null),
('1512102','c001',76),
('1512102','c002',78),
('1512102','c005',66),
('1512104','c002',66),
('1512104','c005',78),
('1512104','c008',66),
('1521102','c001',82),
('1521102','c005',75),
('1521102','c007',92),
('1521102','c009',50),
('1521103','c002',68),
('1521103','c006',null),
('1521103','c007',null),
('1521103','c008',78),
('1531101','c001',80),
('1531101','c005',50),
('1531101','c007',45),
('1531102','c001',80),
('1531102','c002',75),
('1531102','c005',85),
('1531102','c009',88)select sno as 学号,cno as 课程号,grade as 成绩 from sc     ---查询学生选课表的所有信息select sname as 姓名,sage as 年龄 ,sdept as 系名  from student where sdept='计算机系'    --查询计算机系的学生的姓名和年龄select sno as 学号,cno as 课程号, grade as 成绩 from sc where grade between 70 and 80    --查询成绩在70-80之间的学生的学号课程号和成绩select sname as 姓名,sage as 年龄,ssex as 性别, sdept as 系名 from student where sdept='计算机系' and ssex='男' and sage between 18 and 20    ---查询计算机系年龄在18-20岁之间的且性别为男的学生的姓名和年龄select MAX(grade) as 最高分数 from sc where cno='c001'   --查询课程号c001的课程的最高分数
select MAX(sage) as 最大年龄,MIN(sage) as 最小年龄 from student where sdept='计算机系'    ---查询计算机系学生的最大年龄和最小年龄select sdept as 系名,count(*) as 人数 from student
group by sdept                   --统计每个系的学生人数select cno as 课程号, COUNT(*) as 选课人数,MAX(grade) as 考试最高分 from sc
group by cno              ---统计每门课程的选课人数和考试的最高分select sno as 学号, COUNT(*)as 选课门数,sum(grade) as 考试总成绩 from sc
group by sno
order by 选课门数                   ---统计每个学生的选课门数和考试的总成绩,并按选课门数的升序显示结果select sno as 学号, grade =SUM(grade)from sc
group by sno   having SUM(grade)>200               ---10、查询总成绩超过200分的学生,要求列出学号,总成绩select sname as 姓名,sdept as 所在系 from student join sc  on student.sno=sc.sno where cno='c002'          --查询选修了c002号课程的学生的姓名和所在系
select sname as 姓名,cno as 课程,grade as 成绩 from student join sc on student.sno=sc.sno where grade>80
order by grade desc           ---查询成绩在80分以上的学生的姓名、课程号、成绩,并按成绩的降序排列结果
select sname as 姓名,student.sno as 学号,sdept as 所在系 from student join sc on student.sno=sc.sno join course on sc.cno=course.cno
where course.cno=null            ---查询哪些学生没有选课,要求列出其学号,姓名和所在系select  cname as 课程名,semster as 开课学期 from course where semster =
(select semster from course where cname='java'     ---查询与java在同一学期开设的课程的课程名和开课学期
)select sname as 姓名,sdept as 所在系,sage as 年龄 from student where sage=(select sage from student where  sname='李勇')      ---查询和李勇年龄相同的学生的学号姓名和所在系select  sname as 姓名,sdept as 所在系 from student join sc on student.sno=sc.sno join course on sc.cno=course.cnowhere course.cno='c001'             ---查询选修了c001课程的学生的姓名和所在系   ,这是连接查询select  sname as 姓名,sdept as 所在系 from student where sno=where (select cno from sc)----实验三实验四内容部分
select sname as 姓名,sdept as 所在系 from student  where sno in
(select sno from sc where cno='c001'      -----查询选修了“c001”号课程的学生的姓名和所在系
)---或者用到exists
select sname ,sdept from student  where exists
(select *from sc
where sno=student.sno
and cno='c001'
)select  student.sno ,sname ,cno,grade from student join sc on student.sno=sc.sno  where grade>80                    and sc.sno in (select sno from student where sdept='数学系')-----查询数学系成绩80分以上的学生的学号、姓名、课程号和成绩。select sname from student where sno in
( select sno from sc where grade=(select MAX(grade) from sc)      ----查询计算机系考试成绩最高的学生的姓名
)and sdept='计算机系'     select sname ,sdept from student where sno in (select sno from sc  join course on sc.cno=course.cno where grade=(select MAX(grade) from sc) and cname='数据结构'------查询数据结构考试成绩最高的学生的姓名和所在系。 子查询和连接查询套用
)select sname ,sdept from student where sno in
(select sno from sc where cno in (select cno from course where cname='数据结构'              ------查询数据结构考试成绩最高的学生的姓名和所在系) and grade= (select MAX(grade) from sc))
----1、建立一个存储过程proc_ student,要求该存储过程实现:查询某一位学生是否选修某门课程,
--若没有选修,则该生对这门课程进行选修(即在SC表中插入相应的学生学号和课程号);若已选修,则显示学生学号、课程名称及成绩。
--执行存储过程proc_ student。
---、删除所有新建的存储过程。if exists( select * from sysobjects where name='proc_student'  and type='p' )
drop proc proc_studentgo
create proc proc_student (@no char(7),@no1 char(6))
as
beginif exists (select * from student join sc on sc.sno=student.sno where sc.sno=@no and cno=@no1)select * from sc where sno=@no and cno =@no1else insert into sc(sno,cno)values(@no,@no1)
endexec proc_student '1512101','c004'select *from scdrop proc proc_student

所有作品均为作者原创,正处在学习数据库的道路上,希望学习数据库的你们,可以从我的

分享中有所收获。个人能力毕竟有限,如果大家有的地方可以进行查询语句的优化,欢迎大家留言指正。

copyright@作者

更多精彩内容请关注公众号:干货分享录

SQL server 数据库查询语句的基本实现相关推荐

  1. 优化SQL Server数据库查询方法

    本文详细介绍了优化SQL Server数据库查询方法. SQL Server数据库查询速度慢的原因有很多,常见的有以下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) ...

  2. SQL Server数据库查询区分大小写、全半角——排序规则的应用(转载)

    SQL Server数据库查询区分大小写.全半角--排序规则的应用 因为偶然的原因,需要在INNER JOIN联表时,让对应字段进行区分大小写的比较.而默认情况下建立的Sql Server数据库是不区 ...

  3. 数据库-SQL Server数据库查询速度慢(连接超时)原因及优化方法

    SQL Server数据库查询速度慢的原因有很多,常见的有以下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列 ...

  4. SQL Server数据库查询速度慢的原因和解决方法

    SQL Server数据库查询速度慢的原因和解决方法 参考文章: (1)SQL Server数据库查询速度慢的原因和解决方法 (2)https://www.cnblogs.com/MyChange/p ...

  5. Oracle+Sql Server相关查询语句

    上周处理过 Oracle.Sql Server 数据库相关数据,发现其实它们的 SQL 查询语句有些是不太一样的,比如行列转置和将查询结果插入新表.本人还是比较愿意写 SQL 语句的,互联网的技术日新 ...

  6. SQL server 高级查询语句

    1.系统函数 函数名 描述 举例 convert() 数据类型转换 selece convert(varchar(5),12345) 返回:字符串12345 cast() 数据类型转换,与conver ...

  7. sql server一个查询语句引发的死锁

    程序错误日志大量的报死锁错误,去数据库错误日志查看确实有很多死锁(应在数据库实例启动时执行dbcc traceon(1222,-1)开启死锁跟踪): 04/29/2016 14:07:51,spid3 ...

  8. SQL server 数据库——T-SQL语句基础

    T-SQL语句基础 1.创建数据库:create datebase 数据库名 2.删除数据库:delete datebase 数据库名 3.注释:/*一段 */            一行 -- 4. ...

  9. server多笔记录拼接字符串 sql_第四章、SQL Server数据库查询大全(单表查询、多表连接查询、嵌套查询、关联子查询、拼sql字符串的查询、交叉查询)...

    4.1.查询的类型 declare @value as int set @value = 50 select  'age:'as age,2008 years,@valueas va --这种查询时跟 ...

  10. SQL Server经典查询语句练习题及答案

    注意:在插入数据的时候,需要将zahowei改成中文,原数据是中文的,因为最近这个词不能过审,只能用拼音代替了,可能是那个人出了啥事吧,审核不通过就挺莫名其妙的 现在有一教学管理系统,具体的关系模式如 ...

最新文章

  1. python交互界面的退出
  2. 教你使用TensorFlow2对阿拉伯语手写字符数据集进行识别
  3. unity 日志级别_【Unity】通用的Debugger日志模块
  4. Atom飞行手册翻译: 2.7 ~ 2.10
  5. 医学影像设备学_2020考研:影像大咖告诉你,学影像,就业好,不求人。
  6. Spark基础学习笔记10:Scala集成开发环境
  7. udp helper 的使用
  8. Java并发编程(三)什么是线程池
  9. express文件上传中间件Multer详解
  10. 三基色、对比色、互补色之间的关系,图片调色基础
  11. 知识关联视角下金融证券知识图谱构建与相关股票发现
  12. linux 卸载 sdcc,Linux sdcc安装
  13. android 区分wifi是5G还是2.4G
  14. python bind绑定失败_Python tkinter之Bind(绑定事件)的使用示例
  15. 阿里云服务器购买流程
  16. android 教程 美化,安卓Buzz桌面DIY美化教程
  17. 拜占庭将军问题(二)——口头协议
  18. 国内渗透测试新神器--北极熊扫描器4.0
  19. 数据结构的六大排序算法详解
  20. Shell中的布尔运算与逻辑运算

热门文章

  1. (转发)贼牛逼的双数据源切换 赞一个 PS:关于最后事物无法切换的问题 也有解决方案 后续补上
  2. C#打开文件夹加载图片
  3. 什么是透明数据加密(TDE)?
  4. linux下解压rar和7z压缩文件
  5. DrawIO 基于MinIO以及OSS私有云方案
  6. 冗余技术----线路冗余与生成树技术及其安全增强
  7. Nginx-反向代理
  8. pytorch BCEWithLogitsLoss pos_weight参数解疑
  9. CSP2019滚粗记
  10. 微软Exchange多个高危漏洞通告