1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.

Slides:



Advertisements
Similar presentations
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Advertisements

DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
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. 6 Subqueries.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
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.
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.
Restricting and Sorting Data. Consider the table employee(employee_id,last_name,job_id, department_id ) assume that you want to display all the employees.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
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,
Restricting and Sorting Data. ◦ Limiting rows with:  The WHERE clause  The comparison conditions using =,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
4 Copyright © 2006, Oracle. All rights reserved. Restricting and Sorting Data.
Subqueries.
Subqueries.
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.
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
Copyright © 2004, Oracle. All rights reserved. MANIPULATING LARGE DATA SETS Oracle Lecture 10.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
3 Copyright © 2006, Oracle. All rights reserved. Manipulating Large Data Sets.
2 第二讲 Restricting and Sorting Data. Objectives After completing this lesson, you should be able to do the following: Limit the rows retrieved by a query.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
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
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
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.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Creating Views Schedule: Timing Topic 20 minutes Lecture
Using the Set Operators
Using Subqueries to Solve Queries
Writing Correlated Subqueries
Using the Set Operators
Using Subqueries to Solve Queries
高级子查询 Schedule: Timing Topic 60 minutes Lecture 50 minutes Practice
高级子查询 Schedule: Timing Topic 60 minutes Lecture 50 minutes Practice
Subqueries Schedule: Timing Topic 25 minutes Lecture
Retrieving Data by Using Subqueries
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Using the Set Operators
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Restricting and Sorting Data
Presentation transcript:

1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang

2 Using a Subquery to Solve a Problem  Who has a salary greater than Rose’s? Which employees have salaries greater than Rose’s salary? Main Query: ? What is Rose’s salary? ? Subquery

3 Subquery Syntax  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);

4 SELECT last_name FROM employees WHERE salary > (SELECT salary FROM employees WHERE last_name = ‘Rose'); Using a Subquery 11000

5 Guidelines for Using Subqueries  Enclose subqueries in parentheses  Place subqueries on the right side of the comparison condition  The ORDER BY clause in the subquery is not needed unless you are performing top-n analysis  Use single-row operators with single-row subqueries and use multiple-row operators with multiple-row subqueries

6 Types of Subqueries Main query Subquery returns ST_CLERK Multiple-row subquery ST_CLERK SA_MAN Main query Subquery returns Single-row subquery

7 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

8 SELECT last_name, job_id, salary FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE employee_id = 141) AND salary > (SELECT salary FROM employees WHERE employee_id = 143); Executing Single-row Subqueries ST_CLERK 2600

9 SELECT last_name, job_id, salary FROM employees WHERE salary = (SELECT MIN(salary) FROM employees); Using Group Functions in a Subquery 2500

10 The HAVING Clause With Subqueries  The oracle server executes subqueries first  The oracle server returns results into the HAVING clause of the main query SELECT department_id, MIN(salary) FROM employees GROUP BY department_id HAVING MIN(salary) > (SELECT MIN(salary) FROM employees WHERE department_id = 50); 2500

11 SELECT employee_id, last_name FROM employees WHERE salary = (SELECT MIN(salary) FROM employees GROUP BY department_id); What is Wrong with this Statement? ERROR at line 4: ORA-01427: single-row subquery returns more than one row ERROR at line 4: ORA-01427: single-row subquery returns more than one row Single-row operator with multiple-row subquery

12 Null Values in a Subquery no rows selected SELECT last_name, job_id FROM employees WHERE job_id = (SELECT job_id FROM employees WHERE last_name = 'Haas'); Subquery returns no values

13 Top-N Analysis  Top-N queries ask for the n largest or smallest values of a column. For example: What are the ten best selling products? What are the ten worst selling products?  Both largest values and smallest values sets are considered Top-N queries.

14 Performing Top-N Analysis  The high-level structure of a Top-N analysis query is: SELECT [column_list], ROWNUM FROM (SELECT [column_list] FROM table ORDER BY Top-N_column) WHERE ROWNUM <= N; SELECT [column_list], ROWNUM FROM (SELECT [column_list] FROM table ORDER BY Top-N_column) WHERE ROWNUM <= N;

15 Example of Top-N Analysis  To display the top three earner names and salaries from the EMPLOYEES table: SELECT ROWNUM as RANK, last_name, salary FROM (SELECT last_name,salary FROM employees ORDER BY salary DESC) WHERE ROWNUM <= 3;

16 Multiple-row Subqueries  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

