SELECT *
FROM (
-- Single skycells
SELECT
    magicRun.magic_id,
    magicRun.state,
    magicInputSkyfile.node,
    diffSkyfile.uri,
    diffSkyfile.path_base,
    diffSkyfile.fault
FROM magicInputSkyfile
JOIN diffSkyfile
    USING(diff_id)
JOIN magicRun
    USING(magic_id)
UNION
-- Merged skycells
SELECT
    magicRun.magic_id,
    magicRun.state,
    magicTree.node,
    magicNodeResult.uri,
    NULL, -- magicNodeResult doesn't have a path_base
    magicNodeResult.fault
FROM magicTree
JOIN magicRun
    USING(magic_id)
JOIN magicNodeResult
    ON magicTree.magic_id = magicNodeResult.magic_id
    AND magicTree.dep = magicNodeResult.node
) as Foo
WHERE
    fault = 0
