This article is half-done without your Comment! *** Please share your thoughts via Comment ***
I am telling you personally that I hate the use of DISTINCT.
DISTINCT used by those people, who are not sure about their data set or SELECT statement or JOINS.
Whenever I get any query with DISTINCT, immediately I suggest to remove it.
I asked to many developers that, does it makes sense to use DISTINCT or do we have any alternative?
Second, I asked that Why are you required to use DISTINCT.
Guys, what are you thinking? What was that answer?
Very simple, they keep quiet for a minute and were telling like: we are using to avoid duplicate result set.
This is not the correct answer for the use of DISTINCT.
Actually, most of developers are using DISTINCT because they want to hide their mistakes. They are writing incorrect joins and hiding duplication of data.
This is not only for database developer, DBA is a same responsible for this. The DBA must stop the duplication of data.
The DBA should give the answer that how duplicates data inserted in the database and who is responsible for that duplication.
Do you think, the use of DISTINCT is good for query performance?
My answer is simply no. Why, because it fetches unnecessary duplicate data and returns unique from it.
You should know the logical sequence of SQL Query execution. In the logical sequence, DISTINCT execute after SELECT means It checks all the data first and after that It avoids the duplicate data.
As I have raised a question for DISTINCT, few developers asked me to use GROUP BY instead of DISTINCT.
My simple answer was, Please do not use anything to remove duplicates from your result set.
Explicitly, you should remove the duplicate data and then also If you require to count few unique master group data, you can use GROUP BY instead of DISTINCT.