SQL SELECT DISTINCT Statement Explained with Examples
Learn SQL SELECT DISTINCT with single and multi-column examples, NULL behavior, DISTINCT vs GROUP BY, and common mistakes.
On this page
SQL, or Structured Query Language, is the cornerstone of many data-related operations. One of its most powerful tools is the SELECT DISTINCT statement. Crucially, DISTINCT dedupes the entire selected row, not just one column — a fact often misunderstood by beginners. This tutorial guides you through the nuances of this statement, ensuring a robust understanding whether you are preparing for SQL interviews, exploring SQL views, or writing queries in your SQL editor.
What SELECT DISTINCT does
The SELECT DISTINCT statement is pivotal when you aim to retrieve unique values from a table, thereby filtering out duplicates.
SELECT DISTINCT on a single column
Sample Table (Students):
| StudentID | Name |
|---|---|
| 1 | Alice |
| 2 | Bob |
| 3 | Alice |
Query:
SELECT DISTINCT Name FROM Students;
Result:
| Name |
|---|
| Alice |
| Bob |
Explanation:
The result showcases unique names. Even though 'Alice' appears twice in the table, the DISTINCT clause ensures it appears only once in the result.
SQL DISTINCT on multiple columns
This allows for unique combinations of several columns, rather than unique values from just one column.
Sample Table (Orders):
| OrderID | Product | Color |
|---|---|---|
| 1 | Apple | Red |
| 2 | Apple | Green |
| 3 | Orange | Orange |
| 4 | Apple | Red |
Query:
SELECT DISTINCT Product, Color FROM Orders;
Result:
| Product | Color |
|---|---|
| Apple | Red |
| Apple | Green |
| Orange | Orange |
Explanation:
The result provides unique combinations of Product and Color. Notice how Apple + Red only appears once, despite being present twice in the original table.
SQL DISTINCT and NULL values
When using SELECT DISTINCT, multiple NULL values are returned as a single distinct NULL in the result.
Sample Table (Employees):
| ID | Name | Address |
|---|---|---|
| 1 | John | NULL |
| 2 | Michael | 123 Lane St |
| 3 | Sarah | NULL |
Query:
SELECT DISTINCT Address FROM Employees;
Result:
| Address |
|---|
| NULL |
| 123 Lane St |
Explanation:
While there are two NULL values in the Address column of the table, the result only displays one. This illustrates the principle that DISTINCT treats NULL values as equal to one another, so repeated NULLs collapse into a single row.
SQL DISTINCT vs GROUP BY
While both provide unique results, GROUP BY is designed for aggregating data based on specific columns.
Example:
Sample Table (ProductSales):
| Product | SaleDate | UnitsSold |
|---|---|---|
| Apple | 2023-01-01 | 5 |
| Apple | 2023-01-01 | 3 |
| Orange | 2023-01-02 | 2 |
| Apple | 2023-01-03 | 4 |
Using DISTINCT:
To retrieve unique combinations of Product and SaleDate:
SELECT DISTINCT Product, SaleDate FROM ProductSales;
Result:
| Product | SaleDate |
|---|---|
| Apple | 2023-01-01 |
| Orange | 2023-01-02 |
| Apple | 2023-01-03 |
Using GROUP BY:
To aggregate the total units sold for each product on each date:
SELECT Product, SaleDate, SUM(UnitsSold) as TotalUnits
FROM ProductSales
GROUP BY Product, SaleDate;
Result:
| Product | SaleDate | TotalUnits |
|---|---|---|
| Apple | 2023-01-01 | 8 |
| Orange | 2023-01-02 | 2 |
| Apple | 2023-01-03 | 4 |
Explanation:
- Using DISTINCT, we can fetch unique combinations of Product and SaleDate.
- With GROUP BY, not only can we group data based on columns, but we can also perform aggregate functions (like SUM) to understand data at a more granular level. In this case, we aggregated the total units sold for each product on a specific date.
SELECT COUNT(DISTINCT column)
To measure the number of unique values in a column, DISTINCT can be paired with the COUNT function.
Query:
SELECT COUNT(DISTINCT Name) FROM Students;
Result:
| COUNT(DISTINCT Name) |
|---|
| 2 |
Explanation:
The result indicates there are two unique student names in the 'Students' table.
SELECT vs SELECT DISTINCT
| Aspect | SELECT Statement | SELECT DISTINCT Statement |
|---|---|---|
| Purpose | Fetches all data from specified columns. | Fetches unique data, removing duplicates. |
| Performance | Generally faster as it retrieves data as-is. | Might be slower on large datasets due to filtering. |
| Use Cases | When needing all data, including duplicates. | When distinct values are required. |
| Example Result | Could get 'Apple', 'Apple', 'Orange'. | Would get 'Apple', 'Orange'. |
Validate unique-result queries in DbSchema
Before using DISTINCT in reporting logic, inspect the raw rows and then the deduplicated result to verify what is actually being removed. DbSchema helps you compare both views quickly and avoid false assumptions around duplicates.
Common mistakes
- Applying DISTINCT to the wrong columns.
- Inefficient performance due to excessive use of DISTINCT on large datasets.
- Misinterpretation of NULL values with DISTINCT.
Frequently asked questions (FAQs)
- Q: Can I combine DISTINCT with other SQL functions? A: Yes, DISTINCT can be used alongside functions like COUNT, SUM, and others.
Practice questions
- Retrieve unique colors of products in the "Orders" table.
- Determine the count of unique student names in the "Students" table.
- Extract unique combinations of product and color for the product "Apple" from the "Orders" table.
To truly master the SELECT DISTINCT statement in SQL, regular practice and real-world application are essential. This guide offers a comprehensive overview, but hands-on experience will solidify your understanding.
Download DbSchema and run these DISTINCT queries against your own database in its SQL Editor, which is part of the free Community Edition.
Run your DISTINCT queries against a real schema
DbSchema reverse-engineers your database into an interactive ER diagram and runs SQL in a tabbed editor with autocomplete and an execution log. The SQL editor is part of the free Community Edition.

