Chuyển đến nội dung chính
eLearner.app
Mô-đun 2 · Bài học 2 trong tổng số 46/57 trong khóa học~6 min
Bài học theo mô-đun (2/4)

Các giá trị duy nhất với 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

tập thể dục#sql.m2.l2.e1
Nỗ lực: 0Đang tải…

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

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

Add DISTINCT right after SELECT to remove duplicates.

Giải pháp khả dụng sau 3 lần thử

Review exercise

tập thể dục#sql.m2.l2.e2
Nỗ lực: 0Đang tải…

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

Đang tải trình chỉnh sửa…
Hiển thị gợi ý

DISTINCT evaluates uniqueness over the combination of all listed columns.

Giải pháp khả dụng sau 3 lần thử