メインコンテンツにスキップ
eLearner.app
モジュール 2 · レッスン 2 / 4コース内の 6/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 回の試行後に解決策が利用可能になります