Oracle子查詢之高級子查詢

来源:https://www.cnblogs.com/Clonglegs/archive/2018/08/15/9479955.html
-Advertisement-
Play Games

Oracle 高級子查詢 高級子查詢相對於簡單子查詢來說,返回的數據行不再是一列,而是多列數據。 1,多列子查詢 主查詢與子查詢返回的多個列進行比較 查詢與141號或174號員工的manager_id和department_id相同的其他員工的employee_id, manager_id, dep ...


Oracle 高級子查詢

  高級子查詢相對於簡單子查詢來說,返回的數據行不再是一列,而是多列數據。

1,多列子查詢

主查詢與子查詢返回的多個列進行比較

查詢與141號或174號員工的manager_id和department_id相同的其他員工的employee_id, manager_id, department_id

select employee_id, manager_id, department_id

from employees

where (manager_id,department_id) in ( --由於內查詢返回兩列,所以此處也必須使用兩列來進行對應,並且列的順序一致

select manager_id,department_id --內查詢返回2列

from employees

where employee_id in (141,174)

)

and employee_id not in (141,174);

 

2,FROM子句中使用子查詢

將子查詢的結果集,作為一個虛表,然後從這個虛表中查詢數據行

 

返回比本部門平均工資高的員工的last_name, department_id, salary及平均工資

select last_name, e.department_id, salary,avgsal

from employees e,(select department_id,round(avg(salary)) avgsal

from employees

group by department_id) avg_sal

where e.department_id = avg_sal.department_id

and e.salary > avg_sal.avgsal

order by e.department_id;

 

3,單列子查詢的其他應用

在主查詢的select列表中使用

 

問題:顯式員工的employee_id,last_name和location。其中,若員工department_id與location_id為1800的department_id相同,

則location為’Canada’,其餘則為’USA’。

 

select employee_id,last_name,department_id,

(case when department_id = (select department_id

from departments

where location_id = 1800

)

then 'USA'

else 'Canada' end) location

from employees

order by department_id;

select employee_id,last_name,department_id,

(case department_id when (select department_id

from departments

where location_id = 1800

)

then 'USA'

else 'Canada' end) location

from employees

order by department_id;

select employee_id,last_name,department_id,

decode(department_id,(select department_id

from departments

where location_id = 1800

),'USA',

'Canada') location

from employees

order by department_id;

 

4,相關子查詢,

子查詢語句中使用了主查詢語句中的表中的數據(這個數據不一定在主查詢的select語句中)

相關子查詢按照一行接一行的順序執行,即子查詢取主查詢表中的每一行數據值,主查詢的每一行都執行一次子查詢。

使用相關子查詢,要考慮是否必須使用、是否合理,否則在不需要使用相關子查詢的情況下就能輕易得到查詢結果,使用相關子查詢反而會使查詢的效率下降。

 

用法:

get:從主查詢所用的表中獲取候選列

execute:子查詢使用主查詢的數據,併進行相關的篩選

use:如果滿足內查詢的條件則返回值

 

例1:查詢員工的employee_id,last_name,要求按照員工的department_name排序

select employee_id,last_name,e.department_id,department_name

from employees e,departments dd

where e.department_id = dd.department_id(+) --該查詢返回employees表中的所有行,沒返回一行,都會取一個employee_id到子查詢中

order by (select department_name

from departments d

where e.department_id = d.department_id); --子查詢根據主查詢返回的employee_id,在departments表中進行查找,有則返回數據。

desc;

 

例2:查詢員工中工資大於本部門平均工資的員工的last_name,salary,department_id和本部門的平均工資

select last_name,salary,e1.department_id,ss.avgsal

from employees e1,(select department_id,round(avg(salary)) avgsal

from employees

group by department_id) ss --該from字句同前例

where e1.department_id = ss.department_id

and salary > (

select round(avg(salary))

from employees e2

where e1.department_id = e2.department_id --將外查詢中的department_id,查找該處的department_id,並將所有的返回結果分組

group by e2.department_id)

order by e1.department_id;

例3:若employees表中employee_id與job_history表中employee_id相同且job_history表中employee_id的數目不小於2,

輸出employees中這些相同id的員工的employee_id,last_name和其job_id

select e1.employee_id ,last_name,e1.job_id

from employees e1

where (select count(job_id)

from job_history j1

where e1.employee_id = j1.employee_id) >= 2

 

5,exists

EXISTS 操作符檢查在子查詢中是否存在滿足條件的行

如果在子查詢中存在滿足條件的行:

不在子查詢中繼續查找

條件返回 TRUE

如果在子查詢中不存在滿足條件的行:

條件返回 FALSE

繼續在子查詢中查找

 

查詢公司管理者的employee_id,last_name,job_id,department_id信息

子查詢

select employee_id,last_name,job_id,department_id

from employees e1

where e1.employee_id in (

select distinct(manager_id)

from employees);

自連接

select distinct e1.employee_id,e1.last_name,e1.job_id,e1.department_id

from employees e1,employees e2

where e1.employee_id = e2.manager_id;

相關子查詢

select employee_id,last_name,job_id,department_id

from employees e1

where e1.employee_id in (

select manager_id

from employees e2

where e1.employee_id = e2.manager_id);

使用exists相關子查詢

select employee_id,last_name,job_id,department_id

from employees e1

where exists (

select 'A' --根據e1表中的每個 employee_id在e2中查找 manager_id,如果找到,子查詢返回true,主查詢返回該數據行

from employees e2

where e1.employee_id = e2.manager_id);

 

6.not exists

查詢departments表中,不存在於employees表中的部門的department_id和department_name

select d.department_id,d.department_name

from departments d

where not exists ( --not exists 返回子查詢中false的結果

select 'X' --根據e1表中的每個 employee_id在e2中查找 manager_id,如果找到,子查詢返回true,即該部門在departments中存在,也有員工

from employees e

where d.department_id = e.department_id);

 

7.相關更新

使用相關子查詢依據一個表中的數據更新另一個表的數據

例:向employees中添加一列 department_name ,並更具department_id填充

update employees e1

set department_name = (

select department_name

from departments

where e1.department_id = department_id);

 

8,相關刪除

使用相關子查詢依據一個表中的數據刪除另一個表的數據

例:刪除表employees中,其與emp_history表皆有的數據

delete from employees e1

where employee_id in (

select employee_id

from job_history

where e1.employee_id = employee_id);

 

9,使用 WITH 子句

可以避免在 SELECT 語句中重覆書寫相同的語句塊

WITH 子句將該子句中的語句塊執行一次並存儲到用戶的臨時表空間中

使用 WITH 子句可以提高查詢效率

 

例,查詢公司中各部門的總工資,大於公司中各部門的平均總工資,的部門信息

使用普通方法:使用普通方法,彙總後的語句冗長不易理解

--各部門總工資

select department_id,sum(salary) sum_sal from employees group by department_id;

--各部門的平均總工資

select sum(to_sal.sum_sal)/count(*) to_avg_sal

from (select department_id,sum(salary) sum_sal

   from employees group by department_id) to_sal;

--根據前面兩項,查詢最終結果 

select to_sal.department_id,sum_sal

from (select department_id,sum(salary) sum_sal from employees group by department_id) to_sal,

   (select sum(to_sal.sum_sal)/count(*) to_avg_sal from (select department_id,sum(salary) sum_sal from employees group by department_id) to_sal) avg_sal

where to_sal.sum_sal > avg_sal.to_avg_sal;

使用with字句

with to_sal as --各部門總工資

(select department_id,sum(salary) sum_sal from employees group by department_id),

avg_sal as --各部門的平均總工資

(select sum(to_sal.sum_sal)/count(*) to_avg_sal from to_sal)

select department_id,sum_sal

from to_sal

where to_sal.sum_sal > (

select avg_sal.to_avg_sal

from avg_sal);

 

with

to_sal as

(select department_id,sum(salary) sum_sal from employees group by department_id),

avg_sal as

(select sum(to_sal.sum_sal)/count(*) to_avg_sal from to_sal)

select department_id,sum_sal,to_avg_sal

from to_sal,avg_sal

where to_sal.sum_sal > avg_sal.to_avg_sal;

 

 

 

 註明:本博文系學習尚矽谷網易雲課堂教學課程總結輸出。

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本節介紹PL SQL的基本內容 本節所舉示例數據來源oracle用戶scott下的emp表和dept表,數據如下: 一、plsql簡介: 1、概念:procedural language,過程化sql語言,是面向過程的語言,在普通sql的基礎上增加了編程語言的特點。PL/SQL的基本單元是塊。 2、 ...
  • Flink+Kafka整合實例 1.使用工具Intellig IDEA新建一個maven項目,為項目命名為kafka01。 2.我的pom.xml文件配置如下。 3.在項目的目錄/src/main/java在創建兩個Java類,分別命名為KafkaDemo和CustomWatermarkEmitte ...
  • 所有萌生入行的想法與想要學習Java的同學的初衷是一樣的。崗位非常火,就業薪資比較高,,前景非常可觀。基本都是這個原因而嚮往大數據,但是對大數據卻不甚瞭解。 如果你想學習,那麼首先你需要學會編程,其次你需要掌握數學,統計學的知識,最後融合應用,就可以想在數據方向發展,籠統來說,就是這樣的。但是僅僅這 ...
  • 1 下載並安裝MySQL官方的 Yum Repository //下載安裝Yum Repository wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm //yum安裝 yum -y ins ...
  • MongoDB 一、下載MongoDB資料庫 1、進入MongoDB官網進行下載,網址:https://www.mongodb.com/。 2、下載完成後可進行安裝,安裝後,並有了MongoDB服務。 二、配置環境變數 1、在環境變數中的系統變數新建一個名為MONGODB_HOME變數名,變數值為M ...
  • 具體方法如下 一:update 表2 set (要插入的列名)= select 表1.某一列 from 表1 left jion 表2 on 表1和表2的關聯 where ..... 二:update 表1 set 表1.列=表2.列 from 表2 where 表2.id=表1.id 三:upda ...
  • Sqlserver類型 Oracle類型 binary RAW(50) bit NUMBER(2) char CHAR(10) datetime DATE decimal NUMBER(18) float BINARY_DOUBLE image BLOB int NUMBER(10) money N ...
  • 資料庫版本:9.3.23(Windows xp系統) 步驟: 1、需要修改資料庫安裝目錄下的pg_hba.conf文件 修改成: 2、並使用psql執行pg_ctl reload重新載入配置文件 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...