Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dleve123 opened this issue Dec 17, 2017 · 0 comments
Open

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

dleve123 opened this issue Dec 17, 2017 · 0 comments

Comments

@dleve123
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant