




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、6Subqueries 6-2ObjectivesAfter completing this lesson, you should be able to do the following:Describe the types of problem that subqueries can solveDefine subqueriesList the types of subqueriesWrite single-row and multiple-row subqueries6-3Using a Subquery to Solve a ProblemWho has a salary greater
2、 than Abels?Which employees have salaries greater than Abels salary?Main Query:What is Abels salary?Subquery6-4Subquery SyntaxThe subquery (inner query) executes once before the main query.The result of the subquery is used by the main query (outer query).SELECTselect_listFROMtableWHEREexpr operator
3、 (SELECTselect_list FROMtable);6-5SELECT last_nameFROM employeesWHERE salary (SELECT salary FROM employees WHERE last_name = Abel);Using a Subquery110006-6Guidelines for Using SubqueriesEnclose subqueries in parentheses. Place subqueries on the right side of the comparison condition.The ORDER BY cla
4、use in the subquery is not needed unless you are performing top-n analysis.Use single-row operators with single-row subqueries and use multiple-row operators withmultiple-row subqueries.6-7Types of SubqueriesMain querySubqueryreturnsST_CLERK Multiple-row subqueryST_CLERKSA_MANMain querySubqueryretur
5、ns Single-row subquery6-8Single-Row SubqueriesReturn only one rowUse single-row comparison operatorsOperator= = =MeaningEqual toGreater than Greater than or equal to Less than Less than or equal toNot equal to6-9 SELECT last_name, job_id, salaryFROM employeesWHERE job_id = (SELECT job_id FROM employ
6、ees WHERE employee_id = 141)AND salary (SELECT salary FROM employees WHERE employee_id = 143);Executing Single-Row SubqueriesST_CLERK26006-10SELECT last_name, job_id, salaryFROM employeesWHERE salary = (SELECT MIN(salary) FROM employees);Using Group Functions in a Subquery25006-11The HAVING Clause w
7、ith SubqueriesThe Oracle server executes subqueries first.The Oracle server returns results into the HAVING clause of the main query.SELECT department_id, MIN(salary)FROM employeesGROUP BY department_idHAVING MIN(salary) (SELECT MIN(salary) FROM employees WHERE department_id = 50);25006-12SELECT emp
8、loyee_id, last_nameFROM employeesWHERE salary = (SELECT MIN(salary) FROM employees GROUP BY department_id);What Is Wrong With This Statement?ERROR at line 4:ORA-01427: single-row subquery returns more thanone rowSingle-row operator with multiple-row subquery6-13Will This Statement Return Rows?no row
9、s selectedSELECT last_name, job_idFROM employeesWHERE job_id = (SELECT job_id FROM employees WHERE last_name = No);Subquery returns no values6-14Multiple-Row SubqueriesReturn more than one rowUse multiple-row comparison operatorsOperator IN ANY ALLMeaningEqual to any member in the listCompare value
10、to each value returned by the subquery Compare value to every value returned by the subquery 6-15Using the ANY Operator in Multiple-Row Subqueries9000, 6000, 4200SELECT employee_id, last_name, job_id, salaryFROM employeesWHERE salary ANY (SELECT salary FROM employees WHERE job_id = IT_PROG)AND job_i
11、d IT_PROG;6-16SELECT employee_id, last_name, job_id, salaryFROM employeesWHERE salary ALL (SELECT salary FROM employees WHERE job_id = IT_PROG)AND job_id IT_PROG;Using the ALL Operator in Multiple-Row Subqueries9000, 6000, 4200 6-17Null Values in a SubquerySELECT emp.last_nameFROM employees empWHERE emp.employee_id NOT IN (SELECT mgr.manager_id FROM employees mgr);6-18SummaryIn this lesson, you should have learned how to:Ide
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025企業(yè)員工聘用合同
- 避免網(wǎng)絡(luò)詐騙培訓
- 2025二月托班寒暑假托管服務時段擴展附加合同
- 胎漏胎動不安中醫(yī)護理
- 2025YY展廳裝飾裝修合同范本
- 2025商務代理合同范本
- 重癥急性胰腺炎的護理
- 閉經(jīng)的診斷和治療
- 2025年企業(yè)雇傭外來務工人員勞動合同
- 正規(guī)重鋼別墅合同范本
- 電焊工基礎(chǔ)知識培訓-課件
- 園林工程建設(shè)概述課件
- 上海中學自招真題解析
- 中國古典詩歌的多義性
- 《鋼鐵是怎樣煉成的》知識競賽課件講義
- 濟青高速涵洞定期檢查報告模版
- 高考寫作指導:作文訓練之語言的提升
- 弱電安裝安全安全技術(shù)交底
- Commvault數(shù)據(jù)庫備份恢復功能介紹
- SJG 05-2020 基坑支護技術(shù)標準-高清現(xiàn)行
- 部編版語文一年級下冊第一單元教材分析及教學建議
評論
0/150
提交評論