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.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

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 Writing Correlated Subqueries.
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.
Logical Operators Operator AND OR NOT Meaning Returns TRUE if both component conditions are TRUE Returns TRUE if either component condition is TRUE Returns.
SELECT Advanced. Sorting data in a table The ORDER BY clause is used for sorting the data in either ascending or descending order depending on the condition.
SQL - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
4-1 Copyright  Oracle Corporation, All rights reserved. Data Manipulation Language (DML)
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
2 Writing Basic SELECT Statements. 1-2 Copyright  Oracle Corporation, All rights reserved. Capabilities of SQL SELECT Statements Selection Projection.
Copyright  Oracle Corporation, All rights reserved. Writing Basic SQL Statements.
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.
SELECT Statements Lecture Notes Sree Nilakanta Fall 2010 (rev)
Copyright س Oracle Corporation, All rights reserved. I Introduction.
RELATSIOONILISED ANDMEBAASID(alg) SQLi VÕIMALUSED.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
SQL- DQL (Oracle Version). 2 SELECT Statement Syntax SELECT [DISTINCT] column_list FROM table_list [WHERE conditional expression] [GROUP BY column_list]
1 JOIN SUBQUERY Structured Query Language (SQL) - Part III.
Session 2: SQL (A): Parts 1 and 2 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
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.
Structured Query Language
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
I-1 Copyright س Oracle Corporation, All rights reserved. Data Retrieval.
Displaying Data from Multiple Tables (SQL99 Syntax with examples)
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
CSCI N311: Oracle Database Programming 4-1 Chapter 12: When One Query Depends Upon Another Correlated Subqueries. Subqueries in the where clause. Anti-Joins.
An Introduction To SQL Part 2 (Special thanks to Geoff Leese)
1 Information Retrieval and Use (IRU) An Introduction To SQL Part 2.
Copyright س Oracle Corporation, All rights reserved. I Introduction.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
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.
ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Collection Operators These slides are.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Copyright س Oracle Corporation, All rights reserved. 12 Creating Views.
19 Copyright © Oracle Corporation, All rights reserved. Hierarchical Retrieval.
Hierarchical Retrieval Fresher Learning Program December, 2011.
1-1 Copyright  Oracle Corporation, All rights reserved. Logging In to SQL*Plus From Windows environment:From Windows environment: From command line:From.
Copyright  Oracle Corporation, All rights reserved. 2 Restricting and Sorting Data.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Writing Basic SQL Statements. Objectives After completing this lesson, you should be able to do the following: –List the capabilities of SQL SELECT statements.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
7 Copyright © 2004, Oracle. All rights reserved. Hierarchical Retrieval.
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.
Chapter 1 Introduction.
SQL Creating and Managing Tables
Retrieval Queries in SQL(DML)
Writing Correlated Subqueries
SQL Creating and Managing Tables
جملة الاستعلام الأساسية
What Is a View? EMPNO ENAME JOB EMP Table EMPVU10 View
SQL Creating and Managing Tables
Chapter 1 Introduction.
Chapter 1 Introduction.
Restricting and Sorting Data
分级取回数据 Schedule: Timing Topic 30 minutes Lecture 20 minutes Practice
IST 318 Database Administration
Integrity Constraints
Database Programming Using Oracle 11g
Presentation transcript:

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 UA 1400 UA 1500 DL 1530 DL 1900 UA 1830 DL 1500 LAS DL 852 Example. A recursive SQL query (not implemented in Oracle 11g) WITH RECURSIVE Reaches(Source_City, Dest_City) AS (SELECT Source_City, Dest_City FROM Flights) UNION (SELECT R1.Source_City, R2.Dest_City FROM Reaches R1, Reaches R2 WHERE R1.Dest_City = R2.Source_City)

3 Recursion in SQL – Hierarchical Queries n Start With – Connect By [Prior] n The start with-connect by prior clause can be used to select data with hierarchical relationships  usually some sort of parent  child (supervisor  supervisee, or part  subparts) relationships involved n Example. Given the schema parent(Pname, Cname), retrieve all the descendents of “Joe Young.” SELECT Cname FROM Parent START WITH Pname = ‘Joe Young’ CONNECT BY PRIOR Cname = Pname;

