SQL Server基礎SQL腳本以內外鏈接、交叉鏈接;函數、子查詢



use AdventureWorks --切換到AdventureWorks數據庫

--建立Student表和Marks表,用於操做各類聯接

create table Student  --建立學生表,裏面包含兩列,學號和姓名
(
   RollNo char(4),
   Name varchar(20)
)

insert into Student values --向Student表中插入5行記錄
('S001','Allen'),
   ('S002','Jhon'),
   ('S003','David'),
   ('S004','Stefen'),
   ('S005','Steve')

create table Marks    --建立成績表,裏面包含三列,學號,RDMBS和Math
(
   RollNo char(4),
   RDBMS int,
   Math int
)

insert into Marks values --向成績表中插入三行記錄
('S001',98,76),
('S002',67,64),
('S003',76,96)

select * from Student
select * from Marks

--1. 內聯接 INNTER JOIN- 顯示知足公共列中聯接條件的行 inner可加可不加

--問題:查詢有考試成績的學生的學號,姓名,RDBMS成績和Math成績


-----練習:已知
select * from HumanResources.Employee
select * from HumanResources.EmployeeAddress
go
--顯示:EmployeeID, Title, AddressID  的匹配信息  ----inner join


--給表名一個別名
--2. 外聯接 - 顯示包含一個表中的全部行以及另一個表中匹配行的結果集,不匹配的用NULL值填充

--(1)左外聯接 - 返回LEFT OUTER JOIN 左側的表的全部行,以及右側指定的表的匹配行,若右邊找不到匹配項,顯示NULL值
--(2)右外聯接 - 返回RIGHT OUTER JOIN 右側的表的全部行,以及左側指定的表的匹配行,若左邊找不到匹配項,顯示NULL值
--(3)完整外聯接 -  左外聯接和右外聯接的組合,返回兩個表中全部匹配的行和不匹配的行,匹配記錄只顯示一次

--3. 交叉聯接(Cross Join) Product運算,將一個表中的每一行與另外一個表中的
--------------------
   create table Course --建立Course表,裏面包含一列CourseName
(CourseName varchar(10))
insert into Course values --向Course表中插入兩行記錄
('English'),
   ('C Language')
select * from Student
select * from Course
--要求顯示結果爲每一個學生都修一遍Course表中的全部課程


--4. 等值聯接 --使用=號聯接表的內聯接
--練習:查詢員工的員工編號,所屬部門名稱和工資 聯接多個表
select * from HumanResources.Employee
select * from HumanResources.EmployeeDepartmentHistory
select * from HumanResources.Department



--5. 自聯接 - 同一個表當成兩張表使用,一個表中的一行聯接另外一個表中的一行

select * from HumanResources.Employee

select a.EmployeeID,a.Title,a.ManagerID,b.Title from
--查詢員工的編號,職位,其主管的員工編號和其主管的職位
HumanResources.Employee a join HumanResources.Employee b on a.ManagerID=b.EmployeeID
--根據其主管的員工編號找到對應的職位

select a.EmployeeID,a.Title,a.ManagerID,b.Title from
--查詢員工的編號,職位,其主管的員工編號和其主管的職位
HumanResources.Employee a , HumanResources.Employee b where a.ManagerID=b.EmployeeID --根據其主管的員工編號找到對應的職位


---------------------- (二)、使用子查詢查詢數據----------------------------

   --子查詢:將一個select的查詢結果做爲另一個select查詢的輸入/條件,查詢裏面的查詢

--1. 使用比較運算符,IN和EXISTS關鍵字

--比較運算符,=號爲主
select * from HumanResources.Employee

--問題:查詢和員工編號爲1的員工職位(Title)相同的員工的信息



--IN 多個值

--問題:查詢和員工編號爲1,3,4的員工的職位相同的員工的信息


--EXISTS關鍵字-檢查一組記錄是否存在,返回True或False
--if exists(select * from databases where name='UDB') drop database UDB


------------------
   select * from HumanResources.Employee
select * from HumanResources.EmployeeDepartmentHistory

--2. 使用修改過的比較運算符 ALL,ANY

--問題:查詢
--查詢RDBMS成績高於S002或者高於S003的學生的信息
select * from Marks
go

--查詢RDBMS成績高於S002而且高於S003的學生的信息



--3. 使用聚合函數

--問題:查詢RDBMS成績最高的學生的學號和RDBMS成績


--4. 使用嵌套子查詢 --子查詢裏面能夠包含一個或多個子查詢,這樣叫作嵌套子查詢

--問題:查詢工資最高的員工的編號 HumanResources.EmployeePayHistory
select * from HumanResources.EmployeePayHistory


--問題:查詢工資最高的員工所在的部門編號
select * from HumanResources.EmployeeDepartmentHistory


--5. 使用關聯子查詢 -  根據外部查詢做爲評估依據的查詢
--問題:查詢每一個部門最先加入的員工的信息
select * from HumanResources.EmployeeDepartmentHistory  a
where StartDate=
   (
       select min(StartDate) from HumanResources.EmployeeDepartmentHistory
where DepartmentID=a.DepartmentID
)

--6. APPLY運算符 --合併兩個查詢的結果集,

   ---------------------------------------------

       create table Depositor --建立Depositor表,存儲儲蓄用戶信息,表中有兩列,客戶姓名和儲蓄帳戶
(
   "客戶姓名" varchar(20),
   "儲蓄帳戶" char(3)
)

insert into Depositor values  --向Depositor表中插入兩條記錄
('Allen','D01'),
   ('David','D02')

create table Borrower --建立Borrower表,存儲貸款用戶信息,表中有兩列,客戶姓名和貸款帳戶
(
   "客戶姓名" varchar(20),
   "貸款帳戶" char(3)
)

insert into Borrower values --向Borrower表中插入兩行記錄
('Amy','B11'),
   ('David','B12')
--------------------------------------
   select * from Depositor
select * from Borrower

--CROSS APPLY - 返回外部結果集中與內部結果集匹配的行

select a.客戶姓名,a.儲蓄帳戶,br.貸款帳戶 from Depositor a  --外部結果集
cross apply
(select * from Borrower b where b.客戶姓名=a.客戶姓名) br  --br爲內部結果集的別名

--OUTER APPLY - 返回外部結果集中全部的行,即便內部結果集中沒有找到此行

select a.客戶姓名,a.儲蓄帳戶,br.貸款帳戶 from Depositor a  --外部結果集
outer apply
(select * from Borrower b where b.客戶姓名=a.客戶姓名) br  --br爲內部結果集的別名數據庫

相關文章
相關標籤/搜索