Lets strip the question back to the bare minimum and remove the UNION, the other columns and use the DUAL table:
SELECT DUMMY -- comment
FROM DUAL;
This outputs:
| DUMMY |
|-------|
| X |
The statement is referencing a column in a table so that column's name will be used as the alias in the statement's output.
However, if we do not reference a column and use a literal:
SELECT NULL -- comment
FROM DUAL;
This will output:
| NULL--COMMENT |
|---------------|
| X |
and Oracle will generate the column name from the text of the SQL statement and the alias for the column in the output will be the text of the query between the SELECT and FROM keywords so the name will be NULL -- comment with the whitespace stripped out.
So SELECT NULL FROM DUAL would have a column name of NULL.
A slightly more complicated version with other literal values:
SELECT NULL --comment here
, NULL /* other comment */,
'LITERAL' -- third comment
, 0 /* fourth comment */
FROM DUAL
None of the generated columns reference a named column of a table so Oracle will generate the name from the SQL and outputs:
| NULL--COMMENTHERE | NULL/*OTHERCOMMENT*/ | 'LITERAL'--THIRDCOMMENT | 0/*FOURTHCOMMENT*/ |
|-------------------|----------------------|-------------------------|--------------------|
| (null) | (null) | LITERAL | 0 |
and you can see the column names are generated from the SQL statement select clause list delimited by commas.
The SQL statement with a UNION will take the column names from the first SELECT clause (before the UNION) so:
SELECT NULL -- comment
FROM DUAL
UNION ALL
SELECT DUMMY
FROM DUAL;
Outputs:
| NULL--COMMENT |
|---------------|
| (null) |
| X |
The column name will be from the first statement and not from the second (below the UNION) which does reference a column.
If you want to override the default column name then use an alias:
SELECT NULL "NULL" --comment here
, NULL AS other /* other comment */,
'LITERAL' AS literal -- third comment
, 0 AS "0" /* fourth comment */
FROM DUAL
Outputs:
| NULL | other | LITERAL | 0 |
|--------|--------|---------|---|
| (null) | (null) | LITERAL | 0 |
and the aliases are used and the comments have been ignored.