经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » MS SQL Server » 查看文章
SQL?Server数据库连接查询和子查询实战案例
来源:jb51  时间:2023/4/17 9:15:53  对本文有异议

提示: 利用单表简单查询和多表高级查询技能,并且根据查询要求灵活使用内连接查询、外连接查询或子查询等。同时还利用内连接查询的两种格式、三种外连接查询语法格式和子查询的语法格式。

前言

内连接查询(不同表之间查询)

1.查询所有学生的学号、姓名、选修课程号和成绩

方法一

  1. USE XSCJ
  2. GO
  3. SELECT student.sno,sname,cno,grade from student,sc
  4. where student.sno=sc.sno

方法二

  1. USE XSCJ
  2. GO
  3. SELECT student.sno,sname,cno,grade
  4. from student join sc on student.sno=sc.sno

2.查询选修了课程名称为“数据库原理与应用”的学生的学号和姓名

方法一

  1. USE XSCJ
  2. select student.sno,sname from student,sc,course
  3. where student.sno=sc.sno and sc.cno=course.cno and cname='数据库原理与应用'

方法二

  1. select student.sno,sname from student join sc
  2. on student.sno=sc.sno join course on sc.cno=course.cno
  3. where cname='数据库原理与应用'

3.使用别名实现查询所有学生的学号、姓名、选修课程号和成绩

  1. select x.sno,sname,cno,grade
  2. from student x,sc y
  3. where x.sno=y.sno

自身连接查询

4.查询所有年龄比张文宝大的学生的姓名、性别和年龄

  1. select A.sname,A.ssex,A.sage
  2. from student A,student B
  3. where B.sname='张文宝' and A.sage>B.sage

使用第二种格式实现内连接查询(JOIN ON)

5.用格式二实现查询所有学生的学号、姓名、选修课程号和成绩

  1. SELECT student.sno,sname,cno,grade
  2. from student join sc
  3. on student.sno=sc.sno

外连接(左外连接)

6.查询所有学生的学号、姓名及对应选课的信息,如果该学生没有选课,也需要显示该生的学号和姓名

  1. SELECT student.sno,sname,cno,grade
  2. from student left outer join sc
  3. on student.sno=sc.sno

右外连接

7.查询选课学生的基本信息(若实际上有外键约束,这种情况是不存在的)

  1. select sc.sno,sname,cno,grade
  2. from sc right outer join student
  3. on student.sno=sc.sno

8.采用右外连接查询学生的学号、选修的课程号、课程名及学分,同时也列出无学生选修的课程信息

  1. select sc.sno,course.cno,cname,credit
  2. from sc right outer join course
  3. on course.cno=sc.cno

全外连接

9.student和sc表实现全外连接

  1. select *
  2. from sc full outer join student
  3. on student.sno=sc.sno

UNION联合查询

10.从student表中查询年龄为‘19’和‘20’的学生的系部,不包括重复行

  1. select sdept from student where sage='19'
  2. union
  3. select sdept from student where sage='20'

11.从student表中查询年龄为‘19’和‘20’的学生的系部,包括重复行

  1. select sdept from student where sage='19'
  2. union all
  3. select sdept from student where sage='20'

使用IN或NOT IN 的子查询

12.查询所有选修课程的学生的学号和姓名

  1. select sno,sname
  2. from student
  3. where sno in
  4. (select sno from sc)

改为连接查询实现

  1. select distinct student.sno,sname
  2. from student join sc
  3. on student.sno=sc.sno

使用比较运算符的子查询

13.查询年龄高于平均年龄的学生的学号、姓名和年龄

  1. select sno,sname,sage
  2. from student
  3. where sage>
  4. (select AVG(sage) from student)

使用ANY或ALL的子查询

14.查询比CS系的任一学生年龄都大的学生姓名和年龄

  1. select sname,sage
  2. from student
  3. where sage>any
  4. (select sage from student where sdept='CS')
  5. AND sdept!='CS'
  6. select * from student

使用EXISTS的子查询

15.查询已有学生选修的课程信息

  1. select *
  2. from course
  3. where exists
  4. (select * from sc where course.cno=sc.cno)

16.查询尚没有学生选修的课程信息

  1. select *
  2. from course
  3. where not exists
  4. (select * from sc where course.cno=sc.cno)

查看course表

抽取数据到另一个表

17.查询CS系学生的信息,生成一个新表temp

  1. select *
  2. into temp
  3. from student
  4. where sdept='CS'
  5. select * from temp

INSERT语句中的子查询

18.将所有的学号和课程号信息生成一个新表SCL

  1. INSERT INTO SCL(sno,cno)
  2. select sno,cno
  3. from student,course

UPDATE 语句中的子查询

19.将选修了“前台页面设计”课程的学生成绩增加5分

  1. UPDATE sc
  2. set grade=grade+5
  3. where cno=
  4. (select cno from course
  5. where sc.cno=course.cno and cname='前台页面设计')

删除语句中的子查询

20.删除选修了“前台页面设计”课程的选课信息

  1. delete from sc
  2. where cno=
  3. (select cno from course
  4. where sc.cno=course.cno and cname='前台页面设计')

总结

到此这篇关于SQL Server数据库连接查询和子查询的文章就介绍到这了,更多相关SQLServer连接查询和子查询内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号