SQL Training Online

  • Training Courses
  • Beginner Book
  • Blog

June 14, 2012 by Joey Blue Leave a Comment

SQL Order By

The SQL Order By allows you to sort your results when you run a SQL Query.

Let’s look at an example:

select * from employee order by employee_name; 

This will sort by the employee_name column.

Here are the results:

image

If you want to change the sort from Ascending (which is the default) to Descending, you would write your query as follows.

select * from employee order by employee_name desc; 

With the following results:

image

You can also sort by more than one column.

select * from employee order by job asc,employee_name desc; 

Notice how job and employee_name columns are both sorted.

image

And the last trick is that you can use the column number.  In this case I will sort by the 3rd column (job) then the 2nd column (employee_name).

select * from employee order by 3 asc,2 asc; 

Notice that I changed the employee_name column to sort ascending.

image

And that’s it for the SQL Order By clause.

Leave any comments or questions below.

Filed Under: SQL Training Tagged With: Beginning SQL, SQL Order By, SQL Where

Recent Posts

  • SQL Database Normalization – 1NF, 2NF, 3NF, and 4NF
  • SQL Joins Tutorial for Beginners – Inner Join, Left Join, Right Join, Full Outer Join – SQL Training Online
  • Zillow House Price Analysis from CSV Archive – #PowerBI 002
  • Learn Basic SQL – 1 Hour Training Course – SQL Training Online
  • Create Table Statement in SQL Server and Inserting Baseball Homerun Leader Dataset – SQL Training Online

Popular Posts

  • SQL Functions
  • SQL Jobs in Oracle and Microsoft SQL Server
  • Troubleshooting a SQL Join

Have Questions? Connect with me.

Link to my Facebook Page
Link to my Pinterest Page
Link to my Rss Page
Link to my Twitter Page
Link to my Youtube Page
Copyright © 2023 · SQLTrainingOnline.com · Consulting at EmbarkBlue.com