Search⌘ K
AI Features

Sorting Data

Explore how to sort SQL query results using the ORDER BY clause with ascending and descending options. Understand how to retrieve a specific number of records using the LIMIT keyword and apply these concepts to create sorted reports and top N record queries.

Revisiting report 1

Let’s revisit the part where we created a report listing salespersons. Here is what we did:

MySQL
SELECT DISTINCT SalesPerson
FROM SalesData;

Although we had successfully generated the report, we overlooked the requirement that the names of the salespersons in the sample report should be sorted in ascending order. 

Report 1: List of salespersons
Report 1: List of salespersons

To that end, SQL provides a mechanism to arrange the outcomes in either ascending or descending order.

Sorted in descending order
Sorted in descending order

The ...