CASE use for NULL values ordered last

suggest change

In this way, 0 (known values) is ranked first and 1 (NULL values) is sorted last. The order of the NULL rows relative to each other is not guaranteed:

SELECT ID
      ,REGION
      ,CITY
      ,DEPARTMENT
      ,EMPLOYEES_NUMBER
  FROM DEPT
  ORDER BY 
  CASE WHEN REGION IS NULL THEN 1 
  ELSE 0
  END, 
  REGION

ID | REGION |CITY |DEPARTMENT|EMPLOYEES_NUMBER|
—— | —— |—— |—— |—— |
10 | Mid-Atlantic |Philadelphia |RESEARCH |13|
14 | Mid-Atlantic |New York |SALES |12 |
9 | Midwest |Chicago |SALES |8 |
12 | New England |Boston |MARKETING |9|
5 | West |Los Angeles |RESEARCH |11 |
15 | NULL |San Francisco |MARKETING |12|
4 | NULL |Chicago |INNOVATION |11 |
2 | NULL |Detroit |HUMAN RESOURCES |9|

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents