The DISTINCT keyword in SQL is used to return only unique values in the result set. It removes all duplicate rows from your query results.
Basic syntax:
Loading editor...
Let's see how DISTINCT works with our sales data:
↗️
Loading editor...
↶
↷
You can also use DISTINCT with multiple columns. In this case, SQL looks for unique combinations of those columns:
↗️
Loading editor...
↶
↷
You can also combine DISTINCT with COUNT() to count the number of unique values:
↗️
Loading editor...
↶
↷
DISTINCT vs GROUP BY
DISTINCT is simpler than GROUP BY and is used only to remove duplicates. GROUP BY is more powerful because it can be combined with aggregate functions to calculate summaries for each group.