CREATE TABLE "s"."t1" ( "c1" BigSerial PRIMARY KEY, "c2" BigInt NOT NULL, "c3" BigInt ) WITH (OIDS=FALSE);
INSERT INTO s.t1 (c2) VALUES (10); INSERT INTO s.t1 (c2, c3) VALUES (20, 10); INSERT INTO s.t1 (c2, c3) VALUES (30, 10);
/* 1. */ SELECT t3.c1 FROM s.t1 as t3 JOIN s.t1 as t2 ON t3.c3 = t2.c2;
/* 2. */ SELECT t1.c1, ARRAY_TO_STRING(ARRAY_AGG((t2.c1)), ',') FROM s.t1 LEFT JOIN s.t1 as t2 ON t2.c3 = t1.c2 GROUP BY t1.c1;
/* 3. */ SELECT c1, c2, ARRAY_TO_STRING(ARRAY_AGG((SELECT t3.c1 FROM s.t1 as t3 JOIN s.t1 as t2 ON t3.c3 = t2.c2)), ',') FROM s.t1 t1 GROUP BY c1; DROP SCHEMA s CASCADE;
The query
SELECT t3.c1 FROM s.t1 as t3 JOIN s.t1 as t2 ON t3.c3 = t2.c2 produces multiple rows. Since you are calling the aggregate function on the result set and not as part of the expression, you are not able to get single row as an output.