Subqueries.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
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.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
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.
Copyright  Oracle Corporation, All rights reserved. 6 Writing Correlated 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.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
Subqueries.
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.
Copyright © 2004, Oracle. All rights reserved. U SING S UBQUERIES TO S OLVE Q UERIES.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
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.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Chapter 12 Subqueries and Merge Statements
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 © Oracle Corporation, All rights reserved. Subqueries.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
Subqueries.
Oracle CONNECT BY function JAVA WEB Programming. Emp 테이블의 내용 ( 상 / 하급자 계층구조 ) SQL> select * from emp; EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
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.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
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.
Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join) Displaying Data from Multiple Tables (Join Displaying Data from.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Communicating with a RDBMS Using SQL Database SQL> SELECT loc 2 FROM dept; SQL> SELECT loc 2 FROM dept; SQL statement is entered Statement is sent to database.
Using Subqueries to Solve Queries
Multiple-Column Subqueries
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Writing Correlated Subqueries
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Review SQL Advanced.
Restricting and Sorting Data
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:

Subqueries

Objectives After completing this lesson , you should be able to do the following: Describe the types of problems that subqueries can solve Difine subqueries List the types of subqueries Write single-row and multiple-row subqueries

Using a Subquery to Solve a Problem Who has a salary greater than Jones’?” Main “Which employees have a salary greater than Jones’ salary?” subquery ‘What is Jones’ salary?

Subqueries The subquery ( inner query ) executes SELECT select_list FROM table [WHERE expr operator (SELECT select_list FROM table); The subquery ( inner query ) executes once before the main query. The result of the subquery is used by the main query ( outer query ).

Using a Subquery SQL> SELECT ename 2 FROM emp 2975 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno = 7566); ENAME ---------------- KING FORD SCOTT

Guidelines for Using Subqueries Exclose subqueries in parentheses. Place subqueries on the right side of the comparison operator. Do not add an ORDER BY clause to a subquery. Use single-row operators with single-row subquerys. Use multiple-row operators with multiple-row subqueries.

Types of Subqueries Single-row subquery Multiple-row subquery Multiple-column subquery Main query Subquery returns CLERK Main query Subquery CLERK MANAGER returns Main query Subquery returns CLERK 7900 MANAGER 7698

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 to

Executing Single-Row Subqueries SQL> SELECT ename, job 2 FROM emp 3 WHERE job = CLERK 4 (SELECT job 5 FROM emp 6 WHERE empno = 7369) 7 AND sal > 1100 8 (SELECT sal 9 FROM emp 10 WHERE empno = 7876); ENAME JOB ---------------- --------------- MILLER CLERK

Using Group Functions in a Subquery SQL> SELECT ename, job, sal 2 FROM emp 800 3 WHERE sal = 4 (SELECT MIN (sal) 5 FROM emp; ENAME JOB SAL ---------------- --------------- ----------------- SMITH CLERK 800

HAVING Clause with Subqueries The Oracle Server executes subqueries first. The Oracle Server returns results into the HAVING clause of the main query. SQL> SELECT deptno, MIN (sal) 2 FROM emp 3 GROUP BY deptno 800 4 HAVING MIN (sal) > 5 (SELECT MIN (sal) 6 FROM emp 7 WHERE deptno = 20)

What Is Wrong with This Statement? SQL> SELECT empno, ename 2 FROM emp 3 WHERE sal = 4 (SELECT MIN (sal) 5 FROM emp 6 GROUP BY deptno ) ; ERROR : ORA – 01427 : single – row subquery returns more than One row no rows selected Single – row operator with Multiple – row subquery

Will This Statement Work? SQL> SELECT empno, job 2 FROM emp 3 WHERE job = 4 (SELECT job 5 FROM emp 6 WHERE ename=‘SMYTHE’ ) ; no rows selected Subquery returns no values

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 subquery

Using ANY Operator in Multiple-Row Subqueries SQL> SELECT empno, ename , job , 2 FROM emp 3 WHERE sal < ANY 4 (SELECT sal 5 FROM emp 6 WHERE job = ‘ CLERK ’ ) 7 AND job < > ‘ CLERK ’ ; 1300 1100 800 950 EMPNO ENAME JOB ------------- --------------- ------------ 7654 MARTIN SALEMAN 7521 WARD SALEMAN

Using ALL Operator in Multiple-Row Subqueries SQL> SELECT empno, ename , job , 2 FROM emp 3 WHERE sal < ANY 4 (SELECT avg ( sal ) 5 FROM emp 6 GROUP BY depno ) ; 2175 2916.6667 EMPNO ENAME JOB ------------- --------------- ------------ 7893 KING PRESIDENT 7566 JONES MANAGER 7902 FORD ANALYST 7788 SCOTT ANALYST

Summary Subqueries are useful when a query is base on unknown values. SQL> SELECT select_list 2 FROM table 3 WHERE expr operator 4 (SELECT select_list 5 FROM table );

Practice Overview Creating subqueries to query values based on unknown criteria Using subqueries fo find out what values exist in one set of data and not in another