How SQL's DISTINCT clause works? How SQL's DISTINCT clause works? sql sql

How SQL's DISTINCT clause works?


DISTINCT filters out duplicate values of your returned fields.

A really simplified way to look at it is:

  • It builds your overall result set (including duplicates) based on your FROM and WHERE clauses
  • It sorts that result set based on the fields you want to return
  • It removes any duplicate values in those fields

It's semantically equivalent to a GROUP BY where all returned fields are in the GROUP BY clause.


DISTINCT simply de-duplicates the resultant recordset after all other query operations have been performed. This article has more detail.


First selects all the 'available records' and then it 'removes duplicate records' in all 'available records' and prints.