Introduction to SQL ORDER BY Ascending
In SQL, various clauses can be used with the SELECT clause to achieve some specific functionality or make the resultset to be retrieved in a particular format. ORDER BY clause is one such clause that helps in getting the ordered data from the raw resultset. It returns the sorted and ordered data based on a certain column(s) as the criteria to ORDER the data. The data can be ordered either in ascending or descending way.
Most of the time, the ORDER BY function is used when using the aggregate functions of SQL Whenever we do not specify the type of the order that is ascending or descending then by default the data is being ordered in ascending way. In this article, we will learn about the syntax, usage, and execution of the ORDER BY clause in SQL for ascending order.
column1, column2,..., columnm
ORDER BY criteriacolumn1 , criteriacolumn2,...,criteriacolumnj;
The syntax of the ORDER BY clause is as shown above. It is the optional clause used in the select clause whenever we need to retrieve the resultset containing multiple column values. It should always be placed after the FROM and WHERE clause in the SELECT clause. Some of the terms used in the above syntax are explained below –
- column1, column2,…, columnm – These are the names of the columns of the target_table table that need to retrieved and fetched in the resultset.
- target_table – Name of the table from where the result is to be fetched.
- conditions_or_constraints – If you want to apply certain conditions on certain columns they can be mentioned in the optional WHERE clause.
- criteriacolumn1 , criteriacolumn2,…,criteriacolumnj – These are the columns that will be considered as the criteria to create the ordering flow in the SQL query. There can be single or multiple column names on which the criteria need to be applied. Note that multiple criteria of ordering should be mentioned in a comma-separated format.
The ORDER BY clause in SQL is evaluated after the FROM, WHERE, SELECT, HAVING, GROUP BY, and clauses. Also, the evaluation of LIMIT clauses is performed after the evaluation of the ORDER BY clause. The following diagram illustrates the sequence of evaluation.
Examples of SQL ORDER BY Ascending
Let us create one table names developers using the following create query –
CREATE TABLE developers(
developer_id int NOT NULL,
team_id int NOT NULL,
name varchar(100) DEFAULT NULL,
position varchar(100) DEFAULT NULL,
technology varchar(100) DEFAULT NULL,
salary int DEFAULT NULL,
PRIMARY KEY (developer_id,team_id)
Let us insert some records in developers table using insert statement –
INSERT INTO developers (developer_id, team_id, name, position, technology, salary) VALUES
we have inserted multiple values in my table using the same insert format.
Now, after retrieving the records of developers table using the below SELECT query –
SELECT * FROM developers;
gives following output –
Now, let us simply order the records of the developer’s table based on the technology column value. For this, we can simply use the following query statement where we have just specified the name of the column that is the criteria on which the sorting is to be done without specifying what type of sorting needs to be done –
FROM developers ORDER BY technology ;
The output of the execution of the above query statement is as follows that contains the sorting in ascending format on the technology column even when we haven’t mentioned the ASC in GROUP BY clause. This is because ascending is the default type of sorting that is considered.
Let us try specifying the ASC explicitly in the query statement –
FROM developers ORDER BY technology ASC;
The output of the execution of the above query statement is as follows which is the same as the previous one.
The ordering can be done on more than one column values in SQL. For this, we need to mention al the columns on the priority basis in the comma-separated format in the ORDER BY clause. Consider the following example, where the ordering is firstly done based on column values of the technology column and further again the ordered on salary column.
ORDER BY technology, salary ;
The output of the execution of above query statement is as follows in which we can observe that all the columns are ordered in ascending manner on technology column and further internally for each of the technology column value the ordering of the same technology valued records is done again on basis of salary column value with smallest first and then in the increasing fashion.
If we revert the specification of column names in order by clause of the above query output will differ as firstly ordering will be done on salary and then on technology. Consider the following query statement –
ORDER BY salary, technology ;
The output of the execution of the above query statement is as follows
We can use the ORDER BY clause to sort and order results and get the sorted resultset from the original resultset. The order can be set in either an ascending or descending manner. To perform the ordering in an ascending manner, we can either skip specifying the type of order as the default type is ascending or can explicitly mention ASC in the order by clause. Also, note that in case of multiple column criteria ordering the order in which the names of columns are specified in the order by column matter.
This is a guide to SQL ORDER BY Ascending. Here we discuss the introduction, syntax, and examples with code implementation respectively. You may also have a look at the following articles to learn more –