SQL DISTINCT Explained for Beginners (Remove Duplicates)
SQL DISTINCT explained for beginners: learn how to remove duplicate rows, use it with many columns, and count unique values with simple copy-paste examples.
SQL DISTINCT explained for beginners: learn how to remove duplicate rows, use it with many columns, and count unique values with simple copy-paste examples.
SQL DISTINCT removes duplicate rows from your query results, so each value shows up only once. If you have ever run a query and seen the same city or name listed again and again, DISTINCT is the fix. In this beginner guide you will learn what SQL DISTINCT is, how to use it with one or many columns, and how to count unique values.
You do not need any experience. We define every term as we go, and every example is small enough to copy and run.
A database stores data in tables. A table has columns (the labels, like name or city) and rows (one record each). A query is a question you ask the database. Sometimes the answer contains the same value many times. DISTINCT tells the database: "Show me each different value only once."
Think of a class register where students write their home city. Ten students may write "Paris". If you ask for the list of cities, you want Paris once, not ten times. That is DISTINCT.
We will use a small table called customers. Here is what it contains:
| id | name | city | country |
|---|---|---|---|
| 1 | Anna | Paris | France |
| 2 | Ben | London | UK |
| 3 | Chloe | Paris | France |
| 4 | Dev | Berlin | Germany |
| 5 | Ella | London | UK |
| 6 | Finn | Paris | France |
Notice that Paris appears three times and London twice. Let us see what happens without DISTINCT first.
SELECT city
FROM customers;SELECT chooses which columns to show. FROM says which table to read. The result is six rows: Paris, London, Paris, Berlin, London, Paris. The repeats are not helpful if you only want to know which cities you have customers in.
SELECT DISTINCT city
FROM customers;The only change is the word DISTINCT right after SELECT. Now the result has three rows: Paris, London, Berlin. Each city appears once. The original table is not changed. DISTINCT only affects what you see in the result.
You can put several columns after DISTINCT. The database then removes rows where the whole combination is the same.
SELECT DISTINCT city, country
FROM customers;This returns Paris/France, London/UK and Berlin/Germany. A row is only removed when both city and country match an earlier row. This matters: DISTINCT applies to the full list of columns, not just the first one. It is not possible to make only city unique while still showing other columns that differ.
Often you do not want the list. You want a number: how many different cities do we have? An aggregate function is a function that turns many rows into one value. COUNT is one of them. Combine it with DISTINCT like this:
SELECT COUNT(DISTINCT city) AS total_cities
FROM customers;COUNT(DISTINCT city) counts each different city once, so the answer is 3. The word AS gives the result column a friendly name, total_cities. Without DISTINCT, COUNT(city) would return 6, because it counts every row.
DISTINCT works well with other parts of a query. WHERE filters rows, and ORDER BY sorts the result.
SELECT DISTINCT city
FROM customers
WHERE country = 'France'
ORDER BY city;Line 1 asks for unique cities. Line 2 reads the customers table. Line 3 keeps only customers in France. Line 4 sorts the cities from A to Z. The database first filters, then removes duplicates, then sorts. Here the result is just Paris.
NULL means "no value" in SQL. If several rows have NULL in a column, DISTINCT treats them as the same and shows a single NULL row. That is handy to know when a column has missing data.
| You want to... | Use this |
|---|---|
| List each value once | SELECT DISTINCT city FROM customers; |
| List unique combinations | SELECT DISTINCT city, country FROM customers; |
| Count unique values | SELECT COUNT(DISTINCT city) FROM customers; |
| Unique values, sorted | SELECT DISTINCT city FROM customers ORDER BY city; |
The same SELECT DISTINCT syntax works in MySQL, PostgreSQL, SQL Server and SQLite, so what you learn here carries over.
SQL DISTINCT removes duplicate rows from the result of a SELECT query. Each unique value or combination of values is shown only once. It does not change the data stored in the table.
Use COUNT(DISTINCT column_name). For example, SELECT COUNT(DISTINCT city) FROM customers; returns the number of different cities. A plain COUNT(city) counts every row, including repeats.
Both can return unique values. DISTINCT simply removes duplicates, while GROUP BY groups rows so you can run calculations such as COUNT or SUM on each group. For a simple unique list, DISTINCT is easier to read.
Yes. SELECT DISTINCT city, country removes only rows where both values match an earlier row. DISTINCT always looks at the whole row of selected columns.
No. DISTINCT only changes what the query shows. Your table stays exactly the same. To remove rows for real you would use DELETE.
Next, learn how to count and group your data with the SQL GROUP BY clause.
Read the SQL GROUP BY guideWant help turning your data into a real web app? Talk to the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL CASE WHEN step by step. This beginner guide shows how to label, group and sort data with simple examples you can copy and run today.
PostgreSQL ILIKE explained for beginners: search text without worrying about case, learn % and _ wildcards, and copy simple examples you can run today.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.