©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Recursive Queries.

Slides:



Advertisements
Similar presentations
Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 5: Other Relational Languages Database System Concepts, 5th Ed. ©Silberschatz, Korth and.
Advertisements

5.1Database System Concepts - 6 th Edition Chapter 5: Advanced SQL Advanced Aggregation Features OLAP.
E-R Diagram for a Banking Enterprise
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
1 1 Data Warehousing Decision-Support Systems  Data Analysis  OLAP  Extended aggregation features in SQL –Windowing and ranking  Implementation Techniques.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 SQL:1999 Advanced Querying Decision-Support Systems Data Warehousing Data.
©Silberschatz, Korth and Sudarshan22.1Database System Concepts 4 th Edition 1 Extended Aggregation SQL-92 aggregation quite limited  Many useful aggregates.
Copyright: Silberschatz, Korth and Sudarshan 1 OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts - 6 th Edition SQL Schema Changes and table updates instructor teaches.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Advanced SQL.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Advanced SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Lecture 10 of 42 Tuesday, 06 February 2007.
Computing & Information Sciences Kansas State University Monday, 26 Nov 2007CIS 560: Database System Concepts Lecture 37 of 42 Monday, 26 November 2007.
©Silberschatz, Korth and Sudarshan1Database System Concepts - 6 th Edition E-R Diagram for a University Enterprise.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Advanced SQL.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
©Silberschatz, Korth and Sudarshan18.1Database System Concepts - 5 th Edition, Aug 26, 2005 Extended Aggregation in SQL:1999 The cube operation computes.
Computing & Information Sciences Kansas State University Wednesday, 12 Nov 2008CIS 560: Database System Concepts Lecture 31 of 42 Wednesday, 12 November.
Computing & Information Sciences Kansas State University Thursday, 08 Feb 2007CIS 560: Database System Concepts Lecture 11 of 42 Thursday, 08 February.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Computing & Information Sciences Kansas State University Wednesday, 29 Nov 2006CIS 560: Database System Concepts Lecture 39 of 42 Wednesday, 29 November.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Appendix C: Other Relational.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Chapter 5-2: Advanced SQL. 2 Join Expressions Views Integrity Constraints SQL Data Types and Schemas Authorization Accessing SQL From a Programming Language.
Computing & Information Sciences Kansas State University Wednesday, 17 Sep 2008CIS 560: Database System Concepts Lecture 9 of 42 Wednesday, 18 September.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Module B: Advanced SQL.
1 CS 430 Database Theory Winter 2005 Lecture 5: Relational Algebra.
Data warehousing, data analysis and OLAP Sunita Sarawagi
05 | SET Operators, Windows Functions, and Grouping Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
Random Query Generator for Hive November 2015 Hive Contributor Meetup Szehon Ho.
1 SQL II CIS*2450 Advanced Programming Concepts. 2 Data Types INTEGER –numbers without a decimal point –range is to SMALLINT –like.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts - 6 th Edition Chapter 4: Intermediate SQL.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
A Glance at the Window Functions. Window Functions Introduced in SQL 2005 Enhanced in SQL 2012 So-called because they operate on a defined portion of.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Data Analysis Decision Support Systems Data Analysis and OLAP Data Warehousing.
VICTORIA UNIVERSITY OF WELLINGTON Te Whare Wananga o te Upoko o te Ika a Maui SWEN 432 Advanced Database Design and Implementation OLAP Queries and SQL:1999.
Chapter 5: Advanced SQL.
Chapter 3 Introduction to SQL(3)
Database Systems Subqueries, Aggregation
Data Analysis with SQL Window Functions
Chapter 5: Advanced SQL Database System concepts,6th Ed.
Using Window Ranking, Offset, and Aggregate Functions
Chapter 4: Intermediate SQL Joins
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
Chapter 5: Advanced SQL.
Lecture#7: Fun with SQL (Part 2)
T-SQL Window Function Deep Dive part 1
Writing Better Queries with Window Functions
Oracle8i Analytical SQL Features
Chapter 4 Summary Query.
Introduction to Window Functions
Chapter 5: Advanced SQL.
Data Analysis with SQL Window Functions
Introduction to T-sql Window functionS
Chapter 5: Advanced SQL.
CS240B: Assignment1 Winter 2016.
OLAP Functions Order-Dependent Aggregates and Windows in SQL: SQL: same as SQL:1999.
Presentation transcript:

©Silberschatz, Korth and Sudarshan5.1Database System Concepts - 6 th Edition Recursive Queries

Recursion in SQL SQL:1999 permits recursive view definition Example: find which courses are a prerequisite, whether directly or indirectly, for a specific course Course_idPrereq_id CS-347CS-290 CS-347CS-250 CS-290CS-190 CS-290CS-170 CS-250CS-101 CS-190CS-101 CS-190CS-107 CS-347CS-101 CS-319CS-101

Recursion in SQL SQL Statement with recursive rec_prereq(course_id, prereq_id) as ( select course_id, prereq_id from prereq union select rec_prereq.course_id, prereq.prereq_id, from rec_rereq, prereq where rec_prereq.prereq_id = prereq.course_id ) select ∗ from rec_prereq; This example view, rec_prereq, is called the transitive closure of the prereq relation } Base Query } Recursive Query

