Order from Chaos: The ORDER BY Clause

From time to time you will want to present the results of your query in some kind of order. As you know, however, SELECT FROM gives you a listing, and unless you have defined a primary key (see Day 15, “Creating Indexes on Tables to Improve Performance”), your query comes out in the order the rows were entered. Consider a beefed-up CHECKS table:

SQL> SELECT * FROM CHECKS; CHECK# PAYEE AMOUNT REMARKS --------- -------------------- --------- ------------------- 1 MA BELL 150 HAVE SONS NEXT TIME 2 READING R.R. 245.34 TRAIN TO CHICAGO 3 MA BELL 200.32 CELLULAR PHONE 4 LOCAL UTILITIES 98 GAS 5 JOES STALE $ DENT 150 GROCERIES 16 CASH 25 WILD NIGHT OUT 17 JOANS GAS 25.1 GAS 9 ABES CLEANERS 24.35 X-TRA STARCH 20 ...

Get Sams Teach Yourself SQL in 21 Days, Fourth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.