I just stumbled over jOOQ's maxDistinct SQL aggregation function.
What does MAX(DISTINCT x)
do different from just MAX(x)
?
SQL MIN() and MAX() Functions The MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.
DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.
The SQL MAX function is used to return the maximum value of an expression in a SELECT statement.
The SQL SELECT DISTINCT Statement The SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
maxDistinct
and minDistinct
were defined in order to keep consistency with the other aggregate functions where having a distinct option actually makes a difference (e.g., countDistinct
, sumDistinct
).
Since the maximum (or minimum) calculated between the distinct values of a dataset is mathematically equivalent with the simple maximum (or minimum) of the same set, these function are essentially redundant.
In short, there will be no difference. In case of MySQL, it's even stated in manual page:
Returns the maximum value of expr. MAX() may take a string argument; in such cases, it returns the maximum string value. See Section 8.5.3, “How MySQL Uses Indexes”. The DISTINCT keyword can be used to find the maximum of the distinct values of expr, however, this produces the same result as omitting DISTINCT.
The reason why it's possible - is because to keep compatibility with other platforms. Internally, there will be no difference - MySQL will just omit influence of DISTINCT
. It will not try to do something with set of rows (i.e. produce distinct set first). For indexed columns it will be Select tables optimized away
(thus reading one value from index, not a table), for non-indexed - full scan.
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