cybertec-postgresql / postgres-showcase

Postgres features showcase (commented SQL samples) for beginners

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add ARRAY(SELECT) example to the arrays file

polobo opened this issue · comments

SELECT ARRAY(SELECT col1 FROM ( VALUES (1), (2), (3) ) vals (v)); -- {1,2,3}

basically equivalent to SELECT (SELECT array_agg(col1) ...)

I'm not sure whether one performs better than the other but the ARRAY() form seems more natural when working in a scalar subquery context.

Added with minor corrections. Feel free to do a PR when not exactly what you meant:)

-- using the ARRAY constructor
SELECT ARRAY(VALUES (1), (2), (3));
-- ARRAY constructor with a subselect
SELECT ARRAY(SELECT v FROM ( VALUES (1), (2), (3) ) vals (v));