 After completing this lesson, you should be able to do the following: ◦ Interpret the concept of a hierarchical query ◦ Create a tree-structured report.

Slides:



Advertisements
Similar presentations
DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
Advertisements

Notice that No where clause In the syntax.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
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.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
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.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
9 Copyright © 2012, Oracle and/or its affiliates. All rights reserved. Using the Set Operators Assist. Prof. Pongpisit Wuttidittachotti, Ph.D. Faculty.
Ceng 356-Lab2. Objectives After completing this lesson, you should be able to do the following: Limit the rows that are retrieved by a query Sort the.
Ceng 356-Lab1. Objectives After completing this lesson, you should be able to do the following: Get Familiar with the development environment List the.
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.
Single-Row Functions. Two Types of SQL Functions There are two distinct types of functions: Single-row functions Multiple-row functions Single-Row Functions.
Chapter 5 Advanced SQL. 2 Recursion in SQL Example. Let Flights(Flight#, Source_City, Dest_City) be a relational schema DEN CHI SFO DAL NY UA 930 DL 900.
Sections 3 – Joins & Hierarchical Queries
Basisdata Pertanian. After completing this lesson, you should be able to do the following:  Write SELECT statements to access data from more than one.
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.
LECTURE 8.  Consider the table employee(employee_id,last_name,job_id, department_id )  assume that you want to display all the employees in department.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.
Oracle Tutorials – May 2013.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
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. Retrieving Data Using the SQL SELECT Statement Satrio Agung Wicaksono, S.Kom., M.Kom.
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.
2 Copyright © 2004, Oracle. All rights reserved. Restricting and Sorting Data.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
Using SET Operators Fresher Learning Program January, 2012.
Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins.
6 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using Subqueries.
2 Copyright © 2009, Oracle. All rights reserved. Restricting and Sorting Data.
Subqueries.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
19 Copyright © Oracle Corporation, All rights reserved. Hierarchical Retrieval.
ORACLE SQL Fundamental II xpp-e-f 重點 xpp-e : Generating Reports by Grouping Related Data xpp-f : Hierarchical Retrieval.
Hierarchical Retrieval Fresher Learning Program December, 2011.
Special Joins Week 4. Objective –Write SELECT statements to display left, right and full outer joins.
Lab 1 Writing Interactive Queries CISB514 Advanced Database Systems.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
7 Copyright © 2004, Oracle. All rights reserved. Hierarchical Retrieval.
1 Copyright © 2007, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
Oracle 10g Retrieving Data Using the SQL SELECT Statement.
1 Copyright © 2009, Oracle. All rights reserved. Retrieving Data Using the SQL SELECT Statement.
6 Copyright © 2006, Oracle. All rights reserved. Retrieving Data Using Subqueries.
1 Copyright © 2009, Oracle. All rights reserved. B Table Descriptions.
Using Subqueries to Solve Queries
Restricting and Sorting Data
Restricting and Sorting Data
Using Subqueries to Solve Queries
Using the Set Operators
Entity Relationship Diagrams ERDs
Restricting and Sorting Data
Using Subqueries to Solve Queries
Restricting and Sorting Data
Using CASE Value expression
Using Subqueries to Solve Queries
分级取回数据 Schedule: Timing Topic 30 minutes Lecture 20 minutes Practice
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Including Constraints
Restricting and Sorting Data
Presentation transcript:

 After completing this lesson, you should be able to do the following: ◦ Interpret the concept of a hierarchical query ◦ Create a tree-structured report ◦ Format hierarchical data ◦ Exclude branches from the tree structure

De Haan King Hunold EMPLOYEE_ID = 100 (Parent) MANAGER_ID = 100 (Child) Whalen Kochhar Higgins Mourgos Zlotkey RajsDaviesMatos Gietz ErnstLorentz Hartstein Fay AbelTaylor Grant Vargas

WHERE condition: expr comparison_operator expr SELECT [LEVEL], column, expr... FROM table [WHERE condition(s)] [START WITH condition(s)] [CONNECT BY PRIOR condition(s)] ;

◦ Specifies the condition that must be met ◦ Accepts any valid condition  Using the EMPLOYEES table, start with the employee whose last name is Kochhar. Starting Point...START WITH last_name = 'Kochhar' START WITH column1 = value

Walk from the top down, using the EMPLOYEES table. Direction Top down Column1 = Parent Key Column2 = Child Key Bottom up Column1 = Child Key Column2 = Parent Key CONNECT BY PRIOR column1 = column2... CONNECT BY PRIOR employee_id = manager_id

SELECT employee_id, last_name, job_id, manager_id FROM employees START WITH employee_id = 101 CONNECT BY PRIOR manager_id = employee_id ;

SELECT last_name||' reports to '|| PRIOR last_name "Walk Top Down" FROM employees START WITH last_name = 'King' CONNECT BY PRIOR employee_id = manager_id ;

Level 1 root/parent Level 3 parent/child /leaf Level 4 leaf De Haan King Hunold Whalen Kochhar Higgins Mourgos Zlotkey RajsDaviesMatos Gietz ErnstLorentz Hartstein Fay AbelTaylor Grant Vargas Level 2 parent/child

 Create a report displaying company management levels, beginning with the highest level and indenting each of the following levels. COLUMN org_chart FORMAT A12 SELECT LPAD(last_name, LENGTH(last_name)+(LEVEL*2)-2,'_') AS org_chart FROM employees START WITH last_name='King' CONNECT BY PRIOR employee_id=manager_id;

Use the WHERE clause to eliminate a node. Use the CONNECT BY clause to eliminate a branch. WHERE last_name != 'Higgins' CONNECT BY PRIOR employee_id = manager_id AND last_name != 'Higgins' Kochhar Higgins Gietz Whalen Kochhar HigginsWhalen Gietz

 In this lesson, you should have learned the following: ◦ You can use hierarchical queries to view a hierarchical relationship between rows in a table. ◦ You specify the direction and starting point of the query. ◦ You can eliminate nodes or branches by pruning.

 Produce a report showing an organization chart for Mourgos’s department.  Print last names, salaries, and department IDs.

 Create a report that shows the hierarchy of the managers for the employee Lorentz.  Don’t display Lorentz  Display his immediate manager first. Practice 7 (Exercise 2 )

 Create an indented report showing the management hierarchy starting from the employee whose LAST_NAME is Kochhar. Print the employee’s last name, manager ID, and department ID. Practice 7 (Exercise 3)

 Produce a company organization chart that shows the management hierarchy.  Print the employee’s last name, employee ID, manager ID, and job ID.  Exclude all people with a job ID of IT_PROG.  Start with the person at the top level who has no manager id and exclude De Haan and those employees who report to De Haan. QUIZ