Skip to content
Related Articles

Related Articles

Improve Article

Order by in MS SQL Server

  • Difficulty Level : Easy
  • Last Updated : 05 Jun, 2020

In this article, order by and terms related to order by will be discussed.

Introduction –

  1. There are some instances where the tables are to be arranged in a chronological order.
  2. While the users use the select statement to retrieve rows, one cannot guarantee that the rows are arranged in an order.
  3. To solve this, order by clause is being used.

Basic syntax:

select 
select_list
from
table_name
order by 

Example: Sample table-Student



Roll numberNameCourse
111RiyaCSE
112ApoorvaECE
113MinaMech
114RitaBiotechnology
115VeenaChemical
116DeepaEEE

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 numberNameCourse
112ApoorvaECE
116DeepaEEE
113MinaMech
114RitaBiotechnology
111RiyaBiotechnology
115VeenaChemical

Note that the table is arranged in ascending order by default using the order by clause.

ASC | DESC :

  1. A user can arrange the columns in ascending or descending order using ASC or DESC respectively.
  2. ASC arranges the columns from lowest to highest
  3. DESC arranges the columns from highest to lowest.
  4. If there is a NULL column in the table, it will be treated as the lowest value.

Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course  by GeeksforGeeks.

My Personal Notes arrow_drop_up
Recommended Articles
Page :