SELECT语句用于从一个或多个表中查询数据,具体语法如下所示。

[ WITH with_query [, ...] ]
SELECT
[ ALL | DISTINCT ] select_expr [, ...]
[ FROM table_reference [, ...] ]
[ WHERE condition ]
[ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ]
[ HAVING condition]
[ WINDOW window_name AS (window_spec) [, window_name AS (window_spec)] ...]
[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
[ ORDER BY {column_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]       
  • table_reference:查询的数据源,可以是表、视图、关联表或者子查询。
  • 表名和列名不缺分大小写。
  • 表名和列名中如果含有关键字或者空格等字符,可以使用反引号(``)将其引起来。

WHERE

WHERE关键字后跟BOOLEAN表达式,用于从表中查询满足条件的数据。例如,在CUSTOMER表中查询customer_id2368的顾客信息。

SELECT * FROM CUSTOMER where customer_id=2368;          

ALL和DISTINCT

ALL和DISTINCT关键字用于指定查询结果是否返回重复的行,默认值为ALL,即返回所有匹配的行,DISTINCT将从结果集中删除重复的行。

SELECT col1, col2 FROM t1;
SELECT DISTINCT col1, col2 FROM t1;          

以下为SELECT中的其他关键字用法。