0

Why is the following query a valid select ?

SELECT * from arelation somerandomtext;

The content of arelation does not matter, it just hast to be an existing view/table.

It returns the correct result, respectively the output of the select without the somerandomtext.

Why does this query do not throw an error/exception, is there no keyword (Group By, limit...) check ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Akkusativobjekt
  • 2,005
  • 1
  • 21
  • 26
  • 7
    This is called a table alias and is a fundamental part of the SQL language -- required for instance to do self-joins. They are described in the Postgres documentation here (http://www.postgresql.org/docs/9.2/static/queries-table-expressions.html). – Gordon Linoff Feb 05 '14 at 15:53
  • I always use(d) the the alias function with "as" , so this point did not came to my mind. – Akkusativobjekt Feb 05 '14 at 16:04
  • 1
    I always use `as` for column aliases and never for table aliases -- probably the influence of Oracle earlier in my life (Oracle does not allow `as` for table aliases). – Gordon Linoff Feb 05 '14 at 16:06
  • 1
    Yup!! Oracle supports 'AS' for aliasing columns only.. – Maheswaran Ravisankar Feb 05 '14 at 16:09
  • 1
    @GordonLinoff: Always using `AS` for *column aliases* (while the same is optional for table aliases) is also recommended for Postgres. Details [here](http://stackoverflow.com/questions/20229979/query-to-order-by-the-number-of-rows-returned-from-another-select/20230716#20230716). – Erwin Brandstetter Feb 05 '14 at 16:23

1 Answers1

0

Its an alias

i.e.

select c.id, c.* 
from products c 

is valid syntax as it allows joins from a table to itself

i.e.

select c.id, p.id 
from products c inner join products p on p.id = c.id
RoughPlace
  • 1,111
  • 1
  • 13
  • 23