sql之20高階查詢
sql之20高階查詢
Advanced Subqueries
--'巢狀子查詢與關聯子查詢的區別'
Nested Subquery Execution
:) The inner query executes first and finds a value.
:) The outer query executes once, using the value from the inner query.
Correlated Subquery Execution
:) Get a candidate row (fetched by the outer query).
:) Execute the inner query using the value of the candidate row.
:) Use the values resulting from the inner query to qualify or disqualify the candidate.
:) Repeat until no candidate row remains.
--'關聯子查詢的特點:The subquery references a column from a table in the parent query.'
--example1: Find all employees who earn more than the average salary in their department.
SQL> select last_name,salary,department_id
2 from employees outer
3 where salary> (select avg(salary)
4 from employees
5 where department_id=outer.department_id);
LAST_NAME SALARY DEPARTMENT_ID
---------- ---------- -------------
King 24000 90
Lorentz 42000 60
Mourgos 5800 50
Zlotkey 10500 80
Abel 11000 80
Hartstein 13000 20
Higgins 12000 110
另外一種解決方案:
SQL> select a.last_name,a.salary,a.department_id,b.salavg
2 from employees a,(select department_id,avg(salary) salavg
3 from employees
4 group by department_id) b
5 where a.department_id=b.department_id
6 and a.salary>b.salavg;
--example2: Display details of those employees who have switched jobs at least twice.
SQL> select e.employee_id,e.last_name,e.job_id
2 from employees e
3 where 2<= (select count(*) from job_history where employee_id=e.employee_id);
EMPLOYEE_ID LAST_NAME JOB_ID
----------- ---------- ----------
101 Kochhar AD_VP
176 Taylor SA_REP
200 Whalen AD_ASST
--example3:Find employees who have at least one person reporting to them.
SQL> select employee_id,last_name,job_id,department_id
2 from employees outer
3 where exists (select 'x'
4 from employees
5 where manager_id=outer.employee_id);
EMPLOYEE_ID LAST_NAME JOB_ID DEPARTMENT_ID
----------- ---------- ---------- -------------
100 King AD_PRES 90
101 Kochhar AD_VP 90
102 De Haan AD_VP 90
103 Hunold IT_PROG 60
124 Mourgos ST_MAN 50
149 Zlotkey SA_MAN 80
201 Hartstein MK_MAN 20
205 Higgins AC_MGR 110
-- 與上面等價的但是卻低效的SQL語句
SELECT employee_id,last_name,job_id,department_id
FROM employees
WHERE employee_id IN (SELECT manager_id
FROM employees
WHERE manager_id IS NOT NULL);
-- 例4 將部門名稱新增到employees表中
ALTER TABLE employees ADD(department_name VARCHAR2(14));
UPDATE employees e
SET department_name =
(SELECT department_name
FROM departments d
WHERE e.department_id = d.department_id);
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/271063/viewspace-909778/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SQL高階查詢SQL
- sql-server高階查詢SQLServer
- SQL語言基礎(高階查詢)SQL
- 高階查詢
- SQL Server 2008 高階查詢優化概念SQLServer優化
- Mongodb高階查詢MongoDB
- ❖ MongoDB 高階查詢MongoDB
- sql之20再看查詢SQL
- mysql多條件過濾查詢之mysq高階查詢MySql
- 資料庫高階查詢之子查詢資料庫
- Oracle與Mysql的高階查詢與難點sqlOracleMySql
- 【SQL查詢】集合查詢之INTERSECTSQL
- mysql高階查詢語句MySql
- MongoDB高階查詢詳細MongoDB
- 學會寫高階查詢
- Hive高階操作-查詢操作Hive
- 「Oracle」Oracle高階查詢介紹Oracle
- LINUX find的高階查詢Linux
- 理解索引(中):MySQL查詢過程和高階查詢索引MySql
- oracle的sql查詢分析函式-高階部分-分析函授over()子句OracleSQL函式
- 15、Oracle中的高階子查詢Oracle
- 查詢 分析硬解析較高的sql,SQL
- 訂單綜合查詢之sqlSQL
- oracle學習筆記(十一) 高階查詢Oracle筆記
- Google高階搜尋技巧之高階語法查詢指令Go
- 【SQL】Oracle查詢轉換之 OR用法SQLOracle
- 十七、Mysql之SQL優化查詢MySql優化
- SQL Server之查詢檢索操作SQLServer
- 【SQL】Oracle查詢轉換之物化檢視查詢重寫SQLOracle
- Flowable 6.6.0 BPMN使用者指南 - 17 高階用例 - 17.7 高階查詢API:執行時和歷史任務查詢之間的無縫切換API
- PostgreSQL:高階 SQLSQL
- 剖析SQL Server 2005查詢通知之基礎篇SQLServer
- 《MSSQL2008高階教程》之四“SQL優化”SQL優化
- mongoDB高階查詢這一篇就夠了MongoDB
- SQL查詢的:子查詢和多表查詢SQL
- ORACLE_OCP之SQL_子查詢OracleSQL
- MySQL之SQL邏輯查詢順序MySql
- Sql 2012中利用擴充套件事件(Xevents)捕捉高消耗SQL查詢SQL套件事件