Difficulty: Easy
Correct Answer: ORDER BY
Explanation:
Introduction / Context:
Sorting query results is one of the most basic and frequently used features in SQL. When you retrieve data from a table, the default order is not guaranteed unless you explicitly specify how the rows should be ordered. This question checks your understanding of which SQL clause is responsible for sorting rows in a result set, which is critical when presenting ordered reports or preparing data for further processing.
Given Data / Assumptions:
Concept / Approach:
SQL provides the ORDER BY clause to sort rows returned by a SELECT statement. You can specify one or more columns and indicate ascending or descending order using ASC or DESC. Although some languages use phrases like sort by in natural language, the actual SQL keyword is ORDER BY. GROUP BY is another clause but its purpose is to group rows for aggregate calculations, not to define final presentation order, even though some databases may produce grouped output that appears ordered by the group columns without a formal guarantee.
Step-by-Step Solution:
Step 1: Recall the basic syntax: SELECT columns FROM table ORDER BY column1 ASC, column2 DESC.
Step 2: Recognize that ORDER BY explicitly instructs the database engine to sort the rows.
Step 3: Note that GROUP BY is used for aggregating rows, not specifically for sorting final results.
Step 4: Observe that SORT BY and ALIGN BY are not standard SQL keywords in the SQL language defined by common relational database systems.
Step 5: Conclude that ORDER BY is the correct clause for sorting rows in SQL.
Verification / Alternative check:
To verify, consider a query like SELECT name, salary FROM employees ORDER BY salary DESC. Running this query in any major relational database such as MySQL, PostgreSQL, or Oracle will give you employees ordered by salary in descending order. Removing the ORDER BY clause results in an undefined order, which proves that ORDER BY is the mechanism that controls sorting.
Why Other Options Are Wrong:
Option B, GROUP BY, is incorrect because its primary purpose is to group rows that share common values for aggregate functions like SUM or COUNT; any apparent order is incidental and not guaranteed. Option C, SORT BY, is incorrect because it is not a standard SQL keyword in most relational database systems, even though the phrase sounds natural in English. Option D, ALIGN BY, is completely non standard in SQL and has no meaning in standard query syntax.
Common Pitfalls:
A common issue is assuming that rows will always come out in the order they were inserted or that grouping implies ordering. Without ORDER BY, the database engine is free to return rows in any order, especially as data grows and indexes change. Another pitfall is forgetting to use DESC when descending order is required and assuming descending is the default. Always use ORDER BY explicitly and specify the desired sort direction to avoid surprises in query results.
Final Answer:
The clause used to sort rows in an SQL result set is ORDER BY.
Discussion & Comments