A SELECT statement filters 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

  • Test statements
    SELECT * FROM Table name;
  • Test results
    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

  • Test statements
    SELECT a, c AS d FROM Table name;
  • Test results
    a (VARCHAR) d (DATE)
    a1 1990-02-20
    b1 2018-05-12
    c1 2010-06-14
    a1 2016-04-05

Example 3

  • Test statements
    SELECT DISTINCT a FROM Table name;
  • Test results
    a (VARCHAR)
    a1
    b1
    c1

Subquery

Generally, a SELECT statement reads data from several tables, such as SELECT column_1, column_2 … FROM table_name. A SELECT statement can also read data from another SELECT statement, which is called a subquery.

Note A subquery must use aliases, as shown in the following example.
  • Sample statements
    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;
  • Sample results
    a (VARCHAR) b (INT)
    a1 211
    b1 120
    a1 257