DBMS Assignment 02 Asim
DBMS Assignment 02 Asim
DBMS
Name: Muhammad Asim
ID: BAI-24F-602
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.
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.
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.
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.
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
(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: