Copyright  Oracle Corporation, 1998. All rights reserved. 6 Writing Correlated Subqueries.

Slides:



Advertisements
Similar presentations
4-1 Copyright  Oracle Corporation, All rights reserved. Types of Joins Equijoin Non-equijoin Outer join Self join.
Advertisements

Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
12-1 Copyright  Oracle Corporation, All rights reserved. What Is a View? EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
Restricting and sorting data 16 May May May Created By Pantharee Sawasdimongkol.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
1Eyad Alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Copyright  Oracle Corporation, All rights reserved. 6 Subqueries.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
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.
6-1 Copyright  Oracle Corporation, All rights reserved. Types of Subqueries Single-row subquery Main query Subquery returns CLERK Multiple-row subquery.
Multiple-Column Subqueries. 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. 7 Multiple-Column Subqueries.
Copyright  Oracle Corporation, All rights reserved. 7 Multiple-Column Subqueries.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
Session 3: SQL (B): Parts 3 & 4 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
Subqueries.
Subqueries.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Copyright  Oracle Corporation, All rights reserved. 4 Accessing a Database Using JBCL.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
7 Multiple-Column Subqueries. 7-2 Objectives At the end of this lesson, you should be able to: Write a multiple-column subquery Describe and explain the.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
SQL. Relating Multiple Tables Relational Database Terminology Row PK Column FK Field NULL.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Copyright س Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
1 Writing Basic SQL Statements. 1-2 Objectives At the end of this lesson, you should be able to: List the capabilities of SQL SELECT statements Execute.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
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.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
Copyright  Oracle Corporation, All rights reserved. 4 Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following: –Write SELECT statements to access.
4 Displaying Data from Multiple Tables. 4-2 Objectives At the end of this lesson, you should be able to: Write SELECT statements to access data from more.
Copyright س Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Multiple-Column Subqueries
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Writing Correlated Subqueries
جملة الاستعلام الأساسية
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries

6-2 Copyright  Oracle Corporation, All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that can be solved with correlated subqueries Write correlated subqueries Use the EXISTS and NOT EXISTS operators Update and delete rows using correlated subqueries After completing this lesson, you should be able to do the following: Describe the types of problems that can be solved with correlated subqueries Write correlated subqueries Use the EXISTS and NOT EXISTS operators Update and delete rows using correlated subqueries

6-3 Copyright  Oracle Corporation, All rights reserved. What Is a Subquery? A subquery is a SELECT statement embedded in a clause of another SQL statement. SELECT... FROM... WHERE... (SELECT... FROM... WHERE...) MainQuery Subquery

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

6-5 Copyright  Oracle Corporation, All rights reserved SQL> SELECT ename 2 FROM emp 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno = 7566); Using a Subquery ENAME KING FORD SCOTT ENAME KING FORD SCOTT

6-6 Copyright  Oracle Corporation, All rights reserved. Correlated Subqueries Used to affect row-by-row processing, each subquery is executed once for every row of the outer query. GET candidate row EXECUTE inner query using candidate row value USE value(s) from inner query to qualify candidate row

6-7 Copyright  Oracle Corporation, All rights reserved. Correlated Subqueries SELECT outer1, outer2,... FROM table1 alias1 WHERE outer1 operator (SELECT inner1 FROM table2 alias2 WHERE alias1.outer2 = alias2.inner1); subquery references a column from a table in the parent query. The subquery references a column from a table in the parent query.

6-8 Copyright  Oracle Corporation, All rights reserved. Using Correlated Subqueries Each time the outer query is processed the inner query is evaluated. SQL> SELECT empno, sal, deptno 2 FROM emp outer 3 WHERE sal > (SELECT AVG(sal) 4 FROM emp inner 5 WHERE outer.deptno = inner.deptno); EMPNO SAL DEPTNO rows selected. EMPNO SAL DEPTNO rows selected. Find all employees who make more than the average salary in their department.

6-9 Copyright  Oracle Corporation, All rights reserved. Using the EXISTS Operator If a subquery row value is found: – The search does not continue in the inner query. – The condition is flagged TRUE. If a subquery row value is not found: – The condition is flagged FALSE. – The search continues in the inner query. If a subquery row value is found: – The search does not continue in the inner query. – The condition is flagged TRUE. If a subquery row value is not found: – The condition is flagged FALSE. – The search continues in the inner query.

6-10 Copyright  Oracle Corporation, All rights reserved. Find employees who have at least one person reporting to them. Using the EXISTS Operator EMPNO ENAME JOB DEPTNO KING PRESIDENT BLAKE MANAGER CLARK MANAGER JONES MANAGER rows selected. SQL> SELECT empno, ename, job, deptno 2 FROM emp outer 3 WHERE EXISTS (SELECT empno 4 FROM emp inner 5 WHERE inner.mgr = outer.empno);

6-11 Copyright  Oracle Corporation, All rights reserved. Find all departments that do not have any employees. Using the NOT EXISTS Operator DEPTNO DNAME OPERATIONS DEPTNO DNAME OPERATIONS SQL> SELECTdeptno, dname 2 FROM dept d 3 WHERENOT EXISTS (SELECT '1' 4 FROM emp e 5 WHERE d.deptno = e.deptno);

6-12 Copyright  Oracle Corporation, All rights reserved. Correlated UPDATE Use a correlated subquery to update rows in one table based on rows from another table. UPDATE table1 alias1 SET column = (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column); UPDATE table1 alias1 SET column = (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column);

6-13 Copyright  Oracle Corporation, All rights reserved. DELETE FROM table1 alias1 WHERE column operator (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column); DELETE FROM table1 alias1 WHERE column operator (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column); Use a correlated subquery to delete only those rows that also exist in another table. Correlated DELETE

6-14 Copyright  Oracle Corporation, All rights reserved. Summary Correlated subqueries are useful whenever a subquery must return a different result for each candidate row. The EXISTS operator is a Boolean operator, testing the presence of a value. Correlated subqueries can be used with SELECT, UPDATE, and DELETE statements. Correlated subqueries are useful whenever a subquery must return a different result for each candidate row. The EXISTS operator is a Boolean operator, testing the presence of a value. Correlated subqueries can be used with SELECT, UPDATE, and DELETE statements.

6-15 Copyright  Oracle Corporation, All rights reserved. Practice Overview Writing correlated subqueries Using the EXISTS operator Writing correlated subqueries Using the EXISTS operator

6-16 Copyright  Oracle Corporation, All rights reserved.

6-17 Copyright  Oracle Corporation, All rights reserved.

6-18 Copyright  Oracle Corporation, All rights reserved.