Lesson 12Sorting and Limiting Query Results

As row counts increase, it becomes increasingly important to control the order in which results are returned. If results include hundreds, thousands, or even millions of records, the most relevant records should be first. Users do not have the patience to scroll through records to find what they need, nor should they have to.

It is also important to limit results when they get too big. There's no sense fetching 10,000 records from a database when a user looks at only the first 10. Restricting the results prevents the user from being overwhelmed and conserves resources. Limited results cut down on network bandwidth and server processing.

The TrackIt schema from the Lesson 11, “Adding JOIN Queries” will be used again for this lesson. If you already have a copy from an earlier lesson, you are welcome to use that copy. Otherwise, you can run the TrackIt.sql script found in the downloadable files for this book. This will allow you to run the queries provided in this lesson to see the results.

USING ORDER BY

You have the ability to sort the output from a SQL query. You can do this by using ORDER BY. The ORDER BY clause is an optional extension to the SELECT statement. The ORDER BY keywords are followed by one or more columns, ...

Get Job Ready SQL 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.