- use db_sqlserver2
- select 姓名, 工資, 面積, 金額, (工資+金額/1000) as 實發工資 from 職工,倉庫, 訂購單
- where 職工.職工號=訂購單.職工號 and 職工.倉庫號=倉庫.倉庫號

2:
- select 姓名,工資,金額 from 職工,訂購單 where 姓名 like '%平%' and 職工.職工號 = 訂購單.職工號 order by 工資 desc
3:
- select 姓名,工資,金額 from 職工,訂購單 where 姓名 like '%平%' and 職工.職工號 = 訂購單.職工號 order by 工資 desc, 金額 desc

4:
- select 姓名, 工資, 城市, (select AVG(工資) from 職工) as 平均工資 , (工資-(select AVG(工資) from 職工)) as 與平均工資之差
- from 職工, 倉庫 where 倉庫.倉庫號=職工.倉庫號

5:帶保存功能的多表連接查詢
在SQL語句中,利用into語句可以把查詢到的結果保存成一張新表,然后再對新表進行數據信息查詢。
- select 倉庫.倉庫號, 城市, 面積, 姓名, 工資, 金額 into 多表連接產生的新表 from 倉庫, 職工, 訂購單
- where 倉庫.倉庫號=職工.倉庫號 and 職工.職工號=訂購單.職工號
- select * from 多表連接產生的新表

//查看不同倉庫中的所有職工的倉庫號、平均銷售金額、最大銷售金額、最小銷售金額、最大銷售金額與最小銷售金額之差的信息
- select 倉庫號, AVG(金額) as 平均銷售金額, MAX(金額) as 最大銷售金額, MIN(金額) as 最小銷售金額,
- (MAX(金額) - MIN(金額)) as 最大金額與最小金額之差 from 多表連接產生的新表 group by 倉庫號;

可以把分組查詢結果再生成一張新表
- select 倉庫號, AVG(金額) as 平均銷售金額, MAX(金額) as 最大銷售金額, MIN(金額) as 最小銷售金額,
- (MAX(金額) - MIN(金額)) as 最大金額與最小金額之差 into 分組查詢產生的新表
- from 多表連接產生的新表 group by 倉庫號;
- select * from 分組查詢產生的新表
6: 內連接查詢(inner join)
使用比較運算符對表之間的某些數據進行比較,並列出這些表中與連接條件相匹配的數據行。
- select 姓名, 城市 from 倉庫 inner join 職工 on 職工.倉庫號=倉庫.倉庫號
多表的內連接查詢
- select 城市,面積, 姓名, 工資, 金額 from 倉庫
- inner join 職工 on 職工.倉庫號=倉庫.倉庫號
- inner join 訂購單 on 職工.職工號=訂購單.職工號
- and 工資>1800 and 面積<1000 and 金額 != 16600
7:左連接查詢(left join)
除滿足連接條件的記錄顯示外,第一張表中不滿足條件的記錄也顯示在結果集中。
- select 姓名, 城市 from 倉庫
- left join 職工 on 職工.倉庫號=倉庫.倉庫號 and 城市 is not null and 姓名 like '%王%'

- select 城市, 面積, 姓名, 工資, 金額 from 倉庫
- left join 職工 on 職工.倉庫號 = 倉庫.倉庫號
- left join 訂購單 on 職工.職工號=訂購單.職工號
- and 工資>1800 and 面積<1000 and 金額!=16600

在第一個left join左連接中,第一張表是倉庫表,第二張表是職工表,在第二個left join左連接中,第一張表是職工表,第二張表是訂購單表
8:右連接查詢
除滿足連接條件的記錄顯示外,第二張表中不滿足條件的記錄也顯示在查詢結果集中
- select 姓名, 城市 from 倉庫
- right join 職工 on 職工.倉庫號=倉庫.倉庫號 where 城市 is not null and 姓名 like '%王%'

- select 城市, 面積, 姓名, 工資, 金額 from 倉庫
- right join 職工 on 職工.倉庫號=倉庫.倉庫號
- right join 訂購單 on 職工.職工號=訂購單.職工號
- and 工資>1500 and 面積<1000 and 金額!=16600

- select 城市, 面積, 姓名, 工資, 金額 from 倉庫
- right join 職工 on 職工.倉庫號=倉庫.倉庫號
- right join 訂購單 on 職工.職工號=訂購單.職工號
- <span style="color:#ff0000;">where</span> 工資>1500 and 面積<1000 and 金額!=16600
把and關鍵字換為where關鍵字后的效果圖,會發現那些無用的數據沒有了

9:全連接查詢
除滿足連接條件的記錄顯示外,兩張表中的不能滿足條件的記錄也顯示在查詢結果集中
- select 姓名,城市 from 倉庫 full join 職工 on 職工.倉庫號=倉庫.倉庫號 and 城市 is not null and
- 姓名 like '%王%';

集合的交、並、差運算查詢
為了進行並、交、差運算,要求運算的兩個查詢結果具有相同的字段個數,並且對應的字段的值要出自同一個值域,即具有相同的數據類型和取值范圍
10:並運算(union)
主要將兩個或者更多個查詢的結果組合為單個結果集,該結果集包含聯合查詢中的全部查詢的全部行
- select 倉庫號 from 倉庫 where 城市='北京'
- union
- select 倉庫號 from 職工 where 工資>2000
- select 倉庫號 from 倉庫 where 城市='北京'
- union
- select 倉庫號 from 職工 where 工資>2000
- select distinct 倉庫.倉庫號 from 倉庫, 職工 where 倉庫.倉庫號=職工.倉庫號 and (城市='北京' or 工資>2000)
使用union all 保留重復行
- select 倉庫號 from 倉庫 where 城市='北京'
- union all
- select 倉庫號 from 職工 where 工資>2000

11:交運算(intersect)
可以將兩個select語句的查詢結果通過交運算合並成一個查詢結果
- select 倉庫號 from 倉庫 where 城市='北京'
- intersect
- select 倉庫號 from 職工 where 工資>2000
- select distinct 倉庫.倉庫號 from 倉庫, 職工 where 城市='北京' and 倉庫.倉庫號=職工.倉庫號 and 工資>2000

12:差運算(except)
可以計算兩個select查詢結果之間的數據差,即返回在一個查詢結果中存在,但在另一個查詢結果中不存在的所有行。
- select 倉庫號 from 倉庫 where 城市='北京'
- except
- select 倉庫號 from 職工 where 工資>2900
- select 倉庫號 from 倉庫 where 城市='北京' and 倉庫號 not in(select 倉庫號 from 職工 where 工資>2900)

