SQL Select Distinct
SELECT DISTINCT returns only the different values in a column, removing duplicate rows from the result.
Why DISTINCT is useful
A single column often contains the same value many times. In a Customers table, for instance, dozens of customers might share the same country. If you only want to know which countries appear at all, a plain SELECT would repeat each country once for every customer. SELECT DISTINCT solves this by collapsing identical rows down to a single entry, so you see each unique value just once.
Compare the two queries below. The first lists the country of every customer, including many repeats. The second returns each country only once.
Example
SELECT Country FROM Customers;Example
SELECT DISTINCT Country
FROM Customers;How duplicates are decided
When you list more than one column, DISTINCT looks at the combination of all those columns together. A row is treated as a duplicate only when every listed column matches another row exactly. This lets you find unique pairings, such as each distinct combination of country and city.
Example
SELECT DISTINCT Country, City
FROM Customers;DISTINCT is often combined with counting to answer questions like how many different countries the customers come from. You will build on this idea when you reach SQL Aggregate Functions. For now, return to SQL Select to review the basics.