0% found this document useful (0 votes)
2 views5 pages

DBMS Assignment 02 Asim

The document discusses recursive queries in SQL, which allow for querying hierarchical data structures by repeatedly using the results of a query as input for another query until a termination condition is met. It outlines the components of a recursive query, including the anchor query, recursive step, join, and termination condition, along with the advantages and disadvantages of using recursive queries. Additionally, it provides an example of defining a view called SUP_EMP that holds the results of a recursive query to retrieve supervisor-supervisee relationships from an EMPLOYEE table.

Uploaded by

kumarsumair857
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

DBMS Assignment 02 Asim

The document discusses recursive queries in SQL, which allow for querying hierarchical data structures by repeatedly using the results of a query as input for another query until a termination condition is met. It outlines the components of a recursive query, including the anchor query, recursive step, join, and termination condition, along with the advantages and disadvantages of using recursive queries. Additionally, it provides an example of defining a view called SUP_EMP that holds the results of a recursive query to retrieve supervisor-supervisee relationships from an EMPLOYEE table.

Uploaded by

kumarsumair857
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment: 02

DBMS
Name: Muhammad Asim
ID: BAI-24F-602

Subject: Database Management System


Department: Artificial Intelligence
Submitted to: Dr. Sarmad Ahmed Shaikh
RECURSIVE Queries in SQL

Q: What is Recursive Query?

Ans: Recursive queries are a type of query in SQL that allows you to query a
table and then use the results of that query as input for another query,
repeatedly, until a termination condition is meet.

In other words, a recursive query is a query that refers to itself, either


directly or indirectly, to solve a problem. This allows you to query
hierarchical or treelike data structures, such as organizational charts,
family trees, or directory structures.

1. Anchor query:
The recursive query starts with an anchor query, which is a standard SQL
query that retrieves a set of rows.

2. Recursive step:
The anchor query's results are then used as input for the recursive step,
which is another SQL query that references the original table.

3. Join:
The recursive step's results are joined with the original table to produce a
new set of rows.

4. Termination condition:
The recursive query continues to execute until a termination condition is
met, such as reaching a maximum depth or finding no more matching
rows.

Advantages of Recursive Queries in SQL

1. Efficient querying of hierarchical data:


Recursive queries allow you to query complex hierarchical data
structures in a single query, reducing the need for multiple queries or
joins.
2. Simplified data analysis:
Recursive queries can simplify data analysis by allowing you to
traverse complex relationships between data entities.

3. Improved performance:
Recursive queries can be more efficient than using multiple queries or
joins, especially for large datasets.

4. Flexibility:
Recursive queries can be used to query a wide range of hierarchical
data structures, including organizational charts, family trees, and
directory structures.

Disadvantages of Recursive Queries in SQL

1. Complexity:
Recursive queries can be complex and difficult to understand,
especially for large datasets or complex relationships.

2. Performance overhead:
Recursive queries can incur a performance overhead due to the
repeated execution of the recursive step.

3. Risk of infinite recursion:


If the termination condition is not properly defined, recursive queries
can result in infinite recursion, leading to performance issues or errors.

4. Limited support:
Recursive queries are not supported by all database management
systems, and the syntax and features may vary between systems.

5. Debugging challenges:
Recursive queries can be difficult to debug due to the complex
relationships between data entities and the repeated execution of the
recursive step.
Task:
We are defining a view SUP_EMP that will hold the result of the recursive
query. The view is initially empty. It is first loaded with the first level

(supervisor, supervisee) Ssn combinations via the first part

(SELECT Super_ssn, Ssn FROM EMPLOYEE), which is called the base query.
This will be combined via UNION with each successive level of supervisees
through the second part, where the view contents are joined again with the
base values to get the second level combinations, which are UNION with
the first level. This is repeated with successive levels until a fixed point is
reached, where no more tuples are added to the view. At this point, the
result of the recursive query is in the view SUP_EMP.

Query:
WITH RECURSIVE SUP_EMP (SupSsn, EmpSsn) AS (
SELECT Super_ssn, Ssn
FROM EMPLOYEE
UNION
SELECT E.Ssn, S.SupSsn
FROM EMPLOYEE AS E, SUP_EMP AS S
WHERE E.Super_ssn = S.EmpSsn
)
SELECT * FROM

SUP_EMP;
Output:

You might also like