This wikiHow teaches you how to use the ORDER BY clause in SQL to sort results from a table. The ORDER BY clause allows you to sort one or more columns in both ascending and descending order.
Steps
-
Enter SELECT * . This means we'll be looking at all columns. Move to the next line.
-
Enter FROM table_name . Replace table_name with the name of the table, and then move to the next line. [1] X Research sourceAdvertisement
-
Enter ORDER BY criteria ; . Here are some examples:
- For example, if you wanted to display results in alphabetical order based on a column called NAME, you'd use ORDER BY NAME; . Ascending order is the default sort order, but you could also specify that you want it ascending using ORDER BY NAME ASC; if you'd like.
- If you'd rather show the results in the opposite order, you'd use ORDER BY NAME DESC; . DESC means "descending order."
- If you want to sort based on two columns, separate them by commas. For example, ORDER BY LAST_NAME ASC, FIRST_NAME DESC; would display results sorted alphabetically by last name. If the same LAST_NAME matches multiple FIRST_NAME entries, the results of FIRST_NAME will also display in descending order.
-
Execute the command. You will now see your SQL results in the appropriate order.
Advertisement
Expert Q&A
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Tips
Submit a Tip
All tip submissions are carefully reviewed before being published
Thanks for submitting a tip for review!
References
About This Article
Thanks to all authors for creating a page that has been read 105,272 times.
Advertisement