Why does querying table name as a column work in postgresql

First query:

select badges from badges;
           badges
-----------------------------------
 (1,BadgeNameHere,1)
 (2,Figher,1)
(2 rows)

Second query:

select * from badges;
 id |     name      |  person_id
----+---------------+---------------
  1 | BadgeNameHere |  1
  2 | Figher        |  1
(2 rows)

I was expecting first query to fail, since badges is not a column name, but the table name, but it seems if I query a table name, I get the results as some form of tuples?

Is this an SQL feature of a Postgresql feature? Where can I read more about this?

>Solution :

Short story: it seems to be a postgresql’s feature:

In PostgreSQL, a reference to a table name (or alias) in a query is effectively a reference to the composite value of the table’s current row. For example, if we had a table inventory_item as shown above, we could write:

SELECT c FROM inventory_item c;

This query produces a single composite-valued column, so we might get output like:

           c
------------------------
 ("fuzzy dice",42,1.99)
(1 row)

source: https://www.postgresql.org/docs/16/rowtypes.html#ROWTYPES-USAGE

Leave a Reply