Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is MAX(DISTINCT x) in SQL?

Tags:

sql

jooq

I just stumbled over jOOQ's maxDistinct SQL aggregation function.

What does MAX(DISTINCT x) do different from just MAX(x) ?

like image 987
Thilo Avatar asked Mar 07 '14 07:03

Thilo


People also ask

What does Max () mean in SQL?

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.

Can we use distinct and Max in SQL?

DISTINCT can be used with aggregates: COUNT, AVG, MAX, etc.

What is Max case in SQL?

The SQL MAX function is used to return the maximum value of an expression in a SELECT statement.

What are distinct values in SQL?

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.


2 Answers

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.

like image 107
Mureinik Avatar answered Oct 07 '22 15:10

Mureinik


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.

like image 37
Alma Do Avatar answered Oct 07 '22 16:10

Alma Do