# SQL DISTINCT運算子

#### 1. SQL DISTINCT運算子簡介

``````SELECT DISTINCT
column1, column2, ...
FROM
table1;
``````

#### 3. SQL DISTINCT一列範例

``````SELECT
salary
FROM
employees
ORDER BY salary DESC;
``````

``````+--------+
| salary |
+--------+
| 24000  |
| 17000  |
| 17000  |
| 14000  |
| 13500  |
| 13000  |
... ...
``````

``````SELECT
DISTINCT salary
FROM
employees
ORDER BY salary DESC;
``````

``````+--------+
| salary |
+--------+
| 24000  |
| 17000  |
| 14000  |
| 13500  |
| 13000  |
| 12000  |
... ...
``````

#### 4. SQL DISTINCT多列範例

``````SELECT
job_id,
salary
FROM
employees
ORDER BY
job_id,
salary DESC;
``````

``````+--------+--------+
| job_id | salary |
+--------+--------+
|      1 | 8300   |
|      2 | 12000  |
|      3 | 4400   |
|      4 | 24000  |
|      5 | 17000  |
|      5 | 17000  |
... ...
``````

``````SELECT DISTINCT
job_id,
salary
FROM
employees
ORDER BY
job_id,
salary DESC;
``````

``````+--------+--------+
| job_id | salary |
+--------+--------+
|      1 | 8300   |
|      2 | 12000  |
|      3 | 4400   |
|      4 | 24000  |
|      5 | 17000  |
|      6 | 9000   |
|      6 | 8200   |
......
``````

#### 5. SQL DISTINCT和NULL值

NULL值在SQL中是一個特別的值。 它在某些情況下用作標記，比如：缺少資訊或資訊不適用。 因此，`NULL`無法與任何值進行比較。 即使`NULL`也不等於它自己。 如果列中有兩個或多個`NULL`值，資料庫系統是否將它們視為相同或不同的值？

``````SELECT DISTINCT
phone_number
FROM
employees;
``````

``````+----------------+
| phone_number   |
+----------------+
| 0532-86011111  |
| 0551-4243311   |
| 0571-87622362  |
......
| NULL           |
| 0351-2233611   |
| 021-66050000   |
| 010-67237328   |
| 0755-28114518  |
| 0755-83587526  |
| 0513-83512816  |
| 0898-31686222  |
| 022-26144822   |
+----------------+
34 rows in set
``````