Перейти к основному содержимому
eLearner.app
Модуль 2 · Урок 2 из 46/57 в курсе~6 min
Уроки модуля (2/4)

Уникальные значения с 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

Упражнение#sql.m2.l2.e1
Попыток: 0Загрузка…

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

Загрузка редактора…
Показать подсказку

Add DISTINCT right after SELECT to remove duplicates.

Решение доступно после 3 попыток

Review exercise

Упражнение#sql.m2.l2.e2
Попыток: 0Загрузка…

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

Загрузка редактора…
Показать подсказку

DISTINCT evaluates uniqueness over the combination of all listed columns.

Решение доступно после 3 попыток