Sort (Order By) Data in SQL Except Some Certain Row

In this Blog I will tell you about How you can Sort Data or in other words use Order By in SQL which will work according to condition provided. Earlier I was doing Order By like this:
  1. select sub.BroadSubjectArea 'Subject Area',count(BroadSubjectAreasID) 'Number Of Colleges' from defBroadSubjectAreas sub  
  2. join datPFRIBroadAreasDetail area on sub.BroadSubjectAreas_ID=area.BroadSubjectAreasID  
  3. group by sub.BroadSubjectArea  
  4. order by [Subject Area]  
 and I was getting this output:

Sort Data in SQL Except

 Now I will show you the query which will order the data but "Others" will be shown in the last:
  1. select sub.BroadSubjectArea 'Subject Area',count(BroadSubjectAreasID) 'Number Of Companies' from defBroadSubjectAreas sub  
  2. join datPFRIBroadAreasDetail area on sub.BroadSubjectAreas_ID=area.BroadSubjectAreasID  
  3. group by sub.BroadSubjectArea  
  4. order by   
  5.     case when sub.BroadSubjectArea = 'Others' then 1 else 0 end,  
  6. sub.BroadSubjectArea  
  7. GO  

 Now we will see the result: 
 
Ebook Download
View all
Learn
View all