SELECT *
FROM (
-- Single skycells
SELECT
    magicInputSkyfile.node,
    magicRun.state,
    diffSkyfile.uri,
    diffSkyfile.fault
FROM magicInputSkyfile
JOIN diffSkyfile
    using(diff_id)
JOIN magicRun
    USING(magic_id)
UNION
-- Merged skycells
SELECT
    magicTree.node,
    magicRun.state,
    magicNodeResult.uri,
    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
