Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn SQL using MySQL in One Day and Learn It Well

You're reading from  Learn SQL using MySQL in One Day and Learn It Well

Product type Book
Published in Apr 2024
Publisher Packt
ISBN-13 9781836205678
Pages 121 pages
Edition 1st Edition
Languages
Author (1):
Jamie Chan Jamie Chan
Profile icon Jamie Chan

Sorting Rows

 

Now that we know how to select data from our tables and filter the results, let’s cover one last concept before we end this chapter. Let’s look at how we can sort the results returned by the SELECT statement.

 

To do that, we use the ORDER BY clause. We can choose to sort the results based on one or more columns.

 

Suppose we want to sort the rows of the employees table using gender, followed by the employee’s name (em_name), we write

 

SELECT * FROM employees ORDER BY gender, em_name;

 

This results in the records of female employees being displayed first, followed by male employees (since the letter F comes before M).

 

Within each gender, the records will be sorted by the employees’ names.

 

Note that the default sorting order is ascending. If we want to sort by descending order, we use the DESC keyword as shown below:

 

SELECT * FROM employees ORDER BY gender DESC, em_name;

 ...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}