SQL3:
1. Using comments
use--(two dashes) to comment out everything to the right of them on a given line
leave comments across multiple lines using /* to begin the comment and */ to close it(以/*开始,以*/结束)
可以用comments对query中可能产生的错误进行标注,如在SELECT中任一list前加--(two dashes)可使该list不产生results,以检查是否该list出现了错误(debug)
Basic SQL结束
二. Intermediate SQL
1. SQL Aggregate Functions
Arithmetic operators only perform operations across rows. Aggragate functions are used to perform operations across entire columns.
COUNT: SELECT COUNT (*) 或 (1)
不包含空值(not include null value)
SUM: only used on columns containing numerical values;
only aggragate vertically;
treats nulls as 0
MIN/MAX: SQL aggregation functions that return the lowest and highest values in a particular column.
MIN will return the lowest number, earliest date, or non-numerical value as close alphabetically to "A" as possible.
MAX returns the highest number, the latest date, or the non-numerical value closest alphabetically to "Z"
AVG: only be used on numerical columns and ignores nulls completely
2. GROUP BY: aggregate only part of a table, separate data into groups, which can be aggregated independently of one another
GROUP by multiple columns- separate column names with commas.
Using GROUP BY with ORDER BY(把GROUP BY的结果进行排序整理)
最后可加LIMIT限制显示数据结果
注:GROUP BY出现的部分必须要出现在SELECT中(SELECT要与GROUP BY的内容一致)
3. HAVING: to filter a query that has been aggregated.(作用类似于WHERE,但WHERE doesn't allow you to filter on aggregate columns),一般用在GROUP BY之后
WHERE在aggregate之前使用,HAVING在aggregate之后使用
Query clause order: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT
4. CASE WHEN... THEN: SQL's way of handling if/then logic
always goes in the SELECT clause; every CASE statement must end with the END statement; the ELSE statement is optional, and provides a way to capture values not specified in the WHEN/THEN statements.
常见:CASE WHEN... THEN... (ELSE) END