Pular para o conteúdo principal
eLearner.app
Módulo 2 · Lição 2 de 46/57 no curso~6 min
Lições do módulo (2/4)

Valores únicos com DISTINCT

Sometimes a column contains repeated values and you only care about the list of distinct values. The keyword DISTINCT placed right after SELECT removes duplicates from the result:

SQL
SELECT DISTINCT <colonne>
FROM   <tabella>
[WHERE <condizione>];

Uniqueness is evaluated over the combination of all listed columns. If you list two columns, two rows are considered equal only when both values match.

Examples

SQL
-- Le città in cui abbiamo almeno un dipartimento:
SELECT DISTINCT city FROM departments;

-- Le coppie (dipartimento, città) presenti (qui ognuna unica):
SELECT DISTINCT department_id, hired_on
FROM employees;

Try it

Exercício#sql.m2.l2.e1
Tentativas: 0Carregando…

List the distinct department_id values present among employees (duplicates must disappear).

Carregando editor…
Mostrar dica

Add DISTINCT right after SELECT to remove duplicates.

Solução disponível após 3 tentativas

Review exercise

Exercício#sql.m2.l2.e2
Tentativas: 0Carregando…

List the distinct (department_id, hired_on) pairs for employees hired from 2020 onwards.

Carregando editor…
Mostrar dica

DISTINCT evaluates uniqueness over the combination of all listed columns.

Solução disponível após 3 tentativas