Skip to content
Advertisement

Using a MIN MAX query over a DISTINCT one in MySQL?

With my new job, I am needing to use SQL more and more. As I need to remove duplicate columns I thought that using a DISTINCT statement would be best. However, I was indicated that a MIN MAX statement might be more suited. As I am still fairly new to SQL I was wondering if I could get some advice on when to use one over the other. All help is appreciated!

Thanks in advance!

Advertisement

Answer

This is a bit long for a comment.

First, the construct is select distinct, not just distinct. That is an important distinction, and one that people learning SQL often confuse.

Second, select distinct and min()/max() do different things. The latter are aggregation functions and should be used with group by.

In my opinion, select distinct is an unnecessary part of the SQL language. Almost everything it does can be handled by group by. It is much better for you to learn to use group by correctly. (And, as a side note, select distinct is almost never useful with group by.)

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement