草庐IT

SQL Server 连接查询和子查询

长月. 2024-03-08 原文

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

文章目录


前言

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

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='前台页面设计')


总结

今天的学习内容就分享到这里啦,如果对友友们有帮助的话,记得点赞收藏博客,关注后续的SQL Server内容哦~👻👻👻

有关SQL Server 连接查询和子查询的更多相关文章

  1. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  2. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  3. ruby-on-rails - 在 Rails 和 ActiveRecord 中查询时忽略某些字段 - 2

    我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr

  4. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  5. sql - 查询忽略时间戳日期的时间范围 - 2

    我正在尝试查询我的Rails数据库(Postgres)中的购买表,我想查询时间范围。例如,我想知道在所有日期的下午2点到3点之间进行了多少次购买。此表中有一个created_at列,但我不知道如何在不搜索特定日期的情况下完成此操作。我试过:Purchases.where("created_atBETWEEN?and?",Time.now-1.hour,Time.now)但这最终只会搜索今天与那些时间的日期。 最佳答案 您需要使用PostgreSQL'sdate_part/extractfunction从created_at中提取小时

  6. ruby - 我的 Ruby IRC 机器人没有连接到 IRC 服务器。我究竟做错了什么? - 2

    require"socket"server="irc.rizon.net"port="6667"nick="RubyIRCBot"channel="#0x40"s=TCPSocket.open(server,port)s.print("USERTesting",0)s.print("NICK#{nick}",0)s.print("JOIN#{channel}",0)这个IRC机器人没有连接到IRC服务器,我做错了什么? 最佳答案 失败并显示此消息::irc.shakeababy.net461*USER:Notenoughparame

  7. ruby-on-rails - 连接字符串时如何在 <%=%> block 内输出 html_safe? - 2

    考虑一下:现在这些情况:#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2#output:http://domain.com/?foo=1&bar=2我需要用其他字符串输出URL。我如何保证&符号不会被转义?由于我无法控制的原因,我无法发送&。求助!把我的头发拉到这里:\编辑:为了澄清,我实际上有一个像这样的数组:@images=[{:id=>"fooid",:url=>"http://

  8. ruby - Faye WebSocket,关闭处理程序被触发后重新连接到套接字 - 2

    我有一个super简单的脚本,它几乎包含了FayeWebSocketGitHub页面上用于处理关闭连接的内容:ws=Faye::WebSocket::Client.new(url,nil,:headers=>headers)ws.on:opendo|event|p[:open]#sendpingcommand#sendtestcommand#ws.send({command:'test'}.to_json)endws.on:messagedo|event|#hereistheentrypointfordatacomingfromtheserver.pJSON.parse(event.d

  9. ruby-on-rails - solr 清理查询 - 2

    我在Rails上使用带有ruby​​的solr。一切正常,我只需要知道是否有任何现有代码来清理用户输入,比如以?开头的查询。或* 最佳答案 我不知道执行此操作的任何代码,但理论上可以通过查看parsingcodeinLucene来完成并搜索thrownewParseException(只有16个匹配!)。在实践中,我认为您最好只捕获代码中的任何solr异常并显示“无效查询”消息或类似信息。编辑:这里有几个“sanitizer”:http://pivotallabs.com/users/zach/blog/articles/937-s

  10. ruby-on-rails - Rails 3 在一个查询中包含多个表 - 2

    我正在为锦标赛开发一个Rails应用程序。我在这个查询中使用了三个模型:classPlayertruehas_and_belongs_to_many:tournamentsclassTournament:destroyclassPlayerMatch"Player",:foreign_key=>"player_one"belongs_to:player_two,:class_name=>"Player",:foreign_key=>"player_two"在tournaments_controller的显示操作中,我调用以下查询:Tournament.where(:id=>params

随机推荐