SELECT

SELECT

Syntax

1
2
3
4
5
6
SELECT [ ALL | DISTINCT ]
  { * | projectItem [, projectItem ]* }
  FROM tableExpression
  [ WHERE booleanExpression ]
  [ GROUP BY { groupItem [, groupItem ]* } ]
  [ HAVING booleanExpression ]

Description

SELECT is used to select data from a table.

ALL indicates that all results are returned.

DISTINCT indicates that the duplicated results are removed.

Precautions

Example

Select the order which contains more than 3 pieces of data.

1
insert into temp SELECT  * FROM Orders WHERE units > 3; 

Insert a group of constant data.

1
insert into temp select 'Lily', 'male', 'student', 17;

WHERE

Syntax

1
2
3
SELECT   { * | projectItem [, projectItem ]* }
  FROM tableExpression
  [ WHERE booleanExpression ]

Description

This clause is used to filter the query results using the WHERE clause.

Precautions

Example

Search orders which contain more than 3 pieces and fewer than 10 pieces of data.

1
2
insert into temp SELECT  * FROM Orders
  WHERE units > 3 and units < 10; 

HAVING

Function

This clause is used to search for the query results that meet the search condition.

Syntax

1
2
3
4
5
SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
  FROM tableExpression
  [ WHERE booleanExpression ]
  [ GROUP BY { groupItem [, groupItem ]* } ]
  [ HAVING booleanExpression ]

Description

Generally, HAVING and GROUP BY are used together. You can use GROUP BY for grouping and then use HAVING for filtering. Arithmetic operations and aggregate functions are supported in the HAVING clause.

Precautions

If the filtering condition is subject to the results of GROUP BY, the HAVING clause, rather than the WHERE clause, must be used for search.

Example

Group the student table according to the name field and search for the records in which the maximum score is higher than 95 in the group.

1
2
3
insert into temp SELECT name, max(score) FROM student
  GROUP BY name
  HAVING max(score) >95;

Column-Based GROUP BY

Function

This clause is used to group a table based on columns.

Syntax

1
2
3
4
SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
  FROM tableExpression
  [ WHERE booleanExpression ]
  [ GROUP BY { groupItem [, groupItem ]* } ]

Description

Column-based GROUP BY can be categorized into single-column GROUP BY and multi-column GROUP BY.

Precautions

GroupBy generates update results in the stream processing table.

Example

Group the student table according to the score and name fields and return the grouping results.

1
2
insert into temp SELECT name,score, max(score) FROM student 
  GROUP BY name,score;

Expression-Based GROUP BY

Function

This clause is used to group streams according to expressions.

Syntax

1
2
3
4
SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
  FROM tableExpression
  [ WHERE booleanExpression ]
  [ GROUP BY { groupItem [, groupItem ]* } ]

Description

groupItem can have one or more fields. The fields can be called by string functions, but cannot be called by aggregate functions.

Precautions

None

Example

Use the substring function to obtain the character string from the name field, group the student table according to the obtained character string, and return each sub character string and the number of records.

1
2
insert into temp SELECT substring(name,6),count(name) FROM student
  GROUP BY substring(name,6);

Grouping sets, Rollup, Cube

Function

Syntax
SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
  FROM tableExpression
  [ WHERE booleanExpression ]
  [ GROUP BY groupingItem]

Description

Values of groupingItem can be Grouping sets(columnName [, columnName]*), Rollup(columnName [, columnName]*), and Cube(columnName [, columnName]*).

Precautions

None

Example

Return the results generated based on user and product.

INSERT INTO temp SELECT SUM(amount)
FROM Orders
GROUP BY GROUPING SETS ((user), (product));

GROUP BY Using HAVING

Function

This clause filters a table after grouping it using the HAVING clause.

Syntax

1
2
3
4
5
SELECT [ ALL | DISTINCT ]   { * | projectItem [, projectItem ]* }
  FROM tableExpression
  [ WHERE booleanExpression ]
  [ GROUP BY { groupItem [, groupItem ]* } ]
  [ HAVING booleanExpression ]

Description

Generally, HAVING and GROUP BY are used together. You can use GROUP BY for grouping and the HAVING for filtering.

Precautions

Example

Group the transactions by num, use the HAVING clause to search for the records in which the maximum value derived from multiplying price with amount is higher than 5000, and return the filtered results.

1
2
3
4
insert into temp SELECT num, max(price*amount) FROM transactions
  WHERE time > '2016-06-01'
  GROUP BY num
  HAVING max(price*amount)>5000;