AlisdairO / pgexercises

PostgreSQL Exercises web site code
Other
400 stars 62 forks source link

Bad(?) hint in insert calculated data exercise #41

Open dleve123 opened 6 years ago

dleve123 commented 6 years ago

Exercise: https://www.pgexercises.com/questions/updates/insert3.html

Part of the answer text:

Since the VALUES clause is only used to supply constant data, we need to replace it with a query instead. The SELECT statement is fairly simple: there's an inner subquery that works out the next facid based on the largest current id, and the rest is just constant data. The output of the statement is a row that we insert into the facilities table.

However, a solution with VALUES is accepted by the web application:

INSERT INTO cd.facilities (facid, name, membercost, guestcost, initialoutlay, monthlymaintenance)
VALUES ((SELECT max(facid) FROM cd.facilities) + 1, 'Spa', 20, 30, 100000, 800)

Moreover, the postgresql docs for VALUES includes the following:

Syntactically, VALUES followed by expression lists is treated as equivalent to:

SELECT select_list FROM table_expression
and can appear anywhere a SELECT can. For example, you can use it as part of a UNION, or attach a sort_specification (ORDER BY, LIMIT, and/or OFFSET) to it. VALUES is most commonly used as the data source in an INSERT command, and next most commonly as a subquery.

which, if I understand correctly, isn't consistent with the above-quoted answer text.

If you agree, @AlisdairO, I'm happy to change the exercise text accordingly.