In Structured Query Language (SQL), which clause should you use to sort the result set returned by a SELECT query (for example, alphabetically by a column or numerically ascending/descending)?

Difficulty: Easy

Correct Answer: ORDER BY.

Explanation:


Introduction / Context:
Sorting query results is a routine task in SQL. Whether you are listing customers by last name or transactions by amount, you need the correct clause to impose a predictable order on the rows returned by a SELECT statement. Knowing which keyword performs sorting prevents inefficient workarounds and ensures portable SQL.



Given Data / Assumptions:

  • The context is standard SQL used in common relational database systems.
  • We want to sort rows by one or more columns in ascending or descending order.
  • The query is a SELECT reading from one or more tables or views.


Concept / Approach:
The SQL clause that specifies the order of rows in a result set is ORDER BY. It accepts one or more sort expressions, optional ASC or DESC modifiers, and can include positional ordinals or expressions. GROUP BY is for aggregating rows, not sorting. There is no standard SORT BY or ARRANGE BY in SQL.



Step-by-Step Solution:

Identify the task: impose ordering on result rows.Recall the standard clause: ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...Eliminate non-standard keywords and clauses intended for other purposes (GROUP BY).


Verification / Alternative check:
Add ORDER BY to a sample query such as: SELECT last_name FROM customers ORDER BY last_name ASC; The database returns names alphabetically, confirming usage.



Why Other Options Are Wrong:

  • SORT BY / ARRANGE BY: Not part of standard SQL; some tools may offer GUI sorting, but the SQL keyword is ORDER BY.
  • GROUP BY: Groups rows for aggregation; it does not guarantee output ordering unless combined with ORDER BY.
  • None of the above: Incorrect because ORDER BY exists and is the correct choice.


Common Pitfalls:
Assuming GROUP BY sorts rows. Some engines may produce seemingly sorted output after GROUP BY, but this is not guaranteed by the SQL standard. Always use ORDER BY for deterministic ordering.



Final Answer:
ORDER BY.

Discussion & Comments

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