How to get the third level of a tree in SQL

I aim to list the forth generation of a family using MS SQL.

Example family tree in the picture (id based)

FAMILY TREE

The Tables I have are:

  1. Family
    +------+-----------------+
    | f_id | f_name          |
    +------+-----------------+
    | 1    | Name Surname 1  |
    +------+-----------------+
    | 2    | Name Surname 2  |
    +------+-----------------+
    | 3    | Name Surname 3  |
    +------+-----------------+
    | 4    | Name Surname 4  |
    +------+-----------------+
    | 5    | Name Surname 5  |
    +------+-----------------+
    | 6    | Name Surname 6  |
    +------+-----------------+
    | 7    | Name Surname 7  |
    +------+-----------------+
    | 8    | Name Surname 8  |
    +------+-----------------+
    | 9    | Name Surname 9  |
    +------+-----------------+
    | 10   | Name Surname 10 |
    +------+-----------------+
  1. Relations
    +----------+---------+
    | r_parent | r_child |
    +----------+---------+
    | 1        | 2       |
    +----------+---------+
    | 1        | 3       |
    +----------+---------+
    | 2        | 4       |
    +----------+---------+
    | 2        | 5       |
    +----------+---------+
    | 3        | 6       |
    +----------+---------+
    | 3        | 7       |
    +----------+---------+
    | 3        | 8       |
    +----------+---------+
    | 7        | 9       |
    +----------+---------+
    | 8        | 10      |
    +----------+---------+

Needed result is

9 Name Surname 10 Name Surname



Read more here: https://stackoverflow.com/questions/64898865/how-to-get-the-third-level-of-a-tree-in-sql

Content Attribution

This content was originally published by mesut m at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: