concept FROM in category postgis

appears as: FROM
PostGIS in Action, Second Edition

This is an excerpt from Manning's book PostGIS in Action, Second Edition.

Unfortunately, because subselects must return a single row when used in SELECT, you can’t use them to answer the more generic N closest where N != 1. In the next listing, the LATERAL join allows you to use a correlated subquery in the FROM.

For accessing data, you use a SELECT statement, usually accompanied with a FROM and a WHERE clause. The SELECT part of the statement restricts the columns that will be returned, the FROM clause determines where the data comes from, and WHERE restricts the number of records to be returned.

When returning constants or simple calculations that come from nowhere, the FROM clause isn’t needed in PostgreSQL, SQL Server, or MySQL, whereas in databases such as Oracle and IBM DB2, you need to select FROMdual or sys.dual or some other dummy table.

The last kind of simple INSERT is one that uses the SELECT clause, as shown in listing C.11. In the simplest example, it doesn’t have a FROM. Some people prefer this syntax because it allows you to alias what the value is right next to the constant. It’s also a necessary syntax for the more complex kind of INSERT we’ll demonstrate in the next section.

Listing C.11. Simple value INSERT using SELECT instead of VALUES
INSERT INTO poi(poi_name, geom)
SELECT 'Park' AS poi_name,
    ST_GeomFromText('POLYGON ((86980 67760,
                     43975 71292, 43420 56700, 91400 35280,
         91680 72460, 89460 75500, 86980 67760))') As geom
UNION ALL
SELECT 'Zoo' As poi_name,
    ST_GeomFromText('POLYGON ((41715 67525, 61393 64101, 91505 49252,
       91400 35280, 41715 67525))')  As geom;

This is the standard way of inserting multiple rows into a table. It was the only way to do a multi-row INSERT before PostgreSQL 8.2. This syntax is supported by PostgreSQL (all versions), MySQL, and SQL Server. To use it in something like Oracle or IBM DB2, you need to include a FROM clause, like FROMdual or sys.dual.