question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow sorting by NULLS FIRST / NULLS LAST

See original GitHub issue

Problem

Currently can’t seem to specify how to sort nulls in a findMany

Suggested solution

To be able to specify NULLS FIRST or NULLS LAST

Alternatives

Currently making separate queries and concat-ing them in JS

Additional context

On a table with rows that contain data synced from elsewhere, there’s a date field for last_synced_details_at;
The column’s default value is null (never been synced)
When attempting to get the “most stale rows” would be orderBy: { last_synced_details_at: "asc" }, I’d get the ones that have been synced first, and rows that have never been synced after

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:103
  • Comments:23 (3 by maintainers)

github_iconTop GitHub Comments

32reactions
jycouetcommented, Mar 4, 2022

All right, let’s try 💪

Let’s consider tabletosort:

id name
1 John
2 Bob
3 NULL
4 Zaz

PostgreSQL

ASC

SELECT * FROM tabletosort order by name -- default to NULLS LAST

-- How to put null first?
-- Option Native NULLS FIRST
SELECT * FROM tabletosort order by name NULLS FIRST
-- Option col IS NULL then
SELECT * FROM tabletosort order by name IS NULL desc, name

DESC

SELECT * FROM tabletosort order by name desc -- default to NULLS FIRST

-- How to put null last?
-- Option Native NULLS LAST
SELECT * FROM tabletosort order by name desc NULLS LAST
-- Option col IS NULL then
SELECT * FROM tabletosort order by name IS NULL, name desc

MySQL

ASC

SELECT * FROM tabletosort order by name -- default to NULLS FIRST

-- Option col IS NULL then
-- it's supported

DESC

SELECT * FROM tabletosort order by name desc -- default to NULLS LAST

-- Option col IS NULL then
-- it's supported

SQLite

ASC

SELECT * FROM tabletosort order by name -- default to NULLS FIRST

-- Option col IS NULL then
-- it's supported

DESC

SELECT * FROM tabletosort order by name desc -- default to NULLS LAST

-- Option col IS NULL then
-- it's supported

Microsoft SQL Server

ASC

SELECT * FROM tabletosort order by name -- default to NULLS FIRST

-- Option col IS NULL then
-- it's supported

DESC

SELECT * FROM tabletosort order by name desc -- default to NULLS LAST

-- Option col IS NULL then
-- it's supported

Summary

PostgreSQL MySQL SQL Lite Microsoft SQL Server
default asc NULLS LAST NULLS FIRST NULLS FIRST NULLS FIRST
default desc NULLS FIRST NULLS LAST NULLS LAST NULLS LAST
Support NULLS XXXX
col IS NULL then

Proposal


type orderBy = {
  direction:
  | 'asc' // platform asc
  | 'desc' // platform desc
  | 'asc_nulls_first' // force null first then asc
  | 'asc_nulls_last' // force null last then asc
  | 'desc_nulls_first' // force null first then desc
  | 'desc_nulls_last'; // force null last then desc
};

...

await this.prisma.tabletosort.findMany({
  orderBy: { nom: 'asc' }
});

Like this, there is NO change of the current behavior and you can opt-in a specific sorting.

🤞 that the topic can move forward 🥳 //BTW, I 🧡 prisma.

11reactions
Weakkycommented, Jul 21, 2022

Hey folks,

This feature was released as part of the 4.1.0 version.

Thanks for upvoting this feature. If you wanna give any feedback, please comment on this issue https://github.com/prisma/prisma/issues/14377

Cheers 👋

Read more comments on GitHub >

github_iconTop Results From Across the Web

How ORDER BY and NULL Work Together in SQL
If you apply the ORDER BY clause to a column with NULLs, the NULL values will be placed either first or last in...
Read more >
SQL how to make null values come last when sorting ascending
Depending on the database engine, use ORDER BY expr some_column DESC NULLS LAST (Oracle) , ORDER BY ISNULL(some_column, 1), some_column ASC (MSSQL) or...
Read more >
How to Order NULL Values First or Last in MySQL? - Designcise
Ordering NULL values last whilst sorting all non-NULL values first in an ascending order;; Ordering NULL values first followed by non-NULL ...
Read more >
Simulate NULLS FIRST and NULLS LAST in the ORDER BY ...
When your query contains the ORDER BY clause to sort the result set, alphanumeric NULL data will sort to the bottom if the...
Read more >
How to ORDER BY with NULLS comes FIRST or LAST - Help
You can check for IS NULL in the ORDER BY clause. Nulls first: select name from people order by name asc. Nulls last:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found