Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.

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.
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.
5 5 Aggregating Data Using Group Functions Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina,
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. 5 Aggregating Data Using Group Functions.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
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.
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.
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.
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,
Displaying Data from Multiple Tables (Join). EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS.
Subqueries.
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.
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]
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Chapter 12 Subqueries and Merge Statements
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
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.
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.
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.
Defining a Column Alias
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
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 11

Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve Define subqueries List the types of subqueries Write single-row and multiple-row subqueries After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve Define 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 ’ ? ” “ Which employees have a salary greater than Jones ’ salary? ” Main Query ? “ What is Jones ’ salary? ” ? Subquery

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);

Subqueries A subquery is a SELECT statement that is embedded in a clause of another SELECT statement. They can be very useful when you need to select rows from a table with a condition that depends on the data in the table itself. You can place the subquery in a number of SQL clauses: WHERE clause HAVING clause FROM clause

2975 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 In the example, the inner query determines the salary of employee The outer query takes the result of the inner query and uses this result to display all the employees who earn more than this amount.

Guidelines for Using Subqueries Enclose 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 subqueries. Use multiple-row operators with multiple- row subqueries. Enclose 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 subqueries. Use multiple-row operators with multiple- row subqueries.

Types of Subqueries Single-row subquery : Queries that return only one row from the inner SELECT statement Main query Subquery returns CLERK Multiple-row subquery Queries that return more than one row from the inner SELECT statement CLERKMANAGER Main query Subquery returns Multiple-column subquery Queries that return more than one column from the inner SELECT statement CLERK 7900 MANAGER 7698 Main query Subquery returns

Single-Row Subqueries Return only one row Use single-row comparison operators 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

Display the employees whose job title is the same as that of employee 7369.

Executing Single-Row Subqueries CLERK 1100 ENAME JOB MILLER CLERK ENAME JOB MILLER CLERK SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4(SELECT job 5 FROM emp 6 WHERE empno = 7369) 7 AND sal > 8(SELECT sal 9FROMemp 10WHEREempno = 7876); The example on the slide displays employees whose job title is the same as that of employee 7369 and whose salary is greater than that of employee 7876.

Using Group Functions in a Subquery 800 ENAME JOB SAL SMITH CLERK 800 ENAME JOB SAL SMITH CLERK 800 SQL> SELECTename, job, sal 2 FROMemp 3 WHEREsal = 4(SELECTMIN(sal) 5FROMemp); Display the employee name, job title, and salary of all employees whose salary is equal to the minimum salary.

HAVING Clause with Subqueries The Oracle Server executes subqueries first. The Oracle Server returns results into the HAVING clause of the main query. The Oracle Server executes subqueries first. The Oracle Server returns results into the HAVING clause of the main query. 800 SQL> SELECTdeptno, MIN(sal) 2 FROMemp 3 GROUP BYdeptno 4 HAVINGMIN(sal) > 5(SELECTMIN(sal) 6FROMemp 7WHEREdeptno = 20); The SQL statement on the slide displays all the departments that have a minimum salary greater than that of department 20.

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

Errors with Subqueries One common error with subqueries is more than one row returned for a single-row subquery. In the SQL statement on the slide, the subquery contains a GROUP BY (deptno) clause, which implies that the subquery will return multiple rows, one for each group it finds. In this case, the result of the subquery will be 800, 1300, and 950. The outer query takes the results of the subquery (800, 950, 1300) and uses these results in its WHERE clause. The WHERE clause contains an equal (=) operator, a single-row comparison operator expecting only one value. The = operator cannot accept more than one value from the subquery and hence generates the error. To correct this error, change the = operator to IN.

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

Problems with Subqueries A common problem with subqueries is no rows being returned by the inner query. In the SQL statement on the slide, the subquery contains a WHERE (ename= ' SMYTHE ' ) clause. Presumably, the intention is to find the employee whose name is Smythe. The statement seems to be correct but selects no rows when executed. The problem is that Smythe is misspelled. There is no employee named Smythe. So the subquery returns no rows. The outer query takes the results of the subquery (null) and uses these results in its WHERE clause. The outer query finds no employee with a job title equal to null and so returns no rows.

Multiple-Row Subqueries Return more than one row Use multiple-row comparison operators Return more than one row Use multiple-row comparison operators Operator IN ANY ALL Meaning Equal to any member in the list Compare value to each value returned by the subquery Compare value to every value returned by the subquery

Using ANY Operator in Multiple-Row Subqueries EMPNO ENAME JOB MARTIN SALESMAN 7521 WARD SALESMAN EMPNO ENAME JOB MARTIN SALESMAN 7521 WARD SALESMAN SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal < ANY 4(SELECTsal 5 FROMemp 6WHEREjob = 'CLERK') 7 AND job <> 'CLERK'; Display the employees whose salary is less than any clerk and who are not clerks. ANY means more than the minimum. =ANY is equivalent to IN.

Using ALL Operator in Multiple-Row Subqueries EMPNO ENAME JOB KING PRESIDENT 7566 JONES MANAGER 7902 FORD ANALYST 7788 SCOTT ANALYST EMPNO ENAME JOB KING PRESIDENT 7566 JONES MANAGER 7902 FORD ANALYST 7788 SCOTT ANALYST SQL> SELECT empno, ename, job 2 FROM emp 3 WHERE sal > ALL 4 (SELECTavg(sal) 5 FROMemp 6GROUP BYdeptno); Display the employees whose salary is greater than the average salaries of all the departments. >ALL means more than the maximum and <ALL means less than the minimum.The NOT operator can be used with IN, ANY, and ALL operators.

Summary Subqueries are useful when a query is based on unknown values. SELECTselect_list FROMtable WHEREexpr operator (SELECT select_list FROM table);