# How to ORDER BY with NULLS comes FIRST or LAST

**URL:** <https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386>\
**Category:** Help\
**Created:** [February 28, 2019, 9:26pm UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386 "2019-02-28T21:26:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chit-man.tam](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/c/ed8c4c/32.png) [@chit-man.tam](https://www.singlestore.com/forum/u/chit-man.tam)\
**Post date:** [February 28, 2019, 9:26pm UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386/1 "2019-02-28T21:26:08Z")

</div>

I would like to sort on a column and push nulls to the back of the resultset. In oracle I can do ORDER BY COLUMN NULLS LAST, is there something similar in MEMSQL? Thanks.

---

<div class="post-metadata">

**Author:** ![mpskovvang](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/mpskovvang/32/167_2.png) [@mpskovvang](https://www.singlestore.com/forum/u/mpskovvang)\
**Post date:** [March 1, 2019, 6:24am UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386/2 "2019-03-01T06:24:40Z")

</div>

You can check for `IS NULL` in the `ORDER BY` clause.

Nulls first:

```auto
select name from people order by name asc

```

Nulls last:

```auto
select name from people order by name is null, name asc

```

Descending order can be achieved as well with this approach.

Nulls first:

```auto
select name from people order by name is not null, name desc

```

Nulls last:

```auto
select name from people order by name desc

```

---

<div class="post-metadata">

**Author:** ![rob](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/r/838e76/32.png) [@rob](https://www.singlestore.com/forum/u/rob)\
**Post date:** [March 1, 2019, 5:49pm UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386/3 "2019-03-01T17:49:24Z")

</div>

You can also accomplish this by negating the order by column and using `desc`. For example:

```auto
memsql> select * from t order by -a desc;
+------+
| a |
+------+
| 1 |
| 2 |
| NULL |
+------+
3 rows in set (0.00 sec)

memsql> select * from t order by a;
+------+
| a |
+------+
| NULL |
| 1 |
| 2 |
+------+
3 rows in set (0.08 sec)

```

---

<div class="post-metadata">

**Author:** ![chit-man.tam](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/c/ed8c4c/32.png) [@chit-man.tam](https://www.singlestore.com/forum/u/chit-man.tam)\
**Post date:** [March 4, 2019, 8:27pm UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386/4 "2019-03-04T20:27:13Z")

</div>

Hi mpskovvang, thanks of the response. I tried “is null”, although the query went through, the result returned is not sorted.

---

<div class="post-metadata">

**Author:** ![chit-man.tam](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/c/ed8c4c/32.png) [@chit-man.tam](https://www.singlestore.com/forum/u/chit-man.tam)\
**Post date:** [March 4, 2019, 8:28pm UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386/5 "2019-03-04T20:28:15Z")

</div>

Hi rob thanks for the input, any recommendations for Strings?

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [March 4, 2019, 9:59pm UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386/6 "2019-03-04T21:59:16Z")

</div>

You could use a conditional expression and make a NULL come out as ZZZZ or something suitably large.

---

<div class="post-metadata">

**Author:** ![chit-man.tam](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/c/ed8c4c/32.png) [@chit-man.tam](https://www.singlestore.com/forum/u/chit-man.tam)\
**Post date:** [March 5, 2019, 12:36am UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386/7 "2019-03-05T00:36:31Z")

</div>

I see, thanks hanson!

---

<div class="post-metadata">

**Author:** ![jack](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/jack/32/204_2.png) [@jack](https://www.singlestore.com/forum/u/jack)\
**Post date:** [March 5, 2019, 6:06pm UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386/8 "2019-03-05T18:06:06Z")

</div>

> I tried “is null”, although the query went through, the result returned is not sorted.

What was the query you used? Did you make sure to include both `name is null` and `name` in the order by list?

---

<div class="post-metadata">

**Author:** ![chit-man.tam](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/c/ed8c4c/32.png) [@chit-man.tam](https://www.singlestore.com/forum/u/chit-man.tam)\
**Post date:** [March 6, 2019, 5:45pm UTC](https://www.singlestore.com/forum/t/how-to-order-by-with-nulls-comes-first-or-last/386/9 "2019-03-06T17:45:59Z")

</div>

Hi Jack, good call, I only had name is null but not name. It now works, thanks!
