Sorting Query Results

Whereas the WHERE conditional places restrictions on what records are returned, the ORDER BY clause will affect how those records are presented. Much as listing the columns of a table arranges the returned order (compare Figures 4.12 and 4.14), ORDER BY structures the entire list. When you do not dictate the order of the returned data, it will be presented to you in somewhat unpredictable ways (although probably on the primary key in ascending order).

SELECT * FROM tablename ORDER BY column

SELECT email FROM users ORDER BY
→ registration_date

The default order when using ORDER BY is ascending (abbreviated ASC), meaning that numbers increase from small to large and dates go from older to most recent. You can reverse this ...

Get PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition now with O’Reilly online learning.

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