In this article, order by and terms related to order by will be discussed.
Introduction –
- There are some instances where the tables are to be arranged in a chronological order.
- While the users use the select statement to retrieve rows, one cannot guarantee that the rows are arranged in an order.
- To solve this, order by clause is being used.
Basic syntax:
select select_list from table_name order by
Example: Sample table-Student
| Roll number | Name | Course |
|---|---|---|
| 111 | Riya | CSE |
| 112 | Apoorva | ECE |
| 113 | Mina | Mech |
| 114 | Rita | Biotechnology |
| 115 | Veena | Chemical |
| 116 | Deepa | EEE |
If a user wants to arrange the names in an order then the query must be written as follows:
select roll number name course from student order by name
The output is:
| Roll number | Name | Course |
|---|---|---|
| 112 | Apoorva | ECE |
| 116 | Deepa | EEE |
| 113 | Mina | Mech |
| 114 | Rita | Biotechnology |
| 111 | Riya | Biotechnology |
| 115 | Veena | Chemical |
Note that the table is arranged in ascending order by default using the order by clause.
ASC | DESC :
- A user can arrange the columns in ascending or descending order using ASC or DESC respectively.
- ASC arranges the columns from lowest to highest
- DESC arranges the columns from highest to lowest.
- If there is a NULL column in the table, it will be treated as the lowest value.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- Delete statement in MS SQL Server
- SQL Server Mathematical functions (SQRT, PI, SQUARE, ROUND, CEILING & FLOOR)
- SQL Server Identity
- SQL SERVER | Conditional Statements
- SQL Server | STUFF() Function
- SQL Server | SERVERPROPERTY()
- Mean and Mode in SQL Server
- Allow only alphabets in column in SQL Server
- SQL SERVER | IN Condition
- Difference between MySQL and MS SQL Server
- Comparisons between Oracle vs SQL Server
- Introduction of MS SQL Server
- Select statement in MS SQL Server
- Distinct clause in MS SQL Server
- Where clause in MS SQL Server
- Difference between Couchbase and MS SQL Server
- Create, Alter and Drop schema in MS SQL Server
- Difference between MS SQL Server and CouchDB
- SQL Server LAG() function Overview
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

