-- Collection of code snippets by Arne Vajhøj 
-- posted to eksperten.dk, usenet and other places (2002-now) 
CREATE TABLE t (
    gruppe_id INTEGER,
    bruger_id INTEGER,
    PRIMARY KEY (gruppe_id,bruger_id)
);

INSERT INTO t VALUES(1,1);
INSERT INTO t VALUES(1,2);
INSERT INTO t VALUES(2,1);
INSERT INTO t VALUES(2,2);
INSERT INTO t VALUES(2,3);
INSERT INTO t VALUES(3,1);
INSERT INTO t VALUES(3,2);
INSERT INTO t VALUES(3,3);
INSERT INTO t VALUES(3,4);
INSERT INTO t VALUES(4,1);
INSERT INTO t VALUES(4,2);
INSERT INTO t VALUES(4,3);
INSERT INTO t VALUES(4,4);
INSERT INTO t VALUES(4,5);
INSERT INTO t VALUES(5,2);
INSERT INTO t VALUES(5,3);

SELECT t1.gruppe_id,t1.bruger_id
FROM (t t1 JOIN t t2 ON t2.gruppe_id=t1.gruppe_id)
     LEFT JOIN t t3 ON t3.gruppe_id=t1.gruppe_id AND t3.bruger_id<>t1.bruger_id AND t3.bruger_id<>t2.bruger_id
WHERE t3.bruger_id IS NULL AND t1.bruger_id IN (1,2) AND t2.bruger_id IN (1,2);

SELECT t1.gruppe_id,t1.bruger_id
FROM (t t1 JOIN t t2 ON t2.gruppe_id=t1.gruppe_id)
     LEFT JOIN t t3 ON t3.gruppe_id=t1.gruppe_id AND t3.bruger_id<>t1.bruger_id AND t3.bruger_id<>t2.bruger_id
WHERE t3.bruger_id IS NULL AND t1.bruger_id IN (2,3) AND t2.bruger_id IN (2,3);

DROP TABLE t;
