WITH
    params AS ( /* input parameters */
        SELECT 
           2199023260527 AS id,
           CAST('Lin' AS VARCHAR(40)) AS name
    ),
    friends_1 AS (
        SELECT 1 as "distance", p1."id", p1.firstName, p1.lastName, p1.birthDay, p1.creationDate, p1."gender", p1.browserUsed, p1.locationIP
        FROM persons p1, friends f, params p
        WHERE p.id = f."src" AND f."dst" = p1."id"
    ),
    friends_2 AS (
        SELECT * FROM friends_1
      UNION
        SELECT 2 as "distance", p2."id", p2.firstName, p2.lastName, p2.birthDay, p2.creationDate, p2."gender", p2.browserUsed, p2.locationIP
        FROM persons p2, friends f
        WHERE f."src" IN ( SELECT "id" FROM friends_1 ) AND f."dst" = p2."id" AND p2.id NOT IN ( SELECT "id" FROM friends_1 )
    ),
    friends_3 AS (
        SELECT * FROM friends_2 f WHERE f.firstName = (SELECT name FROM params)
      UNION
        SELECT 3 as "distance", p3."id", p3.firstName, p3.lastName, p3.birthDay, p3.creationDate, p3."gender", p3.browserUsed, p3.locationIP
        FROM persons p3, friends f
        WHERE f."src" IN ( SELECT "id" FROM friends_2 ) AND f."dst" = p3."id" AND p3.id NOT IN ( SELECT "id" FROM friends_2 ) AND p3.firstName = (SELECT name FROM params) 
    ),
    filter AS (
            SELECT * FROM friends_3 f WHERE f.firstName = (SELECT name FROM params)
    )
SELECT * FROM filter ORDER BY "distance";