SQL Select Distinct


Sometimes a column in a table has repeating values and you want to know what the list of possible values are. For example, you might have a list of people or customers with address information that includes a country column. How many different countries are represented? What about customers with preferred languages? How many different preferred languages do we have?

Data Cleaning

Suppose we have a table with a column called Direction. There should be 4 different options: north, south, east, and west. You could use distinct to test that theory. If one of the directions was misspelled in one of the rows, and we used a query with distinct, we will see five different directions. We could then fix the spelling mistake in the table’s offending row. Cleaning data is a common task of Data Analysts. We could also create a lookup table of the four directions that the user would select from instead of typing in the direction themselves. We could use a SQL SELECT INTO statement to populate a new lookup table called Directions.

As a comparison, Microsoft Excel has a formula that returns a list of unique values from a range. It is the UNIQUE() function.