Difficulty: Easy
Correct Answer: ORDER BY
Explanation:
Introduction / Context:
Sorting data is a routine task in SQL when preparing reports, lists, or exports. Different words can sound correct in everyday language, such as sort by or order by, but SQL uses a specific keyword combination to perform sorting. This question checks whether you recall the exact syntax used to sort rows in a result set according to one or more columns.
Given Data / Assumptions:
Concept / Approach:
In SQL, the ORDER BY clause is used to specify the column or columns that determine the sorting order of the result set. You can choose ascending or descending order using ASC or DESC. Although the English phrase sort by sounds natural, SQL does not use SORT BY or a standalone SORT keyword in its standard syntax. The keyword ORDER must be followed by BY to form a valid clause.
Step-by-Step Solution:
Step 1: Recall examples such as SELECT name, salary FROM employees ORDER BY salary DESC.
Step 2: Observe that the clause used is ORDER BY, not just ORDER or SORT.
Step 3: Recognize that ORDER alone without BY is not accepted by SQL parsers as a sorting clause.
Step 4: Note that SORT or SORT BY are not standard SQL keywords in major relational database systems.
Step 5: Conclude that ORDER BY is the correct keyword combination for sorting data in SQL.
Verification / Alternative check:
You can run queries in MySQL, PostgreSQL, Oracle, or SQL Server using ORDER BY and verify that sorting works as expected. Attempting to use SORT BY or SORT usually produces syntax errors. Official SQL reference documentation for these systems describes the ORDER BY clause as the only standard way to specify result ordering, confirming this choice.
Why Other Options Are Wrong:
Option A, ORDER, is incorrect because the SQL syntax requires both ORDER and BY to form the clause; ORDER alone is not a complete keyword combination. Option C, SORT BY, is incorrect because SORT is not a standard SQL keyword for sorting; many databases will not recognize it. Option D, SORT, is also incorrect for the same reason, as there is no standalone SORT clause in standard SQL.
Common Pitfalls:
A common mistake is to assume that result sets are automatically sorted by primary key or insertion order, which is not guaranteed. Developers may also forget to specify ORDER BY when relying on a certain order in applications, leading to unstable output. Remember that ORDER BY is the explicit clause required to guarantee ordering, and that ASC is the default direction if not specified.
Final Answer:
The SQL keyword combination used to sort the result set is ORDER BY.
Discussion & Comments