文章詳情頁
SQL Server數據庫連接查詢和子查詢實戰案例
瀏覽:302日期:2023-05-02 10:03:27
目錄
- 前言
- 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系學生的信息,生成一個新表temp
- 18.將所有的學號和課程號信息生成一個新表SCL
- 19.將選修了“前臺頁面設計”課程的學生成績增加5分
- 20.刪除選修了“前臺頁面設計”課程的選課信息
- 總結
提示: 利用單表簡單查詢和多表高級查詢技能,并且根據查詢要求靈活使用內連接查詢、外連接查詢或子查詢等。同時還利用內連接查詢的兩種格式、三種外連接查詢語法格式和子查詢的語法格式。
前言
內連接查詢(不同表之間查詢)
1.查詢所有學生的學號、姓名、選修課程號和成績
方法一
USE XSCJGOSELECT student.sno,sname,cno,grade from student,scwhere student.sno=sc.sno

方法二
USE XSCJGOSELECT student.sno,sname,cno,grade from student join sc on student.sno=sc.sno

2.查詢選修了課程名稱為“數據庫原理與應用”的學生的學號和姓名
方法一
USE XSCJselect student.sno,sname from student,sc,coursewhere 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.cnowhere cname="數據庫原理與應用"

3.使用別名實現查詢所有學生的學號、姓名、選修課程號和成績
select x.sno,sname,cno,gradefrom student x,sc ywhere x.sno=y.sno

自身連接查詢
4.查詢所有年齡比張文寶大的學生的姓名、性別和年齡
select A.sname,A.ssex,A.sagefrom student A,student Bwhere B.sname="張文寶" and A.sage>B.sage

使用第二種格式實現內連接查詢(JOIN ON)
5.用格式二實現查詢所有學生的學號、姓名、選修課程號和成績
SELECT student.sno,sname,cno,gradefrom student join scon student.sno=sc.sno

外連接(左外連接)
6.查詢所有學生的學號、姓名及對應選課的信息,如果該學生沒有選課,也需要顯示該生的學號和姓名
SELECT student.sno,sname,cno,gradefrom student left outer join scon student.sno=sc.sno

右外連接
7.查詢選課學生的基本信息(若實際上有外鍵約束,這種情況是不存在的)
select sc.sno,sname,cno,gradefrom sc right outer join studenton student.sno=sc.sno

8.采用右外連接查詢學生的學號、選修的課程號、課程名及學分,同時也列出無學生選修的課程信息
select sc.sno,course.cno,cname,creditfrom sc right outer join courseon 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"unionselect sdept from student where sage="20"

11.從student表中查詢年齡為‘19’和‘20’的學生的系部,包括重復行
select sdept from student where sage="19"union allselect sdept from student where sage="20"

使用IN或NOT IN 的子查詢
12.查詢所有選修課程的學生的學號和姓名
select sno,snamefrom studentwhere sno in(select sno from sc)

改為連接查詢實現
select distinct student.sno,snamefrom student join scon student.sno=sc.sno

使用比較運算符的子查詢
13.查詢年齡高于平均年齡的學生的學號、姓名和年齡
select sno,sname,sagefrom student where sage>(select AVG(sage) from student)

使用ANY或ALL的子查詢
14.查詢比CS系的任一學生年齡都大的學生姓名和年齡
select sname,sagefrom studentwhere sage>any (select sage from student where sdept="CS") AND sdept!="CS"select * from student

使用EXISTS的子查詢
15.查詢已有學生選修的課程信息
select *from coursewhere exists(select * from sc where course.cno=sc.cno)

16.查詢尚沒有學生選修的課程信息
select *from coursewhere not exists(select * from sc where course.cno=sc.cno)

查看course表

抽取數據到另一個表
17.查詢CS系學生的信息,生成一個新表temp
select *into tempfrom student where sdept="CS"select * from temp

INSERT語句中的子查詢
18.將所有的學號和課程號信息生成一個新表SCL
INSERT INTO SCL(sno,cno)select sno,cnofrom student,course


UPDATE 語句中的子查詢
19.將選修了“前臺頁面設計”課程的學生成績增加5分
UPDATE scset grade=grade+5where 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數據庫連接查詢和子查詢的文章就介紹到這了,更多相關SQLServer連接查詢和子查詢內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!
標簽:
MsSQL
排行榜

網公網安備