CSCI N311: Oracle Database Programming 4-1 Chapter 12: When One Query Depends Upon Another Correlated Subqueries. Subqueries in the where clause. Anti-Joins.

Slides:



Advertisements
Similar presentations
© Abdou Illia MIS Spring 2014
Advertisements

A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Set operators (UNION, UNION ALL, MINUS, INTERSECT) [SQL]
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.
Chapter 5 Recursion in SQL. 2 Example. Let Flights(Flight#, Source_Ctiy, Dest_City) be a relational schema DEN CHI SFO DAL NY UA 930 DL 900 UA 1400 UA.
1 DDL – subquery Sen Zhang. 2 Objectives What is a subquery? Learn how to create nested SQL queries Read sample scripts and book for different kinds of.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
Chapter 6 SQL: Data Manipulation Cont’d. 2 ANY and ALL u ANY and ALL used with subqueries that produce single column of numbers u ALL –Condition only.
Inner join, self join and Outer join Sen Zhang. Joining data together is one of the most significant strengths of a relational database. A join is a query.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Banner and the SQL Select Statement: Part Four (Multiple Connected Select Statements) Mark Holliday Department of Mathematics and Computer Science Western.
Chapter 9 Joining Data from Multiple Tables
SQL advanced select using Oracle 1 7. Multiple Tables: Joins and Set Operations 8. Subqueries: Nested Queries.
ITBIS373 Database Development
A Guide to MySQL 5. 2 Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables Use a subquery.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
SQL (DDL & DML Commands)
Nested Queries (Sub Queries) A nested query is a form of a SELECT command that appears inside another SQL statement. It is also termed as subquery. The.
1 Information Retrieval and Use (IRU) CE An Introduction To SQL Part 1.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
Chapter 4 Multiple-Table Queries
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
1 Multiple Table Queries. 2 Objectives  Retrieve data from more than one table by joining tables  Using IN and EXISTS to query multiple tables  Nested.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SQL advanced select using Oracle 1. 2 Select Simple –data from a single table Advanced –data from more tables join sub-queries.
Chapter 12 Subqueries and Merge Statements
Complex Queries Dale Roberts, Lecturer Computer Science, IUPUI
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6 The Relational Algebra and Relational Calculus.
A Guide to SQL, Eighth Edition Chapter Five Multiple-Table Queries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
1 Ch. 11: Grouping Things Together  ANSI standard SQL Group functions: AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE  Others: 8i: GROUPING (used with CUBE.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
IFS180 Intro. to Data Management Chapter 10 - Unions.
Multiple-Column Subqueries
MySQL Subquery Source: Dev.MySql.com
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Aggregating Data Using Group Functions
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
References: Text Chapters 8 and 9
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Aggregating Data Using Group Functions
Writing Correlated Subqueries
Writing Basic SQL SELECT Statements
(SQL) Aggregating Data Using Group Functions
David M. Kroenke and David J
More SQL Nested and Union queries, and more
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Chapter 8 Advanced SQL.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Presentation transcript:

CSCI N311: Oracle Database Programming 4-1 Chapter 12: When One Query Depends Upon Another Correlated Subqueries. Subqueries in the where clause. Anti-Joins. Using NOT IN or NOT EXISTS with a subquery. Outer Joins. Special type of join between tables where matching data not found. Union, Intersect, Minus. Ways of combining result sets from separate queries together.

4-2 Chapter 12: When One Query Depends Upon Another Regular Joins, Review. Example from p. 242, a 3 table equi-join SELECT W.Name, W.Lodging FROM Worker W, Workerskill WS, Lodging L WHERE W.Name = WS.Name and W.Lodging = L.Lodging and Skill = ‘COMBINE DRIVER’ and Address LIKE ‘%EDMESTON%’;

4-3 Chapter 12: When One Query Depends Upon Another Correlated Subqueries. –A correlated subquery is a query in which the subquery refers to values in the parent query. –A correlated subquery can return the same results as a join, but can be used where a join cannot –update, insert and delete statements. –Group by queries –Example from book, p.245:

4-4 Chapter 12: When One Query Depends Upon Another Another Corrolated Example: –The following statement returns data about employees whose salaries exceed the averages for their departments. The following statement assigns an alias to EMP, the table containing the salary information, and then uses the alias in a correlated subquery: SELECT deptno, ename, sal FROM emp x WHERE sal > (SELECT AVG(sal) FROM emp WHERE x.deptno = deptno) ORDER BY deptno –For each row of the EMP table, the parent query uses the correlated subquery to compute the average salary for members of the same department. The correlated subquery performs these steps for each row of the EMP table: 1. The DEPTNO of the row is determined. 2. The DEPTNO is then used to evaluate the parent query. 3. If that row’s salary is greater than the average salary for that row’s department, then the row is returned. –The subquery is evaluated once for each row of the EMP table.

4-5 Chapter 12: When One Query Depends Upon Another Exists: –The EXISTS keyword is similar IN. –EXISTS tests for the existence of any row. Unlike IN however, EXISTS does not match columns and it usually only makes sense to use with a correlated subquery. –Example from p. 250 select Name, Skill from WORKERSKILL where EXISTS ( select Name from WORKERSKILL group by Name having COUNT(Skill) > 1);

4-6 Chapter 12: When One Query Depends Upon Another Not Exists: –very similar to NOT IN –Much faster! Not exists can use indexes. –Example from p. 255 select S.Skill from SKILL S where NOT EXISTS ( select ‘whatever’ from WORKERSKILL WS where WS.Skill = S.Skill); –Equivalent NOT IN query select S.Skill from SKILL S where S.Skill NOT IN ( select WS.SKILL from WORKERSKILL WS);

4-7 Chapter 12: When One Query Depends Upon Another Outer Join: –The outer join extends the result of a simple join. An outer join returns all rows that satisfy the join condition and those rows from one table for which no rows from the other satisfy the join condition. Such rows are not returned by a simple join. –To write a query that performs an outer join of tables A and B and returns all rows from A, apply the outer join operator (+) to all columns of B in the join condition. –For all rows in A that have no matching rows in B, Oracle returns NULL for any select list expressions containing columns of B. –This is the basic syntax of an outer join of two tables: SELECT table1.column FROM table1, table2 WHERE table1.column = table2.column(+)

4-8 Chapter 12: When One Query Depends Upon Another Outer Join Example: –You want a list of all workers and their skills. If a worker has no skills, you want him listed without corresponding skills. Equi-join query: NAME SKILL ADAH TALBOT WORK DICK JONES SMITHY ELBERT TALBOT DISCUS HELEN BRANDT COMBINE DRIVER JOHN PEARSON COMBINE DRIVER JOHN PEARSON WOODCUTTER JOHN PEARSON SMITHY VICTORIA LYNN SMITHY WILFRED LOWELL WORK WILFRED LOWELL DISCUS Outer Join Query: NAME SKILL ADAH TALBOT WORK ANDREW DYE BART SARJEANT DICK JONES SMITHY DONALD ROLLO ELBERT TALBOT DISCUS GEORGE OSCAR GERHARDT KENTGEN HELEN BRANDT COMBINE DRIVER JED HOPKINS JOHN PEARSON COMBINE DRIVER JOHN PEARSON WOODCUTTER JOHN PEARSON SMITHY KAY AND PALMER WALLBOM PAT LAVAY PETER LAWSON RICHARD KOCH AND BROTHERS ROLAND BRANDT VICTORIA LYNN SMITHY WILFRED LOWELL WORK WILFRED LOWELL DISCUS WILLIAM SWING SELECT W.Name, S.Skill FROM Worker W, WorkerSkill S WHERE W.Name = S.Name; SELECT W.Name, S.Skill FROM Worker W, WorkerSkill S WHERE W.Name = S.Name(+);

4-9 Chapter 12: When One Query Depends Upon Another Outer Join: –Outer join queries are subject to the following rules and restrictions: The (+) operator can only appear in the WHERE clause, not in the select list, and can only be applied to a column of a table or view. If A and B are joined by multiple join conditions, the (+) operator must be used in all of these conditions. The (+) operator can only be applied to a column, rather than to an arbitrary expression, although an arbitrary expression can contain a column marked with the (+) operator. A condition containing the (+) operator cannot be combined with another condition using the OR logical operator. A condition cannot use the IN comparison operator to compare a column marked with the (+) operator to another expression. A condition cannot compare a column marked with the (+) operator to a subquery. –If the WHERE clause contains a condition that compares a column from table B to a constant, the (+) operator must be applied to the column so that the rows from table A for which Oracle has generated NULLs for this column are returned. –In a query that performs outer joins of more than two pairs of tables, a single table can only be the NULL–generated table for one other table. For this reason, you cannot apply the (+) operator to columns of B in the join condition for A and B and the join condition for B and C.

4-10 Chapter 12: When One Query Depends Upon Another Replacing NOT IN with an Outer Join –Example similar to p. 253 Using NOT IN: select A.Name, A.Lodging from Worker A where A.Name NOT IN (select B.Name from WORKERSKILL B where B.skill = ‘SMITHY’); Using Outer Join: select A.Name, A.Lodging from Worker A, WORKERSKILL B where A.Name = B.Name(+) and B.Name IS NULL and B.Skill(+) = ‘SMITHY’;

4-11 Chapter 12: When One Query Depends Upon Another Set Operators: –UNION returns distinct rows for the combination of two select statements. –UNION ALL returns all rows for the combination of two select statements regardless of duplication. –INTERSECT returns distinct rows for the combination of two select statements where data matches. –MINUS return the rows from one select statement excluding the rows of a second select statement.

4-12 Chapter 12: When One Query Depends Upon Another Set Operations Example: –An obvious example: select Name from Longtime minus select Name from Prospect –A subtle example: select Name, Lodging from Longtime minus select Name, Address from Prospect –Columns must be compatible for set operations, but not necessarily the same.

4-13 Chapter 13: Complex Possibilities Creating Complex Views. View of a group, view of a total, combined view. pp FROM Clause Subqueries. Essentially temporary views. pp Temporary Tables, ROLLUP, GROUPING, CUBE. New Oracle8i features. pp Hierarchical Queries. START WITH and CONNECT BY keywords. pp

4-14 Chapter 13: Complex Possibilities FROM Clause Subqueries. New feature as of Oracle 7.3. Works just like a view. Example similar to p. 274 SELECT L1.Person, L1.Amount, 100*L1.Amount/T.Total FROM Ledger L1, (Select SUM(Amount) Total from Ledger where Action IN (‘BOUGHT’,‘PAID’)) T WHERE Action IN (‘BOUGHT’,‘PAID’);

4-15 Chapter 17: DECODE Decode is Oracle non-standard SQL. Extremely powerful, yet underutilized. Decode works similar to if/then/else DECODE ( value, if1, then1, if2, then2,..., default ) Common uses illustrated in the text: –Aggregating groups of data into a single column. Example, p. 353 –Flip a table on its side, ie rows become columns. Example, p. 358 –Dividing data into sections based on row number. This is done in conjunction with the MOD function. Example, p. 359 –Complex computations that require if/then logic. Example, p. 362