17 Using the ANY Operator in Multiple-row Subqueries 9000, 6000, 4200 SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary < ANY (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG'; …

18 SELECT employee_id, last_name, job_id, salary FROM employees WHERE salary < ALL (SELECT salary FROM employees WHERE job_id = 'IT_PROG') AND job_id <> 'IT_PROG'; Using the ALL Operator in Multiple-row Subqueries 9000, 6000, 4200

19 Multiple-Column Subqueries Main query WHERE (MANAGER_ID, DEPARTMENT_ID) IN Subquery Each row of the main query is compared to values from a multiple-row and multiple-column subquery.

20 Column Comparisons  Column comparisons in a multiple-column subquery can be: Pairwise comparisons Nonpairwise comparisons

21 Pairwise Comparison Subquery  Display the details of the employees who are managed by the same manager and work in the same department as the employees with EMPLOYEE_ID 178 or 174 SELECTemployee_id, manager_id, department_id FROMemployees WHERE (manager_id, department_id) IN (SELECT manager_id, department_id FROM employees WHERE employee_id IN (178,174)) ANDemployee_id NOT IN (178,174);

22 Nonpairwise Comparison Subquery SELECT employee_id, manager_id, department_id FROM employees WHERE manager_id IN (SELECT manager_id FROM employees WHERE employee_id IN (174,141)) AND department_id IN (SELECT department_id FROM employees WHERE employee_id IN (174,141)) ANDemployee_id NOT IN(174,141);

23 SELECT a.last_name, a.salary, a.department_id, b.salavg FROM employees a, (SELECT department_id, AVG(salary) salavg FROM employees GROUP BY department_id) b WHERE a.department_id = b.department_id AND a.salary > b.salavg; Using a Subquery in the FROM Clause

24 Correlated Subqueries  Correlated subqueries are used for row-by- row processing  Each subquery is executed once for every row of the outer query GET candidate row from outer query EXECUTE inner query using candidate row value USE values from inner query to qualify or disqualify candidate row

25 Correlated Subqueries SELECT column1, column2,... FROM table1 WHERE column1 operator (SELECT colum1, column2 FROM table2 WHERE expr1 =.expr2); The subquery references a column from a table in the parent query. alias

26 SELECT last_name, salary, department_id FROM employees outer WHERE salary > Using Correlated Subqueries  Find all employees who earn more than the average salary in their department Each time a row from the outer query is processed, the inner query is evaluated (SELECT AVG(salary) FROM employees WHERE department_id = outer.department_id) ;

(1) Select last_name, salary, department_id From employee where salary > (Select Avg(salary) from employee group by department_id ) (2) Select last_name, salary, department_id From employee where salary > (Select Avg(salary) from employee)

28 Using Correlated Subqueries SELECT e.employee_id, last_name,e.job_id FROM employees e WHERE 2 <= (SELECT COUNT(*) FROM job_history WHERE employee_id = e.employee_id); Display details of those employees who have switched jobs at least twice.

29 Using the EXISTS Operator  The EXISTS operator tests for existence of rows in the results set of the subquery  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

30 SELECT employee_id, last_name, job_id, department_id FROM employees outer WHERE EXISTS ( SELECT 'X' FROM employees WHERE manager_id = outer.employee_id); Using the EXISTS Operator Manager ID Null Null

31 SELECT department_id, department_name FROM departments d WHERE NOT EXISTS (SELECT 'X' FROM employees WHERE department_id = d.department_id); Using the NOT EXISTS Operator Find all departments that do not have any employees

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

33 Correlated UPDATE  Denormalize the EMPLOYEES table by adding a column to store the department name  Populate the table by using a correlated update ALTER TABLE employees ADD(department_name VARCHAR2(14)); UPDATE employees e SET department_name = (SELECT department_name FROM departments d WHERE e.department_id = d.department_id);

34 DELETE FROM table1 alias1 WHERE column operator (SELECT expression FROM table2 alias2 WHERE alias1.column = alias2.column); Correlated DELETE Use a correlated subquery to delete rows in one table based on rows from another table.

35 DELETE FROM employees E WHERE employee_id = (SELECT employee_id FROM emp_history WHERE employee_id = E.employee_id); Correlated DELETE  Use a correlated subquery to delete only those rows from the EMPLOYEES table that also exist in the EMP_HISTORY table

36 The WITH Clause  Using the WITH clause, you can use the same query block in a SELECT statement when it occurs more than once within a complex query.  The WITH clause retrieves the results of a query block and stores it in the user's temporary tablespace.  The WITH clause improves performance

37 WITH Clause: Example  Using the WITH clause, write a query to display the department name and total salaries for those departments whose total salary is greater than the average salary across departments.

38 WITH Clause: Example WITH dept_costs AS ( SELECT d.department_name, SUM(e.salary) AS dept_total FROM employees e, departments d WHERE e.department_id = d.department_id GROUP BY d.department_name), avg_cost AS ( SELECT SUM(dept_total)/COUNT(*) AS dept_avg FROM dept_costs) SELECT * FROM dept_costs WHERE dept_total > (SELECT dept_avg FROM avg_cost) ORDER BY department_name;

39 Readings  Connolly & Begg 4 th Ed Chapter ; 5.3.8