Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Pull requests are welcome. Enjoy!
* `LIKE` ‘%or%’ (find any values that have “or” in any position)
* `LIKE` ‘_r%’ (find any values that have “r” in the second position)
* `LIKE` ‘a_%_%’ (find any values that start with “a” and are at least 3 characters in length)
* `LIKE` ‘[a-c]%’ (find any values starting with “a”, “b”, or “c”
* `LIKE` ‘[a-c]%’ (find any values starting with “a”, “b”, or “c”)

### **IN**: operator that allows you to specify multiple values in a WHERE clause
* essentially the IN operator is shorthand for multiple OR conditions
Expand Down Expand Up @@ -97,7 +97,12 @@ Pull requests are welcome. Enjoy!
* `SELECT` column_name1, COUNT(column_name2) `FROM` table_name `WHERE` condition `GROUP BY` column_name1 `ORDER BY` COUNT(column_name2) DESC;

### **HAVING**: this clause was added to SQL because the WHERE keyword could not be used with aggregate functions
* `SELECT` `COUNT`(column_name1), column_name2 `FROM` table `GROUP BY` column_name2 `HAVING` `COUNT(`column_name1`)` > 5;
* `SELECT` `COUNT`(column_name1), column_name2 `FROM` table `GROUP BY` column_name2 `HAVING COUNT`(column_name1) > 5;
* `SELECT` * `FROM` table_name `WHERE` column_name `IN` (`SELECT` column_name `FROM` table_name `GROUP BY` column_name `HAVING COUNT`(*) > 1);

### **EXISTS**: used in a WHERE clause to check whether a subquery returns any rows
* The `EXISTS` operator evaluates to true if the subquery returns at least one row, and false otherwise
* `SELECT` * `FROM` table_name `WHERE EXISTS` (`SELECT` column_name `FROM` table_name `WHERE` condition);

### **WITH**: often used for retrieving hierarchical data or re-using temp result set several times in a query. Also referred to as "Common Table Expression"
* `WITH RECURSIVE` cte `AS` (<br/>
Expand Down Expand Up @@ -128,17 +133,17 @@ Pull requests are welcome. Enjoy!
# 3. Reporting Queries

### **COUNT**: returns the # of occurrences
* `SELECT COUNT (DISTINCT` column_name`)`;
* `SELECT COUNT` (`DISTINCT` column_name);

### **MIN() and MAX()**: returns the smallest/largest value of the selected column
* `SELECT MIN (`column_names`) FROM` table_name `WHERE` condition;
* `SELECT MAX (`column_names`) FROM` table_name `WHERE` condition;
* `SELECT MIN` (column_names) `FROM` table_name `WHERE` condition;
* `SELECT MAX` (column_names) `FROM` table_name `WHERE` condition;

### **AVG()**: returns the average value of a numeric column
* `SELECT AVG (`column_name`) FROM` table_name `WHERE` condition;
* `SELECT AVG` (column_name) `FROM` table_name `WHERE` condition;

### **SUM()**: returns the total sum of a numeric column
* `SELECT SUM (`column_name`) FROM` table_name `WHERE` condition;
* `SELECT SUM` (column_name) `FROM` table_name `WHERE` condition;

<a name="joins"></a>
# 4. Join Queries
Expand All @@ -154,7 +159,7 @@ Pull requests are welcome. Enjoy!
* `SELECT` column_names `FROM` table1 `RIGHT JOIN` table2 `ON` table1.column_name=table2.column_name;

### **FULL (OUTER) JOIN**: returns all records when there is a match in either left or right table
* `SELECT` column_names `FROM` table1 ``FULL OUTER JOIN`` table2 `ON` table1.column_name=table2.column_name;
* `SELECT` column_names `FROM` table1 `FULL OUTER JOIN` table2 `ON` table1.column_name=table2.column_name;

### **Self JOIN**: a regular join, but the table is joined with itself
* `SELECT` column_names `FROM` table1 T1, table1 T2 `WHERE` condition;
Expand Down
31 changes: 22 additions & 9 deletions README_zh-hans.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
4. [ 连接查询 ](#joins)
5. [ 视图查询 ](#view)
6. [ 修改表的查询 ](#alter)
7. [ 创建表的查询。](#create)

<a name="find"></a>
# 1. 查找数据的查询
Expand Down Expand Up @@ -87,7 +88,12 @@
* `SELECT` column_name1, COUNT(column_name2) `FROM` table_name `WHERE` condition `GROUP BY` column_name1 `ORDER BY` COUNT(column_name2) DESC;

### **HAVING**: HAVING 子句指定 SELECT 语句应仅返回聚合值满足指定条件的行。它被添加到 SQL 语言中,因为WHERE关键字不能与聚合函数一起使用。
* `SELECT` `COUNT`(column_name1), column_name2 `FROM` table `GROUP BY` column_name2 `HAVING` `COUNT(`column_name1`)` > 5;
* `SELECT` `COUNT`(column_name1), column_name2 `FROM` table `GROUP BY` column_name2 `HAVING COUNT`(column_name1) > 5;
* `SELECT` * `FROM` table_name `WHERE` column_name `IN` (`SELECT` column_name `FROM` table_name `GROUP BY` column_name `HAVING COUNT`(*) > 1);

### **EXISTS**: 用于 WHERE 子句中,检查子查询是否返回任何行
* 如果子查询返回至少一行,`EXISTS` 运算符的结果为 true,否则为 false
* `SELECT` * `FROM` table_name `WHERE EXISTS` (`SELECT` column_name `FROM` table_name `WHERE` condition);


<a name="modify"></a>
Expand All @@ -109,17 +115,17 @@
# 3. 聚合查询

### **COUNT**: 返回出现次数
* `SELECT COUNT (DISTINCT` column_name`)`;
* `SELECT COUNT` (`DISTINCT` column_name);

### **MIN() and MAX()**: 返回所选列的最小/最大值
* `SELECT MIN (`column_names`) FROM` table_name `WHERE` condition;
* `SELECT MAX (`column_names`) FROM` table_name `WHERE` condition;
* `SELECT MIN` (column_names) `FROM` table_name `WHERE` condition;
* `SELECT MAX` (column_names) `FROM` table_name `WHERE` condition;

### **AVG()**: 返回数字列的平均值
* `SELECT AVG (`column_name`) FROM` table_name `WHERE` condition;
* `SELECT AVG` (column_name) `FROM` table_name `WHERE` condition;

### **SUM()**: 返回数值列的总和
* `SELECT SUM (`column_name`) FROM` table_name `WHERE` condition;
* `SELECT SUM` (column_name) `FROM` table_name `WHERE` condition;

<a name="joins"></a>
# 4. 连接查询
Expand All @@ -135,7 +141,7 @@
* `SELECT` column_names `FROM` table1 `RIGHT JOIN` table2 `ON` table1.column_name=table2.column_name;

### **FULL (OUTER) JOIN**: 全外连接,全连接是左右外连接的并集. 连接表包含被连接的表的所有记录, 如果缺少匹配的记录, 以 NULL 填充。
* `SELECT` column_names `FROM` table1 ``FULL OUTER JOIN`` table2 `ON` table1.column_name=table2.column_name;
* `SELECT` column_names `FROM` table1 `FULL OUTER JOIN` table2 `ON` table1.column_name=table2.column_name;

### **Self JOIN**: 自连接,表自身连接
* `SELECT` column_names `FROM` table1 T1, table1 T2 `WHERE` condition;
Expand Down Expand Up @@ -164,8 +170,15 @@
### **DROP**: 删除字段
* `ALTER TABLE` table_name `DROP COLUMN` column_name;

<a name="create"></a>
# 7. 创建表的查询



### **CREATE**: 创建一张表
* `CREATE TABLE` table_name `(` <br />
`column1` `datatype`, <br />
`column2` `datatype`, <br />
`column3` `datatype`, <br />
`column4` `datatype`, <br />
`);`