The SELECT statement is used to read data from tables.
Syntax
SELECT [ DISTINCT ]
{ * | projectItem [, projectItem ]* }
FROM tableExpression;
Test data
a (VARCHAR) | b (INT) | c (DATE) |
---|---|---|
a1 | 211 | 1990-02-20 |
b1 | 120 | 2018-05-12 |
c1 | 89 | 2010-06-14 |
a1 | 46 | 2016-04-05 |
Example 1
- Statement
SELECT * FROM table_name;
- Result
a (VARCHAR) b (INT) c (DATE) a1 211 1990-02-20 b1 120 2018-05-12 c1 89 2010-06-14 a1 46 2016-04-05
Example 2
- Statement
SELECT a, c AS d FROM table_name;
- Result
a (VARCHAR) d (DATE) a1 1990-02-20 b1 2018-05-12 c1 2010-06-14 a1 2016-04-05
Example 3
- Statement
SELECT DISTINCT a FROM table_name;
- Result
a (VARCHAR) a1 b1 c1
Subquery
In most scenarios, the SELECT statement reads data from several tables, for example,
SELECT column_1, column_2 … FROM table_name
.
Note If the query object is another SELECT operation, you must specify an alias for the
subquery.
- Example
INSERT INTO result_table SELECT * FROM (SELECT t.a, sum(t.b) AS sum_b FROM t1 t GROUP BY t.a ) t1 WHERE t1.sum_b > 100;
- Result
a (VARCHAR) b (INT) a1 211 b1 120 a1 257