跳转到主要内容
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 次尝试后可用的解决方案