---
# System prepended metadata

title: 資料庫語法

---

# 資料庫語法

## DISTINCT
用於返回單一值
```SQL=
SELECT DISTINCT column1, column2, ...
FROM table_name;
```

## ORDER BY
用於按升序或降序對結果集進行排序。
- ASC
預設值，ascending order 升序
- DESC
descending order 降序

```SQL=
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
```

## NULL
```SQL=
-- IS NULL
SELECT column_names
FROM table_name
WHERE column_name IS NULL;

-- IS NOT NULL
SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;
```

## SELECT TOP
用於指定要返回的記錄數。
```SQL=
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
```

> Not all database systems support the `SELECT TOP` clause. MySQL supports the `LIMIT` clause to select a limited number of records.

## MIN, MAX
MIN() 函數返回所選列的最小值，MAX() 函數返回所選列的最大值。
```SQL=
SELECT MIN(column_name), MAX(column_name)
FROM table_name
WHERE condition;
```

## COUNT, AVG, SUM
COUNT() 函數返回與指定條件匹配的行數。
```SQL=
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
```

AVG() 函數返回數字列的平均值。
```SQL=
SELECT AVG(column_name)
FROM table_name
WHERE condition;
```

SUM() 函數返回數字列的總和。
```SQL=
SELECT SUM(column_name)
FROM table_name
WHERE condition;
```

## LIKE
`LIKE` 運算符在 `WHERE` 子句中用於搜索列中的指定模式。
百分號 (`%`) 代表零、一個或多個字符 
下劃線 (`_`) 代表一個，單個字符
```SQL=
SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
```
## IN
IN 運算符允許在 WHERE 子句中指定多個值。
```SQL=
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);
```

## JOIN