SELECT *
FROM (
-- Single skycells
SELECT
    magicRun.magic_id,
    magicRun.state,
    magicInputSkyfile.node,
    diffSkyfile.diff_id,
    diffSkyfile.uri,
    diffSkyfile.path_base,
    diffSkyfile.fault
FROM magicRun
JOIN magicInputSkyfile
USING(magic_id)
JOIN diffSkyfile
    ON magicInputSkyfile.diff_id = diffSkyfile.diff_id
    AND magicInputSkyfile.node = diffSkyfile.skycell_id
UNION
-- Merged skycells
SELECT
    magicRun.magic_id,
    magicRun.state,
    magicTree.node,
    0,   -- no diff_id
    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