4 Recursion in SQL – Hierarchical Queries n Start With – Connect By [Prior]  Using start with–connect by [prior] to answer hierarchical queries on a table. n Start With – indicates root node. n Connect by – indicates relationships between parent and child rows within the hierarchy. n Prior - keyword indicates the parent. n LEVEL – pseudocolumn function that returns 1 for root, 2 for children of root, 3 for next child level, etc. Level 1 Level 2 Level 3 Level 4

5 Hierarchical Queries in SQL – Start With n The Start With clause identifies the row(s) to be used as the root(s) of a hierarchical query. n The clause specifies a condition that the roots must satisfy. n If this clause is omitted, SQL uses all rows in the table as root rows. n A Start With condition can contain a subquery, whereas a Connect By condition cannot contain a subquery. n Example. SELECT PRIOR Cname FROM Parent START WITH Pname = ‘Joe Young’ …

6 Hierarchical Queries in SQL – Connect By n The Connect By clause specifies a relationship (condition) between parent and child rows in a hierarchical query. n Some part of the condition must use the Prior operator to refer to the parent row. The part of the condition containing the Prior operator must have one of the following forms: PRIOR expr comparison_operator expr, or expr comparison_operator PRIOR expr n Example. Given the relation Emp(Emp#, Mgr#, Sal, Comm), the following Prior operator applies only to the Emp# of the parent row and mgr, sal, and comm values for the child row: CONNECT BY PRIOR Emp# = mgr# AND sal > comm; n If the Connect By clause results in a loop in the hierarchy, SQL returns an error. A loop occurs if one row is both the parent and a child of another row.

7 Hierarchical Queries in SQL – Connect By n In Oracle 11g, CONNECT BY could not be used in a query that contains a join n Example. Given the following relation schemas: Emp(EID, Ename, DNO, MgrID) Dept(DNO, Dname) the following query is invalid in Oracle 11g: SELECT Ename, MgrID, Dname FROM Emp E, Dept D WHERE E.DNO = D.DNO START WITH Ename = ‘King’ CONNECT BY PRIOR Mgr_ID = EID

8 Hierarchical Queries in SQL – Where Clauses n SQL evaluates the Where-clause condition for each row individually n Where clause - restricts the rows returned by the query without affecting other rows of the hierarchy. n If a row does not satisfy the condition specified in the Where clause of the query, SQL  eliminates it from the hierarchy, but  does not remove all the children (and their descendents) of the row, which can be accomplished by specifying the condition in the CONNECT BY clause n Example. SELECT Cname FROM Parent WHERE Cname != ‘Peter Pan’ START WITH Pname = ‘Joe Young’ CONNECT BY PRIOR Cname = Pname;

9 Hierarchical Queries in SQL – Level Number n Nodes in a tree are assigned level numbers, depending on how far removed they are from the root of the tree. n LEVEL is a pseudo-column, which can be specified in an SQL statement where a column name may appear. n Example. Given the schema use(Part#, Sub-part#, Quantity), retrieve all the subparts used by Part P 1, with layers identified. SELECT LEVEL AS Layer, Part#, Sub-part# FROM use START WITH Part# = ‘P 1 ’ CONNECT BY PRIOR Sub-part# = Part#;

10 Hierarchical Queries in SQL – To_Number n Convert a character value containing a number to a number n Format: To_number(string-expression)  The string expression to be converted can be the name of a column, a string literal, or the result of another function, where the underlying data type is of type CHAR. n Example. Use To_number to convert a string to a number before inserting it in the database: UPDATE employees SET salary = salary + To_Number(' ') WHERE Lname = 'Williams‘;

11 Hierarchical Queries in SQL – Order By n Specifies the sorting of rows in a result set. n Format: ORDER BY ordering-item [ASC | DESC] {, ordering-item [ASC | DESC]...}  Ordering-item: an expression that determines the sort order. The clause can contain one or more ordering items.  ASC: sorting in ascending order (default).  DESC: sorting in descending order. n Example. SELECT Dept_no, Emp_name, Job_Title FROM Employees ORDER BY Dept_no DESC