SQL?Server數據庫連接查詢和子查詢實戰(zhàn)案例
提示: 利用單表簡單查詢和多表高級查詢技能,并且根據查詢要求靈活使用內連接查詢、外連接查詢或子查詢等。同時還利用內連接查詢的兩種格式、三種外連接查詢語法格式和子查詢的語法格式。
前言
內連接查詢(不同表之間查詢)
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.使用別名實現(xiàn)查詢所有學生的學號、姓名、選修課程號和成績
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
使用第二種格式實現(xiàn)內連接查詢(JOIN ON)
5.用格式二實現(xiàn)查詢所有學生的學號、姓名、選修課程號和成績
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表實現(xiàn)全外連接
select * from sc full outer join student on student.sno=sc.sno
UNION聯(lián)合查詢
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)
改為連接查詢實現(xiàn)
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數據庫連接查詢和子查詢的文章就介紹到這了,更多相關SQLServer連接查詢和子查詢內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SQL Server復制刪除發(fā)布時遇到錯誤18752的問題及解決方法
朋友反饋他無法刪除一臺SQL Server數據庫上的發(fā)布,具體情況為刪除一個SQL Server Replication的發(fā)布時,遇到這樣的錯誤問題如何解決呢,下面小編給大家分享SQL Server復制刪除發(fā)布時遇到錯誤18752的問題及解決方法,感興趣的朋友一起看看吧2024-01-01SQLSERVER編譯與重編譯發(fā)生場景及重用的利弊介紹
本文將介紹編譯的含義;執(zhí)行計劃重用的利弊以及重編譯的發(fā)生場景等等,為您學習SQLSERVER編譯與重編譯打下很好的基礎,感興趣的朋友可以了解下2013-01-01SQLServer觸發(fā)器創(chuàng)建、刪除、修改、查看示例代碼
觸發(fā)器是一種特殊的存儲過程﹐它不能被顯式地調用﹐而是在往表中插入記錄﹑更新記錄或者刪除記錄時被自動地激活。所以觸發(fā)器可以用來實現(xiàn)對表實施復雜的完整性約束。2010-06-06SQLServer2019 數據庫環(huán)境搭建與使用的實現(xiàn)
這篇文章主要介紹了SQLServer2019 數據庫環(huán)境搭建與使用的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04