Copyright  Oracle Corporation, 1999. All rights reserved. 7 Multiple-Column Subqueries.

Slides:



Advertisements
Similar presentations
Using the Set Operators
Advertisements

4-1 Copyright  Oracle Corporation, All rights reserved. Types of Joins Equijoin Non-equijoin Outer join Self join.
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Introduction To SQL Lynnwood Brown President System Managers LLC Copyright System Managers LLC 2003 all rights reserved.
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.
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.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
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.
7 7 Multiple-Column Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated Subqueries.
18 Copyright © Oracle Corporation, All rights reserved. Advanced Subqueries.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
Copyright  Oracle Corporation, All rights reserved. 1 Writing Basic SQL Statements.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
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.
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)
Copyright س Oracle Corporation, All rights reserved. I Introduction.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
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. 18 Interacting with the Oracle Server.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Chapter 12 Subqueries and Merge Statements
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
Review SQL Advanced. Capabilities of SQL SELECT Statements Selection Projection Table 1 Table 2 Table 1 Join.
7 7 Multiple-Column Subqueries Important Legal Notice:  Materials on this lecture are sourced from a book titled “Oracle Education” by Kochhar, Gravina,
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Subqueries.
2-1 Limiting Rows Using a Selection “…retrieve all employees in department 10” EMP EMPNO ENAME JOB... DEPTNO 7839KINGPRESIDENT BLAKEMANAGER CLARKMANAGER.
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.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
Single-Row Functions. SQL Functions FunctionInput arg 1 arg 2 arg n Function performs action OutputResultvalue.
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.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
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.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
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
Subqueries Schedule: Timing Topic 25 minutes Lecture
Review SQL Advanced.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Database Programming Using Oracle 11g
Presentation transcript:

Copyright  Oracle Corporation, All rights reserved. 7 Multiple-Column Subqueries

7-2 Copyright  Oracle Corporation, All rights reserved. Objectives After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe and explain the behavior of subqueries when null values are retrieved Write a subquery in a FROM clause After completing this lesson, you should be able to do the following: Write a multiple-column subquery Describe and explain the behavior of subqueries when null values are retrieved Write a subquery in a FROM clause

7-3 Copyright  Oracle Corporation, All rights reserved. Multiple-Column Subqueries Main query MANAGER 10 Subquery SALESMAN 30 MANAGER 10 CLERK 20 Main query compares MANAGER 10 Values from a multiple-row and multiple-column subquery SALESMAN 30 MANAGER 10 CLERK 20 to

7-4 Copyright  Oracle Corporation, All rights reserved. Using Multiple-Column Subqueries Display the order id, product id, and quantity of items in the item table that match both the product id and quantity of an item in order 605. SQL> SELECTordid, prodid, qty 2 FROMitem 3 WHERE (prodid, qty) IN 4 (SELECT prodid, qty 5 FROM item 6 WHERE ordid = 605) 7 ANDordid <> 605;

7-5 Copyright  Oracle Corporation, All rights reserved. Using Multiple-Column Subqueries Display the order number, product number, and quantity of any item in which the product number and quantity match both the product number and quantity of an item in order 605. SQL> SELECTordid, prodid, qty 2 FROMitem 3 WHERE (prodid, qty) IN 4 (SELECT prodid, qty 5 FROM item 6 WHERE ordid = 605) 7 ANDordid <> 605;

7-6 Copyright  Oracle Corporation, All rights reserved. Column Comparisons Pairwise PRODIDQTY Nonpairwise PRODIDQTY

7-7 Copyright  Oracle Corporation, All rights reserved. Nonpairwise Comparison Subquery SQL> SELECT ordid, prodid, qty 2 FROM item 3 WHERE prodid IN (SELECT prodid 4 FROM item 5 WHERE ordid = 605) 6 ANDqty IN (SELECT qty 7 FROM item 8 WHERE ordid = 605) 9 ANDordid <> 605; Display the order number, product number, and quantity of any item in which the product number and quantity match any product number and any quantity of an item in order 605.

7-8 Copyright  Oracle Corporation, All rights reserved. Nonpairwise Subquery ORDID PRODID QTY rows selected.

7-9 Copyright  Oracle Corporation, All rights reserved. Null Values in a Subquery SQL> SELECTemployee.ename 2 FROM emp employee 3 WHERE employee.empno NOT IN 4(SELECT manager.mgr 5 FROM emp manager); no rows selected.

7-10 Copyright  Oracle Corporation, All rights reserved. SQL> SELECT a.ename, a.sal, a.deptno, b.salavg 2 FROM emp a, (SELECT deptno, avg(sal) salavg 3 FROM emp 4 GROUP BY deptno) b 5 WHERE a.deptno = b.deptno 6 AND a.sal > b.salavg; Using a Subquery in the FROM Clause ENAME SAL DEPTNO SALAVG KING JONES SCOTT rows selected. ENAME SAL DEPTNO SALAVG KING JONES SCOTT rows selected.

7-11 Copyright  Oracle Corporation, All rights reserved. Summary A multiple-column subquery returns more than one column. Column comparisons in multiple- column comparisons can be pairwise or nonpairwise. A multiple-column subquery can also be used in the FROM clause of a SELECT statement. A multiple-column subquery returns more than one column. Column comparisons in multiple- column comparisons can be pairwise or nonpairwise. A multiple-column subquery can also be used in the FROM clause of a SELECT statement.

7-12 Copyright  Oracle Corporation, All rights reserved. Practice Overview Creating multiple-column subqueries

7-13 Copyright  Oracle Corporation, All rights reserved.

7-14 Copyright  Oracle Corporation, All rights reserved.