When you’re working with a database, you may want to order the results of a query. Ordering query results makes it easier to find the exact records you are looking for, or to get a better sense of the highest and lowest values in a table. For example, if you wanted to know which employees had won the most
employee of the month awards, you may want to order your query by that datapoint.
There is a built-in function in
SQL that makes it easy to order data in a query:
ORDER BY. The
ORDER BY function, in simple terms, orders the result of a query based on the terms you specify.
In this tutorial, we are going to break down how
ORDER BY works, and when you may want to use it in database queries.
In order to retrieve information from an
SQL database, you need to run a query. Queries typically begin with the
SELECT statement and are used to specify which information you want to retrieve from a database. Queries also usually include a
FROM statement, which tells the database what table you are querying.
Here is the syntax for a typical
SELECT column_name FROM table_name WHERE your_conditions_here;
Here is an example of an
SQL query that returns the names of all employees within an organization:
SELECT name FROM employees;
Our query returns the following:
You can also select multiple column names by separating their names using a comma. Or if you are looking to retrieve data from every column, you can use an asterisk (
*) instead, which represents all the columns in a table.
In addition, you can use a
WHERE statement to filter records to meet a certain set of criteria. For example, if you wanted to get the name, title, and salary for Emma, you would use the following query:
SELECT name, title, salary FROM employees WHERE name = 'Emma';
The result of our query is as follows:
Now we’ve explored the basics of queries, we can go on to look at the
ORDER BY operator.
SQL Order By
When you use the
SELECT statement in
SQL, the rows are usually not in any particular order. So if you wanted to get a list of names from your database, they would likely not appear in alphabetical order.
If you want your data to appear in a specific order, that’s when you should use the
ORDER BY clause. The
ORDER BY clause is used to sort the result of a query in either ascending or descending order.
Here is the syntax for an
ORDER BY query:
SELECT column_name FROM table_name ORDER BY column_name ASC/DESC;
Let’s run an example query to demonstrate the
ORDER BY operator in action. The following query will return the names and titles of all employees, in alphabetical order by employee name:
SELECT name, title FROM employees ORDER BY name ASC;
Here is what our query returns:
|Geoff||Senior Sales Associate|
|Jonah||Vice President of Sales|
If you wanted to sort in reverse alphabetical order, you would substitute the
ASC statement for
You can also sort by multiple columns. This can be useful if you have duplicate values in your table that you want to be sorted. For example, if you want to find out who has been working for you for the shortest time, in order of their titles, you could use this query:
SELECT name, title, hired_date FROM employees ORDER BY title, hired_date DESC;
Here is the result of our query:
|Geoff||Senior Sales Associate||2012-03-17|
|Jonah||Vice President of Sales||2010-07-23|
As you can see, our table is sorted by both the employee’s title and date on which they were hired. So we can see that the most recently hired sales associate was Alexis, and the sales associate working for the company the longest is Luke. We can also see the date on which every other employee was hired, in order of their title.
That’s how you use
ORDER BY in
SQL. As we have discussed, the
ORDER BY operator allows you to order the results of a query in either ascending or descending order. This can be useful if you are looking to find the highest or lowest values within a database, or need to have a list that is sorted in a certain way.