Chapter 12. SELECT Statement: The ORDER BY Clause

12.1 Introduction

In what actual sequence are the rows in the result of a SELECT statement presented? If the SELECT statement has no ORDER BY clause, the sequence is unpredictable. When working through the examples or exercises, you might have found once or twice that the sequence of the rows in your result differs from the one in the book. Adding an ORDER BY clause at the end of a SELECT statement is the only guarantee that the rows in the end result are sorted in a certain way.

image Definition

<order by clause> ::=   ORDER BY <sort specification> [ , <sort specification> ]...<sort specification> ...

Get SQL for MySQL Developers: A Comprehensive Tutorial and Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.