Aggregate functions scope — numeric vs. non-numeric.\n\n"SUM, AVG, MIN, and MAX can only be used with numeric columns."

Difficulty: Easy

Correct Answer: Invalid (MIN and MAX also work with dates/times and text; SUM/AVG require numeric)

Explanation:


Introduction / Context:
SQL provides aggregate functions such as SUM, AVG, MIN, and MAX. Understanding which data types each function supports is essential for writing correct queries. The statement claims all four can be used only with numeric columns; this is partially true for two of them and false for the others.


Given Data / Assumptions:

  • Different SQL dialects support aggregates over numeric, date/time, and textual types.
  • SUM and AVG compute arithmetic results.
  • MIN and MAX select the smallest or largest value by the type’s defined ordering.


Concept / Approach:
SUM and AVG require numeric (or numeric-coercible) data to perform arithmetic. In contrast, MIN and MAX operate over any comparable domain, including dates/times (earliest/latest) and text (lexicographic order). Therefore, the blanket restriction “only numeric” is incorrect.


Step-by-Step Solution:

Classify aggregates: arithmetic (SUM/AVG) vs. order-based (MIN/MAX).Note that arithmetic requires numeric input types.Observe that order-based aggregates use the type’s sort semantics, enabling dates and text.Conclude the statement is invalid.


Verification / Alternative check:
Run examples: SELECT MIN(last_name), MAX(last_name) FROM employees; and SELECT MIN(order_date), MAX(order_date) FROM orders; which return non-numeric results. Conversely, SUM/AVG on text will error unless cast to numeric.


Why Other Options Are Wrong:

  • “Valid” ignores the broad applicability of MIN/MAX.
  • Vendor-specific or index-based conditions are irrelevant.
  • ORDER BY has no bearing on aggregate type constraints.


Common Pitfalls:
Assuming MIN/MAX imply numeric; overlooking collation effects on text ordering; forgetting to cast string-encoded numbers before SUM/AVG.


Final Answer:
Invalid (MIN and MAX also work with dates/times and text; SUM/AVG require numeric)

More Questions from Introduction to SQL

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion