Column aliases and readability: does SQL provide the AS keyword to assign meaningful output names to expressions and built-in function results in a SELECT list?
-
ACorrect
-
BIncorrect
-
CValid only when creating a view
-
DWorks only for aggregates, not expressions
-
ERequires double quotes around every alias
Answer
Correct Answer: Correct
Explanation
Introduction / Context:Readable column headers help consumers of query results and downstream tools. This question asks whether SQL supports assigning aliases (via AS) to expressions, including built-in function outputs.
Given Data / Assumptions:
- We are writing a SELECT with functions or expressions.
- We want friendly names for result columns.
- We consider standard SQL behavior.
Concept / Approach:Aliases rename the output column: SELECT COUNT() AS order_count, UPPER(customer_name) AS customer_name_upper FROM ...; The AS keyword is optional in several DBMSs (you can write COUNT() order_count), but AS improves clarity. Aliases are also useful inside subqueries and common table expressions, where outer queries reference the alias.
Step-by-Step Solution:
Identify any computed columns or function outputs in the SELECT list.Add AS alias_name to each expression for readability.If using the result in a parent query, reference the alias.For names with spaces or reserved words, use quoted identifiers as required by the DBMS.Verification / Alternative check:Run the query and inspect column headers in the result grid—aliases should appear as defined, aiding users and BI tools.
Why Other Options Are Wrong:
- Aliases are not limited to views or aggregates.
- Quoted identifiers are needed only if the alias contains spaces/special characters; otherwise quoting is optional.
- “Incorrect” ignores standard SELECT aliasing.
Common Pitfalls:Forgetting that some DBMSs treat unquoted aliases as folded to lower/upper case depending on settings. Be consistent, especially when referencing aliases in ORDER BY.
Final Answer:Correct