How SQL DISTINCT and ORDER BY are Related
'One of the things that confuse SQL users all the time is how DISTINCT and ORDER BY are related in a SQL query.
The Basics
Running some queries against the Sakila database, most people quickly understand:
1
|
SELECT DISTINCT length FROM film
|
This returns results in an arbitrary order, because the database can (and might apply hashing rather than ordering to remove duplicates).
Most people also understand:
1
|
SELECT length FROM film ORDER BY length
|
This will give us duplicates, but in order.
And, of course, we can combine the two:
1
|
SELECT DISTINCT length FROM film ORDER BY length
|
'...
https://blog.jooq.org/2018/07/13/how-sql-distinct-and-order-by-are-related/
Trackbacks
Die Kommentarfunktion wurde vom Besitzer dieses Blogs in diesem Eintrag deaktiviert.
Kommentare
Ansicht der Kommentare: Linear | Verschachtelt