(SELECT sa.source FROM src_associations sa)
AND s.id NOT IN
(SELECT b.source FROM binaries b)
+ AND s.id NOT IN (SELECT esr.src_id FROM extra_src_references esr)
AND f.id NOT IN
(SELECT bqf.fileid FROM build_queue_files bqf)""")
SELECT f.id, f.filename FROM source s, files f, dsc_files df
WHERE f.last_used IS NOT NULL AND s.id = df.source AND df.file = f.id
AND ((EXISTS (SELECT 1 FROM src_associations sa WHERE sa.source = s.id))
+ OR (EXISTS (SELECT 1 FROM extra_src_references esr WHERE esr.src_id = s.id))
OR (EXISTS (SELECT 1 FROM binaries b WHERE b.source = s.id))
OR (EXISTS (SELECT 1 FROM build_queue_files bqf WHERE bqf.fileid = s.file)))""")