🔍
👶 Kids📝 Blog About Contact 🚀 Get Started Free

SQL – SELECT DISTINCT

Eliminate duplicate rows from your results using SELECT DISTINCT to get unique values.

Imagine you have 10,000 student records and you want to know which cities they come from — but you do not want to see “Mumbai” listed 847 times. That is exactly what SELECT DISTINCT solves. It collapses duplicate values into a single unique entry, giving you a clean list of only the distinct occurrences.

DISTINCT operates on the entire row after your column selection. If you select two columns, a combination must appear twice to be considered duplicate — not just one of the columns individually.

Syntax

SELECT DISTINCT column1, column2
FROM table_name;

Try It Yourself — Interactive SQL Editor

Edit the query below and click Run Query ▶ to see live results powered by SQLite running directly in your browser.

SQLite – edit & run
Results
← Click Run Query ▶ to see results

Key Points

  • DISTINCT applies to all selected columns combined
  • NULL is treated as a distinct value by most databases
  • DISTINCT can slow queries on large datasets — use indexes
  • Alternative: use GROUP BY for more control

Pro Tip from CodesCompiler: The best way to learn SQL is to break things intentionally — modify the query above, change the WHERE conditions, try different columns. Every error teaches you something the docs cannot.

In the next lesson, we continue exploring SQL’s powerful feature set to build your database mastery.