1

I was creating a function following an example from a database class which included the creation of a temporary variable (base_salary) and using a SELECT INTO to calculate its value later. However, I did not realize I used a different order for the syntax (SELECT ... FROM ... INTO base_salary) and the function could be used later without any visible issues (values worked as expected). Is there any difference in using "SELECT ... FROM ... INTO" syntax order? I tried looking about it in the PostgreSQL documentation but found nothing about it. Google search did not provide any meaningful information neither. Only thing I found related to it was from MySQL documentation, which only mentioned about supporting the different order in an older version.

2 Answers2

0

I always use SELECT ... INTO ... FROM , I believe that is the standard supported notation

https://www.w3schools.com/sql/sql_select_into.asp

I would recommend using this, also if there are any updates or if the other version might become unsupported as you mentioned...

James
  • 1
  • If that is to create a new table, the SQL standard only specifies `create table new_table as select ....` - another example of the poor quality of w3fools. –  Jul 07 '22 at 10:39
0

There is no difference. From the docs of pl/pgsql:

The INTO clause can appear almost anywhere in the SQL command. Customarily it is written either just before or just after the list of select_expressions in a SELECT command, or at the end of the command for other command types. It is recommended that you follow this convention in case the PL/pgSQL parser becomes stricter in future versions.

Notice that in (non-procedural) SQL, there is also a SELECT INTO command which works like CREATE TABLE AS, in this version the INTO must come right after the SELECT clause.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375