List of Topics

SfC Home > Web Design > SQL >

 

Sorting Retrieved Column Data with SQL

by Ron Kurtus (revised 17 March 2016)

When you retrieve a column from a database table using SQL, the data is usually listed in order of ID numbers, which may not be handy to use.

You can sort the records in the column, so that they will be displayed in a specific order. To do that, you use the ORDER BY clause, which is the last clause in your SELECT statement.

You can sort in ascending order, descending order and according to multiple columns in your table.

Questions you may have include:

  • How do you sort in ascending order?
  • How do you sort in descending order?
  • How do you sort with multiple tables?

This lesson will answer those questions.



Ascending order

To display the data in a column in an alphanumeric order, you used the ORDER BY clause and repeat the name of the column.

SELECT column_name
FROM table_name
ORDER BY column_name;

Descending order

You can a sort in a descending order by including the DESC cause at the end of your ORDER BY clause.

SELECT column_name
FROM table_name
ORDER BY column_name DESC;

Multiple columns

There are a number of combinations of sorting when making a query of multiply columns.

Sort by data in specific column

If you are retrieving data from several columns, you can still sort by the data in a specific column. For example, sorting table_name by the record names in column_2, in descending order, you would write:

SELECT column_1, column_2, column_7
FROM table_name
ORDER BY column_2 DESC;

Sort by one column and then the other

If you want to sort the data by one column and then the other, you state the column names after the ORDER BY clause, in the order of preference and separated by a comma.

SELECT column_1, column_2, column_7
FROM table_name
ORDER BY column_2, column_1;

Note that the sorting is done in ascending order in this example.

Sort ascending and descending

You can also mix ascending and descending orders in your sorting.

SELECT column_1, column_2, column_7
FROM table_name
ORDER BY column_2 DESC, column_1;

The sorting is done first in descending order of column_2, and then those items are sorted in ascending order in column_1.

Summary

You can sort the records in the column, so that they will be displayed in a specific order by using the ORDER BY clause. You can sort in ascending order, descending order and according to multiple columns in your table.


Think big thoughts


Resources and references

Ron Kurtus' Credentials

Websites

SQL Resources

Books

Top-rated books on Using SQL


Questions and comments

Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. I will try to get back to you as soon as possible.


Share

Click on a button to bookmark or share this page through Twitter, Facebook, email, or other services:

 

Students and researchers

The Web address of this page is:
www.school-for-champions.com/sql/
sorting_data_in_columns.htm

Please include it as a link on your website or as a reference in your report, document, or thesis.

Copyright © Restrictions


Where are you now?

School for Champions

SQL topics

Sorting Retrieved Column Data with SQL




SQL topics

Also see



Let's make the world a better place

Be the best that you can be.

Use your knowledge and skills to help others succeed.

Don't be wasteful; protect our environment.

You CAN influence the world.





Live Your Life as a Champion:

Take care of your health

Seek knowledge and gain skills

Do excellent work

Be valuable to others

Have utmost character

Be a Champion!



The School for Champions helps you become the type of person who can be called a Champion.