SQL Constructs : ORDER BY

The ORDER BY clause is used to sort the data in ascending or descending order, based on one or more columns. It is used with Select statement for arranging retrieved data in sorted order. The Order by clause by default sort data in ascending order. To sort data in descending order DESC keyword is used with Order by clause. The Basic syntax of HAVING clause is as follow :
 SELECT cloumn_list FROM table_name ORDER BY column1, column2...., columnN [ASC | DESC];
For example consider the following table students :
 STUDENT_ID  STUDENT_NAME     MARKS   GRADE
 ---------- --------------- --------- -----
   101        John             89       A    
   102        Bruce            85       A    
   103        Kevin            91       A    
   104        Rick             37       C    
   105        Joe              95       A    
   106        Patrik           57       B    
   107        Alice            50       B    
   108        Peter            87       A    
 SQL > SELECT student_name, grade, marks FROM students ORDER BY marks;
The above query will return result in ascending order of the Marks.
  STUDENT_NAME   GRADE     MARKS
 --------------- -----  ----------
   Rick            C        37
   Alice           B        50
   Patrik          B        57
   Bruce           A        85
   Peter           A        87
   John            A        89 
   Kevin           A        91
   Joe             A        95
And for the descending order the query will be :
 SQL > SQL > SELECT student_name, grade, marks FROM students ORDER BY marks DESC;
  STUDENT_NAME    GRADE    MARKS
 --------------- ------- ----------
   Joe              A       95
   Kevin            A       91
   John             A       89
   Peter            A       87
   Bruce            A       85
   Patrik           B       57
   Alice            B       50
   Rick             C       37


Next Topic :

No comments:

Post a Comment