Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading