SQL Server数据库连接查询和子查询实战案例
作者:长月. 时间:2024-01-15 02:44:21
提示: 利用单表简单查询和多表高级查询技能,并且根据查询要求灵活使用内连接查询、外连接查询或子查询等。同时还利用内连接查询的两种格式、三种外连接查询语法格式和子查询的语法格式。
前言
内连接查询(不同表之间查询)
1.查询所有学生的学号、姓名、选修课程号和成绩
方法一
USE XSCJ
GO
SELECT student.sno,sname,cno,grade from student,sc
where student.sno=sc.sno
方法二
USE XSCJ
GO
SELECT student.sno,sname,cno,grade
from student join sc on student.sno=sc.sno
2.查询选修了课程名称为“数据库原理与应用”的学生的学号和姓名
方法一
USE XSCJ
select student.sno,sname from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno and cname='数据库原理与应用'
方法二
select student.sno,sname from student join sc
on student.sno=sc.sno join course on sc.cno=course.cno
where cname='数据库原理与应用'
3.使用别名实现查询所有学生的学号、姓名、选修课程号和成绩
select x.sno,sname,cno,grade
from student x,sc y
where x.sno=y.sno
自身连接查询
4.查询所有年龄比张文宝大的学生的姓名、性别和年龄
select A.sname,A.ssex,A.sage
from student A,student B
where B.sname='张文宝' and A.sage>B.sage
使用第二种格式实现内连接查询(JOIN ON)
5.用格式二实现查询所有学生的学号、姓名、选修课程号和成绩
SELECT student.sno,sname,cno,grade
from student join sc
on student.sno=sc.sno
外连接(左外连接)
6.查询所有学生的学号、姓名及对应选课的信息,如果该学生没有选课,也需要显示该生的学号和姓名
SELECT student.sno,sname,cno,grade
from student left outer join sc
on student.sno=sc.sno
右外连接
7.查询选课学生的基本信息(若实际上有外键约束,这种情况是不存在的)
select sc.sno,sname,cno,grade
from sc right outer join student
on student.sno=sc.sno
8.采用右外连接查询学生的学号、选修的课程号、课程名及学分,同时也列出无学生选修的课程信息
select sc.sno,course.cno,cname,credit
from sc right outer join course
on course.cno=sc.cno
全外连接
9.student和sc表实现全外连接
select *
from sc full outer join student
on student.sno=sc.sno
UNION联合查询
10.从student表中查询年龄为‘19’和‘20’的学生的系部,不包括重复行
select sdept from student where sage='19'
union
select sdept from student where sage='20'
11.从student表中查询年龄为‘19’和‘20’的学生的系部,包括重复行
select sdept from student where sage='19'
union all
select sdept from student where sage='20'
使用IN或NOT IN 的子查询
12.查询所有选修课程的学生的学号和姓名
select sno,sname
from student
where sno in
(select sno from sc)
改为连接查询实现
select distinct student.sno,sname
from student join sc
on student.sno=sc.sno
使用比较运算符的子查询
13.查询年龄高于平均年龄的学生的学号、姓名和年龄
select sno,sname,sage
from student
where sage>
(select AVG(sage) from student)
使用ANY或ALL的子查询
14.查询比CS系的任一学生年龄都大的学生姓名和年龄
select sname,sage
from student
where sage>any
(select sage from student where sdept='CS')
AND sdept!='CS'
select * from student
使用EXISTS的子查询
15.查询已有学生选修的课程信息
select *
from course
where exists
(select * from sc where course.cno=sc.cno)
16.查询尚没有学生选修的课程信息
select *
from course
where not exists
(select * from sc where course.cno=sc.cno)
查看course表
抽取数据到另一个表
17.查询CS系学生的信息,生成一个新表temp
select *
into temp
from student
where sdept='CS'
select * from temp
INSERT语句中的子查询
18.将所有的学号和课程号信息生成一个新表SCL
INSERT INTO SCL(sno,cno)
select sno,cno
from student,course
UPDATE 语句中的子查询
19.将选修了“前台页面设计”课程的学生成绩增加5分
UPDATE sc
set grade=grade+5
where cno=
(select cno from course
where sc.cno=course.cno and cname='前台页面设计')
删除语句中的子查询
20.删除选修了“前台页面设计”课程的选课信息
delete from sc
where cno=
(select cno from course
where sc.cno=course.cno and cname='前台页面设计')
来源:https://blog.csdn.net/qq_66238381/article/details/130087594
标签:sqlerver,连接查询,子查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
通过数据库对Django进行删除字段和删除模型的操作
2024-01-19 09:28:44
Goland激活码破解永久版及安装详细教程(亲测可以)
2023-06-18 14:30:43
![](https://img.aspxhome.com/file/2023/7/86287_0s.png)
python3+openCV 获取图片中文本区域的最小外接矩形实例
2022-03-16 00:57:11
![](https://img.aspxhome.com/file/2023/9/103719_0s.jpg)
PHP实现手机号码中间四位用星号(*)隐藏的自定义函数分享
2023-11-06 06:36:22
搭建websocket消息推送服务,必须要考虑的几个问题
2023-11-28 12:23:45
![](https://img.aspxhome.com/file/2023/2/135192_0s.png)
Go语言中调用外部命令的方法总结
2024-05-13 10:44:09
![](https://img.aspxhome.com/file/2023/8/125108_0s.png)
pytorch中的自定义反向传播,求导实例
2021-08-07 06:57:53
PHP结构型模式之外观模式
2023-05-25 11:43:14
微信小程序录音实现功能并上传(使用node解析接收)
2024-04-16 08:46:56
Python中np.argmax()函数用法示例
2021-06-17 23:47:09
JS实现求5的阶乘示例
2024-04-30 10:09:43
![](https://img.aspxhome.com/file/2023/2/130502_0s.png)
CentOS 7上为PHP5安装suPHP的方法(彭哥)
2024-03-24 23:34:12
![](https://img.aspxhome.com/file/2023/3/67223_0s.png)
Swift 3.0在集合类数据结构上的一些新变化总结
2023-10-19 02:35:47
python学习之编写查询ip程序
2023-11-09 18:52:29
![](https://img.aspxhome.com/file/2023/2/95502_0s.jpg)
Go语言编程中字符串切割方法小结
2023-06-16 01:41:24
Python抓包并解析json爬虫的完整实例代码
2021-12-17 14:54:32
![](https://img.aspxhome.com/file/2023/5/120485_0s.png)
OpenCV Python实现拼图小游戏
2021-07-30 18:10:34
![](https://img.aspxhome.com/file/2023/2/79762_0s.gif)
vue项目部署到Apache服务器中遇到的问题解决
2024-05-22 10:42:36
![](https://img.aspxhome.com/file/2023/8/123218_0s.png)
JavaScript与JQuery框架基础入门教程
2024-04-22 22:23:08
Python Json数据文件操作原理解析
2022-10-27 19:00:32
![](https://img.aspxhome.com/file/2023/6/81556_0s.png)