What is this doing? SELECT (a,b,c) FROM mytable ...

Started by ljbalmost 15 years ago2 messagesgeneral
Jump to latest
#1ljb
ljb9832@pobox.com

What syntax or operator did I (accidentally) invoke by putting parentheses
around my column list?
SELECT (a, b, c) FROM mytable...
It gets me a single result column with comma-separated values in
parentheses (see 2nd SELECT below). I can't find an explanation in the
PostgreSQL manual. It doesn't seem to be an array, a subquery, row
constructor, etc. What sort of thing is it?

test=> CREATE TABLE mytable (a INTEGER, b INTEGER, c INTEGER);
test=> INSERT INTO mytable VALUES (1, 2, 3), (4, 5, 6), (7, 8, 9);
test=> SELECT a, b, c FROM mytable ORDER BY 1;
a | b | c
---+---+---
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
(3 rows)

test=> SELECT (a, b, c) FROM mytable ORDER BY 1;
row
---------
(1,2,3)
(4,5,6)
(7,8,9)
(3 rows)

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: ljb (#1)
Re: What is this doing? SELECT (a,b,c) FROM mytable ...

ljb <ljb9832@pobox.com> writes:

What syntax or operator did I (accidentally) invoke by putting parentheses
around my column list?
SELECT (a, b, c) FROM mytable...
It gets me a single result column with comma-separated values in
parentheses (see 2nd SELECT below). I can't find an explanation in the
PostgreSQL manual. It doesn't seem to be an array, a subquery, row
constructor, etc. What sort of thing is it?

Yeah, it's a ROW() constructor. According to the SQL spec you're
allowed to omit the "ROW" keyword. Not one of their better ideas
IMO, especially in a syntax that generally prefers wordiness.

It is documented, very briefly, under 4.2.13 Row Constructors:

The key word ROW is optional when there is more than one
expression in the list.

regards, tom lane