Перейти до основного вмісту
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 спроб