Select Distinct SQL Tutorial

The SELECT DISTINCT statement is used to return unique rows based on combination of values in the specified columns.

When working with data, it’s often helpful to identify unique values rather than displaying duplicates. That’s where the DISTINCT keyword comes in handy! By adding DISTINCT to your SELECT statement, you can remove duplicate records and get a list of distinct values.

Example

Consider the product table, which includes over 10 products:

PizzaDB Product Table example for the Select Distinct tutorial.

Based on what we have already learned, we could write the following query to list all ProductTypes:

select ProductType
from Product

But what if we only want to see unique values of product types amongst all products? In order to achieve this, we can use the Select Distinct statement.

select distinct ProductType from Product
/* Answer */
select distinct ProductType
from Product

SELECT DISTINCT Syntax

The syntax is very similar to the basic SELECT statement you’ve seen in prior lessons. The biggest difference Distinct.

select distinct column1, column2, ...
from TableName

The distinct keyword is placed immediately after the select. It returns a unique list of values based on the columns specified within the select.

Exercise

Now you try it. Can you list unique ProductName and ProductType values from Product?

/* Answer */
select distinct ProductName, ProductType
from Product