最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SQL?Server數(shù)據(jù)庫(kù)連接查詢(xún)和子查詢(xún)實(shí)戰(zhàn)案例

 更新時(shí)間:2023年04月15日 11:31:52   作者:長(zhǎng)月.  
子查詢(xún)(嵌套查詢(xún))子查詢(xún)也稱(chēng)嵌套查詢(xún),是指一個(gè)SELECT查詢(xún)語(yǔ)句可以嵌入另一個(gè)SELECT查詢(xún)語(yǔ)句之中,下面這篇文章主要給大家介紹了關(guān)于SQL?Server數(shù)據(jù)庫(kù)連接查詢(xún)和子查詢(xún)的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下

提示: 利用單表簡(jiǎn)單查詢(xún)和多表高級(jí)查詢(xún)技能,并且根據(jù)查詢(xún)要求靈活使用內(nèi)連接查詢(xún)、外連接查詢(xún)或子查詢(xún)等。同時(shí)還利用內(nèi)連接查詢(xún)的兩種格式、三種外連接查詢(xún)語(yǔ)法格式和子查詢(xún)的語(yǔ)法格式。

前言

內(nèi)連接查詢(xún)(不同表之間查詢(xún))

1.查詢(xún)所有學(xué)生的學(xué)號(hào)、姓名、選修課程號(hào)和成績(jī)

方法一

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.查詢(xún)選修了課程名稱(chēng)為“數(shù)據(jù)庫(kù)原理與應(yīng)用”的學(xué)生的學(xué)號(hào)和姓名

方法一

USE XSCJ
select student.sno,sname from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno and cname='數(shù)據(jù)庫(kù)原理與應(yīng)用'

方法二

select student.sno,sname from student join sc 
on student.sno=sc.sno join course on sc.cno=course.cno
where cname='數(shù)據(jù)庫(kù)原理與應(yīng)用'

3.使用別名實(shí)現(xiàn)查詢(xún)所有學(xué)生的學(xué)號(hào)、姓名、選修課程號(hào)和成績(jī)

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

自身連接查詢(xún)

4.查詢(xún)所有年齡比張文寶大的學(xué)生的姓名、性別和年齡

select A.sname,A.ssex,A.sage
from student A,student B
where B.sname='張文寶' and A.sage>B.sage

使用第二種格式實(shí)現(xiàn)內(nèi)連接查詢(xún)(JOIN ON)

5.用格式二實(shí)現(xiàn)查詢(xún)所有學(xué)生的學(xué)號(hào)、姓名、選修課程號(hào)和成績(jī)

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

外連接(左外連接)

6.查詢(xún)所有學(xué)生的學(xué)號(hào)、姓名及對(duì)應(yīng)選課的信息,如果該學(xué)生沒(méi)有選課,也需要顯示該生的學(xué)號(hào)和姓名

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

右外連接

7.查詢(xún)選課學(xué)生的基本信息(若實(shí)際上有外鍵約束,這種情況是不存在的)

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

8.采用右外連接查詢(xún)學(xué)生的學(xué)號(hào)、選修的課程號(hào)、課程名及學(xué)分,同時(shí)也列出無(wú)學(xué)生選修的課程信息

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

全外連接

9.student和sc表實(shí)現(xiàn)全外連接

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

UNION聯(lián)合查詢(xún)

10.從student表中查詢(xún)年齡為‘19’和‘20’的學(xué)生的系部,不包括重復(fù)行

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

11.從student表中查詢(xún)年齡為‘19’和‘20’的學(xué)生的系部,包括重復(fù)行

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

使用IN或NOT IN 的子查詢(xún)

12.查詢(xún)所有選修課程的學(xué)生的學(xué)號(hào)和姓名

select sno,sname
from student
where sno in
(select sno from sc)

改為連接查詢(xún)實(shí)現(xiàn)

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

使用比較運(yùn)算符的子查詢(xún)

13.查詢(xún)年齡高于平均年齡的學(xué)生的學(xué)號(hào)、姓名和年齡

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

使用ANY或ALL的子查詢(xún)

14.查詢(xún)比CS系的任一學(xué)生年齡都大的學(xué)生姓名和年齡

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

使用EXISTS的子查詢(xún)

15.查詢(xún)已有學(xué)生選修的課程信息

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

16.查詢(xún)尚沒(méi)有學(xué)生選修的課程信息

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

查看course表

抽取數(shù)據(jù)到另一個(gè)表

17.查詢(xún)CS系學(xué)生的信息,生成一個(gè)新表temp

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

INSERT語(yǔ)句中的子查詢(xún)

18.將所有的學(xué)號(hào)和課程號(hào)信息生成一個(gè)新表SCL

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

UPDATE 語(yǔ)句中的子查詢(xún)

19.將選修了“前臺(tái)頁(yè)面設(shè)計(jì)”課程的學(xué)生成績(jī)?cè)黾?分

UPDATE sc
set grade=grade+5
where cno=
(select cno from course
 where sc.cno=course.cno and cname='前臺(tái)頁(yè)面設(shè)計(jì)')

刪除語(yǔ)句中的子查詢(xún)

20.刪除選修了“前臺(tái)頁(yè)面設(shè)計(jì)”課程的選課信息

delete from sc
 where cno=
 (select cno from course
 where sc.cno=course.cno and cname='前臺(tái)頁(yè)面設(shè)計(jì)')

總結(jié)

到此這篇關(guān)于SQL Server數(shù)據(jù)庫(kù)連接查詢(xún)和子查詢(xún)的文章就介紹到這了,更多相關(guān)SQLServer連接查詢(xún)和子查詢(xún)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

新乡市| 七台河市| 廊坊市| 镇安县| 龙游县| 长宁县| 涿鹿县| 林西县| 沭阳县| 翁源县| 汉阴县| 定西市| 阿拉善盟| 辽宁省| 成安县| 博野县| 瑞昌市| 贵阳市| 上思县| 昌乐县| 龙岩市| 靖安县| 新野县| 磐石市| 正宁县| 黔江区| 建昌县| 富平县| 新宁县| 静乐县| 班戈县| 和顺县| 荥经县| 阿城市| 南澳县| 河东区| 隆回县| 隆林| 驻马店市| 陈巴尔虎旗| 南澳县|