data management
SQL queries
unique values
database tips
column selection

selecting unique values from a column

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Selecting unique values from a column is one of the most common SQL tasks for reporting, cleanup, and exploratory analysis. In SQL, the standard tool is SELECT DISTINCT, but the details still matter because ordering, NULL handling, and multi-column queries can change the result in ways people do not always expect.

The basic DISTINCT query

If you only need the distinct values from one column, the core pattern is simple:

sql
SELECT DISTINCT department
FROM employees;

This returns each department only once, no matter how many rows share that value. It is the usual way to build a list of categories, statuses, or other repeated labels.

Ordering the unique results

DISTINCT removes duplicates, but it does not guarantee a useful order by itself. If the output will be shown to a person, add an ORDER BY:

sql
SELECT DISTINCT department
FROM employees
ORDER BY department;

That keeps the result stable and easier to read.

DISTINCT versus GROUP BY

You can also use GROUP BY to get unique values:

sql
SELECT department
FROM employees
GROUP BY department;

For a single column, the result is usually the same as SELECT DISTINCT. The difference is intent. DISTINCT says "remove duplicates." GROUP BY says "form groups," which is more useful when you are also calculating aggregates.

Counting unique values

If you want the number of unique values rather than the values themselves, use COUNT(DISTINCT ...):

sql
SELECT COUNT(DISTINCT department) AS unique_departments
FROM employees;

This is common in dashboards and summary queries.

Multi-column uniqueness

DISTINCT applies to the whole selected row, not one column at a time. That matters in queries such as:

sql
SELECT DISTINCT city, state
FROM customers;

This returns unique city-state pairs. Two rows with the same city but different states are still considered different because the full selected tuple is different.

NULL, case, and performance

Most SQL engines treat NULL as one distinct grouping value for the purpose of DISTINCT. That means several rows with NULL in the selected column usually appear as a single NULL in the result.

If you want to exclude missing values, filter first:

sql
1SELECT DISTINCT department
2FROM employees
3WHERE department IS NOT NULL
4ORDER BY department;

You may also need normalization. For example, HR and hr might be treated differently depending on collation rules, so sometimes it is better to apply UPPER() or LOWER() before deduplicating. On very large tables, appropriate indexes can also make DISTINCT queries cheaper, especially when the column is frequently used for reporting filters. That performance difference becomes noticeable quickly on wide reporting tables, where a careless distinct query can force expensive sorts. In analytics workloads, that often matters as much as the SQL syntax itself, because the deduplication step can dominate the runtime.

Common Pitfalls

  • Forgetting that DISTINCT applies to the full selected row, not just the first column.
  • Expecting a stable sort order without an explicit ORDER BY.
  • Using GROUP BY when DISTINCT would be simpler and clearer.
  • Forgetting to filter or normalize NULL and case variants when the final list should be cleaner.

Summary

  • Use SELECT DISTINCT column_name to return unique values from one column.
  • Add ORDER BY when the result should be predictable and readable.
  • Use COUNT(DISTINCT column_name) when you need a unique count instead of the list.
  • Remember that multi-column DISTINCT works on the full selected tuple.

Course illustration
Course illustration

All Rights Reserved.