Database Programming with SQL

Similar documents
Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

Database Programming with SQL

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Database Programming with SQL

Working with Columns, Characters and Rows. Copyright 2008, Oracle. All rights reserved.

Introduction to Relational Database Concepts. Copyright 2011, Oracle. All rights reserved.

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

Retrieving Data from Multiple Tables

Join, Sub queries and set operators

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

RMOUG Training Days 2018

Cartesian Product and the Join Operations. Copyright 2008, Oracle. All rights reserved.

Oracle Database SQL Basics

Manipulating Data. Copyright 2004, Oracle. All rights reserved.

Recursive Common Table Expressions in Oracle Database 11g Release 2. Iggy Fernandez Database Specialists Session #303

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Database Foundations. 6-9 Joining Tables Using JOIN. Copyright 2014, Oracle and/or its affiliates. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.

Retrieving Data Using the SQL SELECT Statement. Copyright 2004, Oracle. All rights reserved.

DUE: 9. Create a query that will return the average order total for all Global Fast Foods orders from January 1, 2002, to December 21, 2002.

Data Manipulation Language


Database Programming with SQL

Limit Rows Selected. Copyright 2008, Oracle. All rights reserved.

Additional Practice Solutions

RETRIEVING DATA USING THE SQL SELECT STATEMENT

GIFT Department of Computing Science. CS-217/224: Database Systems. Lab-5 Manual. Displaying Data from Multiple Tables - SQL Joins

Táblák tartalmának módosítása. Copyright 2004, Oracle. All rights reserved.

DEFAULT Values, MERGE, and Multi-Table Inserts. Copyright 2009, Oracle. All rights reserved.

Bsc (Hons) Software Engineering. Examinations for / Semester 1. Resit Examinations for BSE/15A/FT & BSE/16A/FT

Oracle Database 11g: SQL Fundamentals I COPYRIGHTED MATERIAL. PArt

School of Computing and Information Technology. Examination Paper Autumn Session 2017

Database Programming with PL/SQL

Intermediate SQL: Aggregated Data, Joins and Set Operators

Oracle Database: SQL Fundamentals I. Oracle Internal & Oracle Academy Use Only. Volume II Student Guide. D64258GC10 Edition 1.0 January 2010 D65028

Creating Other Schema Objects

Course Overview. Copyright 2010, Oracle and/or its affiliates. All rights reserved.

Introduction to Oracle

CS 275 Winter 2011 Problem Set 3

chapter 2 G ETTING I NFORMATION FROM A TABLE

Architecture. Architecture. Introduction to Oracle 10g Express Edition. Help

2. What privilege should a user be given to create tables? The CREATE TABLE privilege

Conversion Functions

Assignment Grading Rubric

Getting Information from a Table

THE SCHOOL OF COMPUTING AND TECHNOLOGY EASTERN MEDITERRANEAN UNIVERSITY

Using DbVisualizer Variables

KORA. RDBMS Concepts II

Oracle Database 10g: SQL Fundamentals I

Database Programming with SQL 5-1 Conversion Functions. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

School of Computing and Information Technology Session: Spring CSCI835 Database Systems (Bridging Subject) Sample class test 23 July 2018

Database Programming with PL/SQL

Using the Set Operators. Copyright 2004, Oracle. All rights reserved.

Database Design. 2-3 Entity Relationship Modeling and ERDs. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

Oracle MOOC: SQL Fundamentals

Displaying Data from Multiple Tables. Copyright 2004, Oracle. All rights reserved.

Introductory SQL SQL Joins: Viewing Relationships Pg 1

BackLogic Quick-Start Tutorial

RESTRICTING AND SORTING DATA

EXISTS NOT EXISTS WITH

Oracle Database 11gR2 Optimizer Insights

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Introduction to Oracle9i: SQL

Oracle Database 10g: Introduction to SQL

MANAGING DATA(BASES) USING SQL (NON-PROCEDURAL SQL, X401.9)

What Are Group Functions? Reporting Aggregated Data Using the Group Functions. Objectives. Types of Group Functions

Introduction to Explicit Cursors. Copyright 2008, Oracle. All rights reserved.

Sun Certified MySQL Associate

CSE 530A SQL. Washington University Fall 2013

Oracle Database. 2 Day Developer's Guide, 11g Release 1 (11.1) B

Oracle Database 10g: SQL Fundamentals II

Oracle Syllabus Course code-r10605 SQL

ÇALIŞMA TEST SORULARI

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

BraindumpsVCE. Best vce braindumps-exam vce pdf free download

1z Exam Code: 1z Exam Name: Oracle Database SQL Expert

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Alkérdések II. Copyright 2004, Oracle. All rights reserved.

Graphical Joins in More Detail

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Database Programming with PL/SQL

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Apex 5.1 Interactive Grid and Other New features

