CREATE VIEW activeQueryRequirements AS
SELECT q.id, q.type, q.activeVersion, rq.requiredQueryId
FROM queries q
JOIN requiredQueries rq ON q.id = rq.queryId
    AND q.activeVersion >= rq.addedVersion
    AND q.activeVersion < COALESCE(rq.removedVersion, q.activeVersion + 1);