• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

ORDER BY with NULL values

I have a column in a MySQL database which can contain either 1,2,3 or be NULL. I need to extract the data, sorting it in such a way that 1,2,3 come first, the NULL rows after.
By default, MySQL puts the NULL values first, unless you specify DESC but then the results are in the reverse order! Any advice much appreciated!
1 Solution
The better solution is to order by NVL(my_col,4), if NVL is supported by your version of MySQL.  NVL takes an input column and passes back, unchanged, all non-NULL values in the column; NVL will trap NULLs and pass back whatever value is in the second argument, in this case, '4'.

So if your table contained 1,2,3,NULL as values, the above NVL would return 1,2,3,4.

The much less nice solution involves updating your data to change NULL to 4, or creating an ORDER BY column containing this conversion, etc. -- very ugly stuff.

See if NVL works for you.
Hi bergsy.

Just sort by negative values ...

SELECT tblMyTable.fldID, tblMyTable.fldSomeTextField
FROM tblMyTable

bergsyAuthor Commented:
Nice idea tbcox, but as far as I can find out MySQL doesn't have the NVL function. But if you know otherwise, please tell me!

I've used a variant of Dedushka's answer, which wasn't quite correct but pointed me in the right direction :-

SELECT rank, (0-rank) AS foo
FROM mytable

The syntax is less elegant than Dedushka's, but MySQL seems fussy on this point!
A proven path to a career in data science

At Springboard, we know how to get you a job in data science. With Springboard’s Data Science Career Track, you’ll master data science  with a curriculum built by industry experts. You’ll work on real projects, and get 1-on-1 mentorship from a data scientist.

Hi guys,

what about using a UNION where the first select clause just selects and orders non-null values and the second select clause just returns rows with null values?

Although, Dedushka's idea is not bad...not bad :-)

Ciao, Mike
So I win this points? :-)
Since tbcox's comment is not suitable (that would be my first choice, too) see if this works. The idea is to use an additional table just to help you with the sort problem. Let's call this table tblAdTable and build it with 2 fields: "contents" and "order"

1 - Load it like this:
     contents   order
     1                1
     2                2
     3                3
     NULL           4

2 - Now join field "contents" from tblAdTable with the column in your table, and sort the query by tblAdTable.order;



bergsyAuthor Commented:
Dedushka's solution seems the most elegant, and it's what I have adopted.
Building another table is impractical as a) It's inefficient and b) The actual column was generated on the fly using a natural left join, & I wanted to keep the whole thing as one simple statement.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now