Consider the below
empname salary
a 10000
b 5000
c 5000
If I do select distinct salary from @t
the output being
salary
5000
10000
It has been sorted ... Why so?
No. There are a number of circumstances in which a DISTINCT in Oracle does not imply a sort, the most important of which is the hashing algorithm used in 10g+ for both group by and distinct operations. Always specify ORDER BY if you want an ordered result set, even in 9i and below.
The DISTINCT keyword in the SELECT clause is used to eliminate duplicate rows and display a unique list of values. In other words, the DISTINCT keyword retrieves unique values from a table.
We can sort results in ascending or descending order with an ORDER BY clause in Select statement.
SQL DISTINCT clause is used to remove the duplicates columns from the result set. The distinct keyword is used with select keyword in conjunction. It is helpful when we avoid duplicate values present in the specific columns/tables. The unique values are fetched when we use the distinct keyword.
Given that without an explicit ORDER BY
the order of returned rows is undefined; the DISTICT
probably causes a distinct-sort
(look at the plan) operation which is probably why they are ordered in your example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With