The goal of DISTINCT is to trim duplicate records from the result set for all selected columns.
- If any of the selected columns is unique after joining, you can delete DISTINCT.
- If you do not know this, but know that the combination of values โโof the selected column is unique, you can delete DISTINCT.
In fact, as a rule, with correctly designed databases you rarely need DISTINCT, and in those cases when you do this, it is (?) Obvious that you need it. RDBMS, however, cannot leave it to its own devices and actually build an indexing structure to establish it.
Usually you find DISTINCT everywhere when people are not sure about JOINs and the relationship between tables.
Also, in classes, when it comes to pure relational databases, where the result should be the correct set (without duplicate elements = records), you may find it quite common for people to stick to DISTINCT to guarantee this property for theoretical correctness. Sometimes it penetrates production systems.
source share