The Power of Recursion Recursive views make it possible to write queries, such as transitive closure queries, that cannot be written without recursion or iteration. Intuition: Without recursion, a non-recursive non-iterative program can perform only a fixed number of joins of prereq with itself  This can give only a fixed number of levels of managers  Given a fixed non-recursive query, we can construct a database with a greater number of levels of prerequisites on which the query will not work  Alternative: write a procedure to iterate as many times as required – See procedure findAllPrereqs in book

The Power of Recursion Computing transitive closure using iteration, adding successive tuples to rec_prereq Each step of the iterative process constructs an extended version of rec_prereq from its recursive definition. The final result is called the fixed point of the recursive view definition. Recursive views are required to be monotonic. That is, if we add tuples to prereq the view rec_prereq contains all of the tuples it contained before, plus possibly more

Recursion Example: trace CS-347 Course_idPrereq_id CS-347CS-290 CS-347CS-250 CS-290CS-190 CS-290CS-170 CS-250CS-101 CS-190CS-101 CS-190CS-107 CS-315CS-101 CS-319CS-101 Course_idPrereq_id CS-347CS-290 CS-347CS-250 First Iteration Course_idPrereq_id CS-347CS-290 CS-347CS-250 CS-347CS-190 CS-347CS-170 CS-347CS-101 Second Iteration with recursive rec_prereq(course_id, prereq_id) as ( select course_id, prereq_id from prereq union select rec_prereq.course_id, prereq.prereq_id, from rec_rereq, prereq where rec_prereq.prereq_id = prereq.course_id ) select ∗ from rec_prereq;

Recursion Example Course_idPrereq_id CS-347CS-290 CS-347CS-250 CS-290CS-190 CS-290CS-170 CS-250CS-101 CS-190CS-101 CS-190CS-107 CS-315CS-101 CS-319CS-101 Course_idPrereq_id CS-347CS-290 CS-347CS-250 CS-347CS-190 CS-347CS-170 CS-347CS-107 CS-347CS-101 Third Iteration Course_idPrereq_id CS-347CS-290 CS-347CS-250 CS-347CS-190 CS-347CS-170 CS-347CS-107 CS-347CS-101 Fourth Iteration Fix point

©Silberschatz, Korth and Sudarshan5.8Database System Concepts - 6 th Edition Advanced Aggregation Features

Ranking Suppose we are given a relation student_grades(ID, GPA) giving the grade-point average of each student Find the rank of each student with the class. IDGPA

Ranking Find the rank of each student with the class. A brute force solution. Ranking can be done using basic SQL aggregation select ID, (1 + (select count(*) from student_grades B where B.GPA > A.GPA)) as s_rank from student_grades A order by s_rank; but resultant query is cumbersome and very inefficient!

Ranking Ranking is done in conjunction with an order by specification. select ID, rank() over (order by GPA desc) as s_rank from student_grades IDs_rank Ranking result IDGPA Not sorted!

Ranking An extra order by clause is needed to get them in sorted order select ID, rank() over (order by GPA desc) as s_rank from student_grades order by s_rank IDs_rank Ranking result IDGPA

Ranking Ranking may leave gaps: e.g. if 2 students have the same top GPA, both have rank 1, and the next rank is 3 dense_rank does not leave gaps, so next dense rank would be 2 select ID, dense_rank() over (order by GPA desc) as s_rank from student_grades IDs_rank Ranking result IDGPA

Ranking (Cont.) Ranking can be done within partition of the data. Suppose we are given a relation student_grades(ID, dept_name, GPA) giving the grade-point average of each student and each student’s department affiliation. Find the rank of students within each department. select ID, dept_name, rank () over (partition by dept_name order by GPA desc) as dept_rank from dept_grades order by dept_name, dept_rank;

Ranking (Cont.) Multiple rank clauses can occur in a single select clause. Ranking is done after applying group by clause/aggregation Can be used to find top-n results More general than the limit n clause supported by many databases, since it allows top-n within each partition select ID, GPA from student_grades order by GPA limit 10;

Ranking (Cont.) Other ranking functions: percent_rank (within partition, if partitioning is done) cume_dist (cumulative distribution)  fraction of tuples with preceding values row_number (non-deterministic in presence of duplicates) How about them NULLs? Since SQL:1999 the user can specify nulls first or nulls last select ID, rank ( ) over (order by GPA desc nulls last) as s_rank from student_grades

Ranking (Cont.) For a given constant n, the ranking the function ntile(n) takes the tuples in each partition in the specified order, and divides them into n buckets with equal numbers of tuples. E.g., select ID, ntile(4) over (order by GPA desc) as quartile from student_grades; What does this query do?

Windowing Used to smooth out random variations. E.g., moving average: “Given sales values for each date, calculate for each date the average of the sales on that day, the previous day, and the next day” Window specification in SQL: Given relation sales(date, value) select date, sum(value) over (order by date between rows 1 preceding and 1 following) from sales

Windowing Examples of other window specifications: between rows unbounded preceding and current rows unbounded preceding range between 10 preceding and current row  All rows with values between current row value –10 to current value range interval 10 day preceding  Not including current row

Windowing (Cont.) Can do windowing within partitions E.g., Given a relation transaction (account_number, date_time, value), where value is positive for a deposit and negative for a withdrawal “Find total balance of each account after each transaction on the account” select account_number, date_time, sum (value) over (partition by account_number order by date_time rows unbounded preceding) as balance from transaction order by account_number, date_time