Fravo.com. Certification Made Easy. World No1 Cert Guides. Introduction to Oracle9i: SQL Exam 1Z Edition 1.0

Power Up Your Apps with Recursive Subquery Factoring. Jared Still 2014

Exam : 1Z Title : Introduction to Oracle9i: SQL

CONCAT SUBSTR LOWER (*) All three will be evaluated simultaneously. Correct

HR Database. Sample Output from TechWriter 2007 for Databases

CS2 Current Technologies Lecture 2: SQL Programming Basics

Transcription:

Database Programming with SQL 6-4

Objectives This lesson covers the following objectives: Construct and execute a SELECT statement to join a table to itself using a self-join Interpret the concept of a hierarchical query Create a tree-structured report Format hierarchical data Exclude branches from the tree structure 3

Purpose In data modeling, it was sometimes necessary to show an entity with a relationship to itself. For example, an employee can also be a manager. We showed this using the recursive or "pig's ear" relationship. EMPLOYEE # Employee ID * First Name * Last Name * Hire Date * Salary O Commission managed by manager of 4

Purpose Once we have a real employees table, a special kind of join called a self-join is required to access this data. A self-join is use to join a table to itself as if it was two tables. SELECT worker.last_name ' works for ' manager.last_name AS "Works for" FROM employees worker JOIN employees manager ON (worker.manager_id = manager.employee_id); 5

SELF-JOIN To join a table to itself, the table is given two names or aliases. This will make the database "think" that there are two tables. EMPLOYEES (worker) employee_id last_name 100 King 101 Kochar 100 102 De Haan 100 103 Hunold 102 104 Ernst 103 107 Lorentz 103 124 Mourgos 100 manager_id EMPLOYEES (manager) employee_id 100 King last_name 101 Kochar 102 De Haan 103 Hunold 104 Ernst 107 Lorentz 124 Mourgos Manager_id in the worker table is equal to employee_id in the manager table. 6

SELF-JOIN Choose alias names that relate to the data's association with that table. EMPLOYEES (worker) employee_id last_name manager_id EMPLOYEES (manager) employee_id last_name 100 King 101 Kochar 100 102 De Haan 100 103 Hunold 102 104 Ernst 103 107 Lorentz 103 124 Mourgos 100 100 King 101 Kochar 102 De Haan 103 Hunold 104 Ernst 107 Lorentz 124 Mourgos Manager_id in the worker table is equal to employee_id in the manager table. 7

SELF-JOIN Example SELECT worker.last_name, worker.manager_id, manager.last_name AS "Manager name" FROM employees worker JOIN employees manager ON (worker.manager_id = manager.employee_id); LAST_NAME MANAGER_ID Manager name Hunold 102 De Haan Fay 201 Hartstein Gietz 205 Higgins Lorentz 103 Hunold Ernst 103 Hunold Zlotkey 100 King Mourgos 100 King 8

Hierarchical Queries Closely related to self-joins are hierarchical queries. On the previous pages, you saw how you can use self-joins to see each employee's direct manager. With hierarchical queries, we can also see who that manager works for, and so on. With this type of query, we can build an Organization Chart showing the structure of a company or a department. 9

Hierarchical Queries Imagine a family tree with the eldest members of the family found close to the base or trunk of the tree and the youngest members representing branches of the tree. Branches can have their own branches, and so on. 10

Using Hierarchical Queries Using hierarchical queries, you can retrieve data based on a natural hierarchical relationship between rows in a table. A relational database does not store records in a hierarchical way. However, where a hierarchical relationship exists between the rows of a single table, a process called tree walking enables the hierarchy to be constructed. A hierarchical query is a method of reporting the branches of a tree in a specific order. 11

Hierarchical Queries Data Examine the sample data from the EMPLOYEES table below, and look at how you can manually make the connections to see who works for whom starting with Steven King and moving through the tree from there. EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMM_PCT MGR_ID DEPT_ID 100 Steven King SKING 515.123.4567 17-JUN-1987 AD_PRES 24000 (null) (null) 90 101 Neena Kochhar NKOCHHAR 515.123.4568 21-SEP-1989 AD_VP 17000 (null) 100 90 102 Lex De Haan LDEHAAN 515.123.4569 13-JAN-1993 AD_VP 17000 (null) 100 90 103 Alexander Hunold AHUNOLD 590.423.4567 03-JAN-1990 IT_PROG 9000 (null) 102 60 104 Bruce Ernst BERNST 590.423.4568 21-MAY-1991 IT_PROG 6000 (null) 103 60 124 Kevin Mourgos KMOURGOS 650.123.5234 16-NOV-1999 ST_MAN 5800 (null) 100 50 141 Trenna Rajs TRAJS 650.121.8009 17-OCT-1995 ST_CLERK 3500 (null) 124 50 12

