SQL?Server数据库连接查询和子查询实战案例(sql server连接查询语句)学到了吗

随心笔谈12个月前发布 admin
93 0



目录前言1.查询所有学生的学号、姓名、选修课程号和成绩2.查询选修了课程名称为“数据库原理与应用”的学生的学号和姓名3.使用别名实现查询所有学生的学号、姓名、选修课程号和成绩4.查询所有年龄比张文宝大的学生的姓名、性别和年龄5.用格式二实现查询所有学生的学号、姓名、选修课程号和成绩6.查询所有学生的学号、姓名及对应选课的信息,如果该学生没有选课,也需要显示该生的学号和姓名7.查询选课学生的基本信息(若实际上有外键约束,这种情况是不存在的)8.采用右外连接查询学生的学号、选修的课程号、课程名及学分,同时也列出无学生选修的课程信息9.student和sc表实现全外连接10.从student表中查询年龄为‘19’和‘20’的学生的系部,不包括重复行11.从student表中查询年龄为‘19’和‘20’的学生的系部,包括重复行12.查询所有选修课程的学生的学号和姓名13.查询年龄高于平均年龄的学生的学号、姓名和年龄14.查询比CS系的任一学生年龄都大的学生姓名和年龄15.查询已有学生选修的课程信息16.查询尚没有学生选修的课程信息17.查询CS系学生的信息,生成一个新表temp18.将所有的学号和课程号信息生成一个新表SCL19.将选修了“前台页面设计”课程的学生成绩增加5分20.删除选修了“前台页面设计”课程的选课信息总结

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

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

方法一

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

方法一

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=’数据库原理与应用’

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

自身连接查询

select A.sname,A.ssex,A.sage
from student A,student B
where B.sname=’张文宝’ and A.sage>B.sage

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

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

外连接(左外连接)

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

右外连接

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

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

全外连接

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

UNION联合查询

select sdept from student where sage=’19’
union
select sdept from student where sage=’20’

select sdept from student where sage=’19’
union all
select sdept from student where sage=’20’

使用IN或NOT IN 的子查询

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

使用比较运算符的子查询

select sno,sname,sage
from student
where sage>
(select AVG(sage) from student)

使用ANY或ALL的子查询

select sname,sage
from student
where sage>any
(select sage from student where sdept=’CS’)
AND sdept!=’CS’
select * from student

使用EXISTS的子查询

select *
from course
where exists
(select * from sc where course.cno=sc.cno)

select *
from course
where not exists
(select * from sc where course.cno=sc.cno)

查看course表

抽取数据到另一个表

select *
into temp
from student
where sdept=’CS’
select * from temp

INSERT语句中的子查询

INSERT INTO SCL(sno,cno)
select sno,cno
from student,course

UPDATE 语句中的子查询

UPDATE sc
set grade=grade+5
where cno=(select cno from course
where sc.cno=course.cno and cname=’前台页面设计’)

删除语句中的子查询

delete from sc
where cno=(select cno from course
where sc.cno=course.cno and cname=’前台页面设计’)

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

您可能感兴趣的文章:SQL Server中的连接查询详解SQL Server 2012 多表连接查询功能实例代码SQL Server使用CROSS APPLY与OUTER APPLY实现连接查询SQL Server连接查询的实用教程使用SQL Server数据库嵌套子查询的方法在SQL Server中使用子查询更新语句SQL Server2019数据库之简单子查询的具有方法SQL Server子查询的深入理解

© 版权声明

相关文章