SQLPathLab
Basic Query · Basic

DISTINCT

Removes duplicate rows from the query result.

Overview

DISTINCT removes duplicate rows based on the complete set of selected columns. With one column it returns unique values; with several columns it removes only identical combinations. Do not use DISTINCT to hide duplicates caused by an incorrect JOIN; investigate their business meaning first.

Basic syntax

SELECT DISTINCT department FROM employees;

Examples

DISTINCT example

Return the unique department values from employees and compare them with the repeated source values.

Sample table

Table:employees
employees
department
Sales
IT
Sales
HR

Run result

result
department
Sales
IT
HR

Common issues

Which columns does DISTINCT use for deduplication?

It compares the complete combination of selected columns, not only the first column.

How does DISTINCT treat NULL?

Duplicate selected-column combinations containing NULL are generally reduced to one result combination.

Can DISTINCT replace GROUP BY?

Use DISTINCT for unique combinations and GROUP BY when you need aggregates per group.

Tags

CommonFiltering

Was this explanation clear?