6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.

Slides:



Advertisements
Similar presentations
DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
Advertisements

Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
6 6 Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published.
Multiple-Column Subqueries 12. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using the Set Operators Assist. Prof. Pongpisit Wuttidittachotti, Ph.D. Faculty.
2 Copyright © 2004, Oracle. All rights reserved. Creating Stored Functions.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Subqueries.
Subqueries.
Copyright © 2004, Oracle. All rights reserved. U SING S UBQUERIES TO S OLVE Q UERIES.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Chapter 12 Subqueries and Merge Statements
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Subqueries.
6 Subqueries. 6-2 Objectives At the end of this lesson, you should be able to: Describe the types of problems that subqueries can solve Define subqueries.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Using the Set Operators
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries

6-2 Copyright © 2004, Oracle. All 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 subqueries

6-3 Copyright © 2004, Oracle. All rights reserved. Using a Subquery to Solve a Problem Who has a salary greater than Abel’s? Which employees have salaries greater than Abel’s salary? Main query: What is Abel’s salary? Subquery:

6-4 Copyright © 2004, Oracle. All rights reserved. The subquery (inner query) executes once before the main query (outer query). The result of the subquery is used by the main query. SELECTselect_list FROMtable WHEREexpr operator (SELECTselect_list FROMtable); Subquery Syntax

6-5 Copyright © 2004, Oracle. All rights reserved. SELECT last_name FROM employees WHERE salary > (SELECT salary FROM employees WHERE last_name = 'Abel'); Using a Subquery 11000

6-6 Copyright © 2004, Oracle. All 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 © 2004, Oracle. All rights reserved. Types of Subqueries Single-row subquery Multiple-row subquery Main query Subquery returns ST_CLERK SA_MAN Main query Subquery returns

6-8 Copyright © 2004, Oracle. All rights reserved. Single-Row Subqueries Return only one row Use single-row comparison operators OperatorMeaning = Equal to > Greater than >= Greater than or equal to < Less than <= Less than or equal to <> Not equal to

6-9 Copyright © 2004, Oracle. All rights reserved. SELECT last_name, job_id, salary FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE employee_id = 141) AND salary > (SELECT salary FROM employees WHERE employee_id = 143); Executing Single-Row Subqueries ST_CLERK 2600

6-10 Copyright © 2004, Oracle. All rights reserved. SELECT last_name, job_id, salary FROM employees WHERE salary = (SELECT MIN(salary) FROM employees); Using Group Functions in a Subquery 2500

6-11 Copyright © 2004, Oracle. All rights reserved. SELECT department_id, MIN(salary) FROM employees GROUP BY department_id HAVING MIN(salary) > (SELECT MIN(salary) FROM employees WHERE department_id = 50); The HAVING Clause with Subqueries The Oracle server executes subqueries first. The Oracle server returns results into the HAVING clause of the main query. 2500

6-12 Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name FROM employees WHERE 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 than one row Single-row operator with multiple-row subquery

6-13 Copyright © 2004, Oracle. All rights reserved. SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name = 'Haas'); Will This Statement Return Rows? no rows selected Subquery returns no values.

6-14 Copyright © 2004, Oracle. All rights reserved. Multiple-Row Subqueries Return more than one row Use multiple-row comparison operators OperatorMeaning 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 subquery

6-15 Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary < ANY (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG'; Using the ANY Operator in Multiple-Row Subqueries 9000, 6000, 4200 …

6-16 Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary < ALL (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG'; Using the ALL Operator in Multiple-Row Subqueries 9000, 6000, 4200

6-17 Copyright © 2004, Oracle. All rights reserved. SELECT emp.last_name FROM employees emp WHERE emp.employee_id NOT IN (SELECT mgr.manager_id FROM employees mgr); no rows selected Null Values in a Subquery

6-18 Copyright © 2004, Oracle. All rights reserved.

6-19 Copyright © 2004, Oracle. All rights reserved. SELECTselect_list FROMtable WHEREexpr operator (SELECT select_list FROM table); 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