• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lesson06 使用子查询
 

Lesson06 使用子查询

on

  • 307 views

Oracle OCP考试之007第6章,学会使用子查询

Oracle OCP考试之007第6章,学会使用子查询

Statistics

Views

Total Views
307
Views on SlideShare
307
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Lesson06 使用子查询 Lesson06 使用子查询 Presentation Transcript

    • Oracle OCP 考试系列培训 之 1Z0-007 Lesson6 www.OracleOnLinux.cn6-1 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • 6Using Subqueries to Solve Queries Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Objectives After completing this lesson, you should be able to do the following: • Define subqueries • Describe the types of problems that subqueries can solve • List the types of subqueries • Write single-row and multiple-row subqueries6-3 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Using a Subquery to Solve a Problem Who has a salary greater than Abel’s? Abel’ Main query: Which employees have salaries greater than Abel’s salary? Abel’ Subquery: What is Abel’s salary? Abel’6-4 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Subquery Syntax SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table); • The subquery (inner query) executes once before the main query (outer query). • The result of the subquery is used by the main query.6-5 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Using a Subquery SELECT last_name FROM employees 11000 WHERE salary > (SELECT salary FROM employees WHERE last_name = Abel);6-6 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Guidelines for Using Subqueries • Enclose subqueries in parentheses. • Place subqueries on the right side of the comparison condition. • The ORDER BY clause 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 with multiple-row subqueries.6-7 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Types of Subqueries • Single-row subquery Main query returns Subquery ST_CLERK • Multiple-row subquery Main query returns ST_CLERK Subquery SA_MAN6-8 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Single-Row Subqueries • Return only one row • Use single-row comparison operators Operator Meaning = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to6-9 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Executing Single-Row Subqueries SELECT last_name, job_id, salary FROM employees WHERE job_id = ST_CLERK (SELECT job_id FROM employees WHERE employee_id = 141) AND salary > 2600 (SELECT salary FROM employees WHERE employee_id = 143);6-10 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Using Group Functions in a Subquery SELECT last_name, job_id, salary FROM employees 2100 WHERE salary = (SELECT MIN(salary) FROM employees);6-11 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • The HAVING Clause with Subqueries • The Oracle server executes subqueries first. • The Oracle server returns results into the HAVING clause of the main query. SELECT department_id, MIN(salary) FROM employees GROUP BY department_id 2100 HAVING MIN(salary) > (SELECT MIN(salary) FROM employees WHERE department_id = 50);6-12 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • What Is Wrong with This Statement? SELECT employee_id, last_name FROM employees WHERE salary = (SELECT MIN(salary) FROM employees GROUP BY department_id); ERROR at line 4: ORA-01427: single-row subquery returns more than one row Single-row operator with multiple-row subquery6-13 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Will This Statement Return Rows? SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name = Haas); no rows selected Subquery returns no values.6-14 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Multiple-Row Subqueries • Return more than one row • Use multiple-row comparison operators Operator Meaning IN Equal to any member in the list ANY Compare value to each value returned by the subquery ALL Compare value to every value returned by the subquery6-15 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Using the ANY Operator in Multiple-Row Subqueries SELECT employee_id, last_name, job_id, salary FROM employees 9000, 6000, 4200 WHERE salary < ANY (SELECT salary FROM employees WHERE job_id = IT_PROG) AND job_id <> IT_PROG; …6-16 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Using the ALL Operator in Multiple-Row Subqueries SELECT employee_id, last_name, job_id, salary FROM employees 9000, 6000, 4200 WHERE salary < ALL (SELECT salary FROM employees WHERE job_id = IT_PROG) AND job_id <> IT_PROG;6-17 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Null Values in a Subquery SELECT emp.last_name FROM employees emp WHERE emp.employee_id NOT IN (SELECT mgr.manager_id FROM employees mgr); no rows selected SELECT emp.last_name FROM employees emp WHERE emp.employee_id NOT IN (SELECT mgr.manager_id FROM employees mgr WHERE manager_id is not null); 子查询中:凡是返回结果包含NULL值,其结果一定为NULL6-18 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Summary In this lesson, you should have learned how to: • Identify when a subquery can help solve a question • Write subqueries when a query is based on unknown values SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table);6-19 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.
    • Practice 6: Overview This practice covers the following topics: • Creating subqueries to query values based on unknown criteria • Using subqueries to find out which values exist in one set of data and not in another – P59 Q92 – P61 Q98 ,P62 Q99 – P73 Q119,P91 Q151,P92 Q1536-20 Copyright © 2012, http://OracleOnLinux.cn. Part rights reserved.