-- Collection of code snippets by Arne Vajhøj 
-- posted to eksperten.dk, usenet and other places (2002-now) 
CREATE TABLE spgsvar (
    spgnr INTEGER,
    svarnr INTEGER
);

INSERT INTO spgsvar VALUES (1,2);
INSERT INTO spgsvar VALUES (1,2);
INSERT INTO spgsvar VALUES (1,2);
INSERT INTO spgsvar VALUES (1,2);
INSERT INTO spgsvar VALUES (1,1);
INSERT INTO spgsvar VALUES (1,1);
INSERT INTO spgsvar VALUES (1,3);
INSERT INTO spgsvar VALUES (2,1);
INSERT INTO spgsvar VALUES (2,1);
INSERT INTO spgsvar VALUES (3,1);
INSERT INTO spgsvar VALUES (3,2);
INSERT INTO spgsvar VALUES (3,2);

SELECT *
FROM spgsvar;

SELECT *
FROM (SELECT spgnr,svarnr,COUNT(*) AS n FROM spgsvar GROUP BY spgnr,svarnr) x;

SELECT spgnr,svarnr
FROM (SELECT spgnr,svarnr,COUNT(*) AS n FROM spgsvar GROUP BY spgnr,svarnr) x
WHERE n = (SELECT MAX(n) FROM (SELECT spgnr,svarnr,COUNT(*) AS n FROM spgsvar GROUP BY spgnr,svarnr) x2 WHERE x2.spgnr=x.spgnr);

DROP TABLE spgsvar;