Hierarchical Queries Illustrated The organization chart that we can draw from the data in the EMPLOYEES table will look like this: King Kochar De Haan Mourgos Hunold Rajs Ernst 13

Hierarchical Queries Keywords Hierarchical queries have their own new keywords: START WITH, CONNECT BY PRIOR, and LEVEL. START WITH identifies which row to use as the Root for the tree it is constructing, CONNECT BY PRIOR explains how to do the inter-row joins, and LEVEL specifies how many branches deep the tree will traverse. 14

Hierarchical Queries Keyword Example SELECT employee_id, last_name, job_id, manager_id FROM employees START WITH employee_id = 100 CONNECT BY PRIOR employee_id = manager_id EMPLOYEE_ID LAST_NAME JOB_ID MANAGER_ID 100 King AD_PRES - 101 Kochhar AD_VP 100 200 Whalen AD_ASST 101 205 Higgins AC_MGR 101 206 Gietz AC_ACCOUNT 205 102 De Haan AD_VP 100 103 Hunold IT_PROG 102 104 Ernst IT_PROG 103 15

Hierarchical Queries Another Example SELECT last_name ' reports to ' PRIOR last_name AS "Walk Top Down" FROM employees START WITH last_name = 'King' CONNECT BY PRIOR employee_id = manager_id; King reports to Kochhar reports to King Whalen reports to Kochhar Higgins reports to Kochhar Gietz reports to Higgins De Haan reports to King Hunold reports to De Haan Ernst reports to Hunold Walk Top Down 16

Hierarchical Queries Level Example SELECT LEVEL LEVEL, is a pseudo-column last_name ' reports used to with ' hierarchical PRIOR last_name queries, and AS "Walk Top Down" FROM it counts employees the number of steps it has taken from the root of START the tree. WITH last_name = 'King' CONNECT BY PRIOR employee_id=manager_id; LEVEL Walk Top Down 1 King reports to 2 Kochhar reports to King 3 Whalen reports to Kochhar 3 Higgins reports to Kochhar 4 Gietz reports to Higgins 2 De Haan reports to King 3 Hunold reports to De Haan 4 Ernst reports to Hunold 17

Hierarchical Query Report If you wanted to create a report displaying company management levels, beginning with the highest level and indenting each of the following levels, then this would be easy to do using the LEVEL pseudo column and the LPAD function to indent employees based on their level. 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; 18

Hierarchical Query Output Levels As you can see in the result on the right, each row is indented by two underscores per level. 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; Org_Chart King Kochhar Whalen Higgins Gietz De Haan Hunold Ernst Lorentz Rajs Davies Matos Vargas Zlotkey Abel Taylor Grant Hartstein Fay 19

Bottom Up Hierarchical Query As you can see in the result below, this example shows how to create a Bottom Up Hierarchical Query by moving the keyword PRIOR to after the equals sign, and using 'Grant' in the START WITH clause. SELECT LPAD(last_name, LENGTH(last_name) + (LEVEL*2)-2, '_') AS ORG_CHART FROM employees START WITH last_name = 'Grant' ORG_CHART CONNECT BY employee_id = PRIOR manager_id Grant Zlotkey King 20

Hierarchical Queries Pruning Pruning branches from the tree can be done using either the WHERE clause or the CONNECT BY PRIOR clause. If the WHERE clause is used, only the row named in the statement is excluded; if the CONNECT BY PRIOR clause is used, the entire branch is excluded. 21

Hierarchical Queries Pruning For example, if you want to exclude a single row from your result, you would use the WHERE clause to exclude that row; however, in the result, it would then look like Gietz worked directly for Kochhar, which he does not. SELECT last_name FROM employees WHERE last_name!= 'Higgins' START WITH last_name = 'Kochhar' CONNECT BY PRIOR employee_id = manager_id; Whalen Kochhar Higgins Gietz 22

Hierarchical Queries Pruning If, however, you wanted to exclude one row and all the rows below that one, you should make the exclusion part of the CONNECT BY statement. In this example that excludes Higgins, we are also excluding Gietz in the result. SELECT last_name FROM employees START WITH last_name = 'Kochhar' CONNECT BY PRIOR employee_id = manager_id AND last_name!= 'Higgins'; Whalen Kochhar Higgins Gietz 23

Terminology Key terms used in this lesson included: Connect By prior Hierarchical queries Level Self join Start with Tree Structure Tree Walking Branches 24

Summary In this lesson, you should have learned how to: Construct and execute a SELECT statement to join a table to itself using a self-join Interpret the concept of a hierarchical query Create a tree-structured report Format hierarchical data Exclude branches from the tree structure 25