0% found this document useful (0 votes)
29 views9 pages

BasicRecursion Annotated

The document discusses recursion in SQL. It provides examples of using recursive SQL queries to find all ancestors of a person, calculate the total salary cost of a project by traversing the company hierarchy, and find the cheapest flight route between two airports by considering connecting flights. It also explains the basic structure of the SQL WITH recursive statement for defining recursive queries in SQL.

Uploaded by

asim_ubit
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views9 pages

BasicRecursion Annotated

The document discusses recursion in SQL. It provides examples of using recursive SQL queries to find all ancestors of a person, calculate the total salary cost of a project by traversing the company hierarchy, and find the cheapest flight route between two airports by considering connecting flights. It also explains the basic structure of the SQL WITH recursive statement for defining recursive queries in SQL.

Uploaded by

asim_ubit
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Recursion in SQL

Basic recursive WITH statement


Jennifer Widom

SQL is not a Turing complete language

Basic SQL Recursion

Simple, convenient, declarative Expressive enough for most database queries But basic SQL cant express unbounded computations

Jennifer Widom

Example 1: Ancestors
ParentOf(parent,child)

Basic SQL Recursion

Find all of Marys ancestors

Jennifer Widom

Example 2: Company hierarchy


Employee(ID,salary) Manager(mID,eID) Project(name,mgrID)

Basic SQL Recursion

Find total salary cost of project X

Jennifer Widom

Example 3: Airline flights


Flight(orig,dest,airline,cost)

Basic SQL Recursion

Find cheapest way to fly from A to B

Jennifer Widom

SQL With Statement

Basic SQL Recursion

With R1 As (query-1), R2 As (query-2), ... Rn As (query-n) <query involving R1,,Rn (and other tables)>

Jennifer Widom

SQL With Statement

Basic SQL Recursion

With R1(A1,A2,,Am) As (query-1), R2 As (query-2), ... Rn As (query-n) <query involving R1,,Rn (and other tables)>

Jennifer Widom

SQL With Recursive Statement

Basic SQL Recursion

With Recursive R1 As (query-1), R2 As (query-2), ... Rn As (query-n) <query involving R1,,Rn (and other tables)>

Jennifer Widom

SQL With Recursive Statement


With Recursive R As ( base query Union recursive query ) <query involving R (and other tables)>

Basic SQL Recursion

Jennifer Widom

You might also like