1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. SELECT price * qty AS total FROM items ORDER BY total…

Added 30 Nov -0001

SELECT price * qty AS total FROM items ORDER BY total DESC; works in PostgreSQL. What is total referring to?

  1. The original column named total, since aliases are never visible to ORDER BY
  2. A positional reference, because total is parsed as the second select expression
  3. A re-evaluated expression, since ORDER BY recomputes price times qty again
  4. The output column alias, which ORDER BY accepts even though WHERE does not Correct answer

Correct answer

D. The output column alias, which ORDER BY accepts even though WHERE does not

17 views

MCQSEXAM

Explanation

ORDER BY is logically evaluated after SELECT, so it can see output aliases, while WHERE runs before and cannot. Standard SQL also allows an integer position in ORDER BY, which is convenient but fragile when the select list changes. The alias must be double-quoted, or written in lowercase, to survive the folding rules of the database.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More SQL for Data Analysis MCQs

Practise all SQL for Data Analysis MCQs

Similar MCQs