WITH
permitted as (
SELECT
folder_id
FROM
permissions
WHERE
user_id = $1
)
folder_id, owner_id, folder_name, created_at
folders
folder_id IN (
permitted
AND parent_folder_id NOT IN (