I have code with or without using aliases in outer query. Both work well and provide similar result.
Not using aliases in outer query:
SELECT DISTINCT ON (msg_id, msg_checksum) *
FROM
(
SELECT id, jsonb_var,
jsonb_var ->> 'id' as msg_id,
jsonb_var ->> 'checksum' as msg_checksum
FROM testing_json_as_table
) AS subquery_table_with_msg_id_and_msg_checksum
ORDER BY msg_id, msg_checksum;
Using aliases in outer query:
SELECT DISTINCT ON (subquery_table_with_msg_id_and_msg_checksum.msg_id, subquery_table_with_msg_id_and_msg_checksum.msg_checksum) *
FROM
(
SELECT id, jsonb_var,
jsonb_var ->> 'id' as msg_id,
jsonb_var ->> 'checksum' as msg_checksum
FROM testing_json_as_table
) AS subquery_table_with_msg_id_and_msg_checksum
ORDER BY subquery_table_with_msg_id_and_msg_checksum.msg_id, subquery_table_with_msg_id_and_msg_checksum.msg_checksum;
My question is:
- Why both code give similar result?
- When I should use aliases in outer query and when not?