Sunday, November 30, 2025

SQL SORT BY Assertion | DatabaseJournal


In relational databases, the SQL ORDER BY assertion is a robust device for sorting knowledge in a desk. The assertion is used to rearrange queried knowledge into rows in a particular order, primarily based upon a number of columns. On this database programming tutorial, we’ll discover ways to use the SQL ORDER BY assertion to type retrieved knowledge in ascending or descending order.

Learn: Greatest Programs for Database Directors

SQL ORDER BY Assertion Syntax

The syntax for the SQL ORDER BY assertion is proven within the following instance code:

SELECT columnA, columnB, columnC
FROM table_example
ORDER BY columnA, columnB, columnC ASC|DESC;

Within the instance code above, the SELECT assertion is used to first choose the columns to be displayed from a given desk. Then, the FROM assertion defines the desk identify the information might be pulled from. The ORDER BY assertion then types the retrieved knowledge in both ascending or descending order, primarily based on one (or extra) columns.

They key phrases ASC and DESC are optionally available parameters of ORDER BY, which dictate the order of the kind. ASC – which is brief for ascending – is the default type order, whereas DESC – which is brief for descending – types the information within the reverse order.

How one can Type Information in Ascending Order in SQL

In SQL databases, database builders can type knowledge in ascending order utilizing the SQL ORDER BY assertion and the optionally available parameter (mentioned above) ASC, alongside the column (or columns) you need to type by.

For example, if a programmer or database administrator has a desk named workers that has the columns: employee_id, first_name last_name, and hire_date. If we needed to type the queried knowledge by ascending order by the last_name column, we might use the next instance SQL question:

SELECT * FROM workers
ORDER BY last_name ASC;

The above question chooses each column from the workers desk. It then types the information in ascending order by the last_name column.

How one can Type Information in Descending Order in SQL

Sorting knowledge in descending order in SQL is feasible as properly, additionally utilizing the SQL ORDER BY assertion. All database builders have to do is add the DESC key phrase after the column (or columns) you plan to type by. For example, if you wish to type the workers desk by descending order utilizing the last_name column, you’d use the next SQL question:

SELECT * FROM workers
ORDER BY last_name DESC;

The above question retrieves all columns from the workers desk after which types the information by last_name in descending order.

How one can Type by A number of Columns in SQL Utilizing ORDER BY

Database directors and database programmers may type by a number of columns utilizing the SQL ORDER BY assertion. To do that, all it’s worthwhile to do is be aware which columns you need to type by within the ORDER BY clause, utilizing commas as separators. The retrieved knowledge will get sorted first by the primary column, and then utilizing the second column. If there are greater than two type clauses, the kind continues so as as anticipated. For example, if we needed to type our workers desk by the last_name column, and then utilizing the first_name column, we’d use the SQL question beneath:

SELECT * FROM workers
ORDER BY last_name ASC, first_name ASC;

The above SQL question retrieves each column from the workers desk after which types the data by ascending order utilizing the last_name column. In circumstances the place a couple of worker has the identical final identify, then the returned knowledge will then be sorted in ascending order by the first_name column.

Learn: Undertaking Administration Software program for Database Builders

How one can Type Information by Calculated Columns in SQL

Database builders and db admins may type knowledge by columns in a desk utilizing calculated columns. Calculated columns are columns which are created once we use a calculation or equation on a number of current columns in a desk. For instance, in our desk workers if we needed to type by the sum of the wage and bonus columns, in ascending order, we might use the next SQL question:

SELECT *, wage + bonus as total_income FROM workers
ORDER BY total_income ASC;

The above SQL question chooses all columns from the workers desk after which creates a calculated column named total_income by utilizing the sum of wage and bonus. The returned knowledge is then sorted in ascending order courtesy of the total_income column.

How one can Type Information by Alias Names in SQL with ORDER BY

There are some situations the place database programmers may have to type knowledge utilizing an alias identify that we outline in our SELECT assertion. To realize this, put the alias identify within the ORDER BY clause.

For example, utilizing our worker desk let’s say we needed to type our knowledge utilizing a total_income desk we set as an alias identify, by descending order, we will use the next SQL question and assertion:

SELECT *, wage + bonus as total_income FROM workers
ORDER BY total_income DESC;

Our instance question works by deciding on each column from our workers desk and creates a brand new calculated column named total_income (our alias named column) by including collectively the wage and bonus columns. This knowledge is then sorted in descending order utilizing our new total_income column.

Sorting Information by NULL Values

Generally it’s worthwhile to type knowledge in a relational database that comprises a NULL worth; querying this form of knowledge may cause issues if the database developer doesn’t account for this chance. The rationale for that is that NULL is just not technically a worth, however, as an alternative, a placeholder representing the absence of a worth. In case you are sorting knowledge containing NULL, they’re usually sorted at both the starting or finish of the outcomes, relying on the way you set the kind order.

Builders can specify the place NULL values are sorted utilizing the NULLS FIRST or NULLS LAST key phrases within the ORDER BY clause. Let’s say you needed to type the workers” desk in ascending order utilizing the manager_id column, with NULL values sorted final. To take action, we might use the next SQL question:

SELECT * FROM workers
ORDER BY manager_id ASC NULLS LAST;

The question above selects all columns from the workers desk, then types the information in ascending order by the manager_id column, sorting the NULL values final.

You’ll be able to be taught extra about NULL values in our tutorial: Working with NULL Values in SQL.

Last Ideas on SQL ORDER BY

The SQL ORDER BY assertion is used to type knowledge in a relational database. Utilizing it, database builders and database directors can type knowledge in ascending or descending order, utilizing a number of columns, calculated columns, or alias names. You can even specify the place NULL values must be sorted within the outcomes, avoiding some widespread question errors brought on by NULL values. Understanding find out how to use the ORDER BY assertion in SQL lets programmers and admins manipulate knowledge to create extra sturdy queries and make your relational database extra environment friendly.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles