Do we have to select the column which we are ordering by in a SQL statement?

Question

In the context of this code challenge, do we have to select the column which we are ordering by in a SQL statement?

Answer

No, you do not have to select the column, or columns, that you are applying the ORDER BY clause on. For example, if you were applying the clause like so,

ORDER BY column_1

you do not have to select column_1 in your statement.

Most of the time, for the sake of clarity of how the data is being ordered, it might make sense to include that column in your SELECT statement, but it is not absolutely necessary. The following would be valid,

SELECT column_1, column_2
FROM table
ORDER BY column_3;
5 Likes