Skip to main content
Version: V1.4.0

SELECT

Description​

This statement is used to query data from one or more tables.

This section mainly introduces the general SELECT syntax. For more information about other SELECT statements, see:

Permissions​

To execute the SELECT statement, the current user must have the SELECT permission. For more information about the permissions of seekdb, see Permission classification of seekdb.

Syntax​

select_stmt:
SELECT [hint_options]
[{DISTINCT | UNIQUE} | ALL]
[SQL_CALC_FOUND_ROWS]
[STRAIGHT_JOIN]
select_expr_list
[FROM from_list]
[WHERE where_condition]
[GROUP BY group_by_condition_list [WITH ROLLUP]
| GROUP BY [group_by_condition_list] group_by_summary_option (expression_list)]
[HAVING having_condition]
[window_clause]
[ORDER BY order_by_condition_list]
[LIMIT limit_clause]
[lock_option];

select_expr_list:
select_expr [, select_expr ...]

select_expr:
*
| table_name.{* | column_name}
| table_alias_name.{* | column_name}
| expr [[AS] column_alias_name]

from_list:
DUAL
| table_references

where_condition:
expression

group_by_condition_list:
group_by_condition [, group_by_condition ...]

group_by_condition:
expression [ASC | DESC]

group_by_summary_option:
GROUPING SETS
| ROLLUP
| CUBE

expression_list:
expression [, expression ...]

having_condition:
expression

order_by_condition_list:
order_by_condition [, order_by_condition ...]

order_by_condition:
expression [ASC | DESC]

limit_clause:
[offset,] row_count
| row_count OFFSET offset

lock_option:
FOR UPDATE [opt_for_update_wait]
| LOCK IN SHARE MODE

opt_for_update_wait:
WAIT {decimal | intnum}
| NOWAIT
| NO_WAIT
| SKIP LOCKED

Parameter description​

ParameterDescription
select_stmtThe SQL statement part that specifies which data to retrieve from the database.
hint_optionsOptional. Specifies the hint option. For more information about hints, see Optimizer Hint.
{DISTINCT | UNIQUE} | ALLOptional. Specifies whether to return duplicate rows in the result set.
  • DISTINCT/UNIQUE: Indicates that the result set does not contain duplicate rows.
  • ALL: Indicates that all rows, including duplicates, are returned. The default value is ALL.
SQL_CACHE | SQL_NO_CACHEOptional. Specifies whether to cache the query result.
  • SQL_CACHE: Indicates that the query result is cached.
  • SQL_NO_CACHE: Indicates that the query result is not cached.
SQL_CALC_FOUND_ROWSOptional. Records the total number of rows returned in a query with the LIMIT clause. You can then call the FOUND_ROWS() function to obtain the total number of rows.
STRAIGHT_JOINOptional. Forces the optimizer to join tables in the order specified in the FROM clause. This command does not affect the execution result.:::tip seekdb does not fully support the STRAIGHT_JOIN syntax in MySQL. If the STRAIGHT_JOIN clause specifies the join order of tables, seekdb executes the clause. If the STRAIGHT_JOIN clause cannot uniquely determine the join order, seekdb ignores the clause. :::
select_expr_listThe list of columns or expressions to display in the query result. For more information about the columns or expressions, see select_expr.
column_nameThe name of a column.
FROM from_listOptional. Specifies the data source for the query. The data source can be a table, a view, or a subquery. For more information about the data source, see from_list.
WHERE where_conditionOptional. Specifies the filtering condition for the query. For more information, see where_condition.
GROUP BY group_by_condition_listOptional. Groups the result set by the specified columns. This clause is typically used with aggregate functions. For more information, see group_by_condition.
WITH ROLLUPOptional. Aggregates each group to represent a higher-level aggregation (also known as super-aggregation) and generates additional rows.
GROUP BY [group_by_condition_list] group_by_summary_option (expression_list)Optional. Performs advanced grouping and aggregation operations.
  • group_by_condition_list: Optional. Specifies the columns or expressions by which to group. For more information, see group_by_condition.
    • When you explicitly specify group_by_condition_list, these columns are considered fixed grouping conditions. They are combined with subsequent group_by_summary_option to form the final grouping logic. For example, GROUP BY col1, GROUPING SETS ((col2), (col3)) is equivalent to GROUP BY GROUPING SETS ((col1, col2), (col1, col3)).
    • If you do not specify group_by_condition_list, all grouping logic is determined by group_by_summary_option. In other words, the grouping conditions are entirely based on the columns or expressions in expression_list.
  • group_by_summary_option: Specifies the advanced aggregation option. For more information, see group_by_summary_option.
  • expression_list: Specifies the list of one or more column names, aliases, or expressions. This list is used to define the aggregation dimensions.
:::info seekdb supports advanced grouping and aggregation operations. :::
HAVING having_conditionOptional. Filters the result set after grouping. The HAVING clause is similar to the WHERE clause, but it can use aggregate functions such as SUM and AVG. For more information, see having_condition.
window_clauseOptional. Specifies the window definition for the analysis function (also known as window function in some databases). For more information about the window_clause syntax, see WINDOW clause.
ORDER BY order_by_condition_listOptional. Sorts the result set. You can specify one or more columns for sorting. For more information, see order_by_condition.
LIMIT limit_clauseOptional. Specifies the maximum number of rows to return. For more information, see limit_clause.
lock_optionOptional. Locks the query result. For more information, see lock_option.

select_expr​

select_expr: specifies the columns or expressions in the query result. Multiple expressions or column names are separated by commas (,). The value can be as follows:

  • *: specifies all columns.

    Here is an example:

    1. Create tables tbl1 and tbl2.

      CREATE TABLE tbl1(col1 INT PRIMARY KEY, col2 VARCHAR(20), col3 INT)
      PARTITION BY HASH(col1)
      PARTITIONS 5;
      CREATE TABLE tbl2(col1 INT PRIMARY KEY, col2 VARCHAR(20), col3 INT);
    2. Insert test data into tbl1.

      INSERT INTO tbl1 VALUES(1, 'A1', 1),(2, 'A2', 2),(3, 'A3', 3);
      INSERT INTO tbl2 VALUES(1, 'A1', 1),(2, 'A2', 22),(3, 'A3', 33);
    3. Query data from tbl1.

      SELECT * FROM tbl1;

      The return result is as follows:

      +------+------+------+
      | col1 | col2 | col3 |
      +------+------+------+
      | 1 | A1 | 1 |
      | 2 | A2 | 2 |
      | 3 | A3 | 3 |
      +------+------+------+
      3 rows in set (0.022 sec)
  • table_name.{* | column_name}: specifies all columns or a specific column in the specified table.

    • table_name.*: specifies all columns in the specified table.

    • table_name.column_name: specifies a specific column in the specified table.

      Here is an example:

      Query the col1 column from tbl1.

      SELECT tbl1.col1 FROM tbl1;

      The return result is as follows:

      +------+
      | col1 |
      +------+
      | 1 |
      | 2 |
      | 3 |
      +------+
      3 rows in set (0.025 sec)
  • table_alias_name.{* | column_name}: similar to the previous format, but the column is selected using the table alias.

  • expr [[AS] column_alias_name]: specifies an expression and allows you to assign an alias to the selected expression as the column name.

    • expr: specifies a column name, function, or calculation expression.

      • expr AS column_alias_name/expr column_alias_name: specifies the expression and assigns the alias column_alias_name to it.

    Here is an example:

    Query the col1 column from tbl1, calculate a new column col1_add with the value col1+1, and calculate a new column col3_mul with the value col3*2.

    SELECT col1, col1+1 AS col1_add, col3*2 col3_mul FROM tbl1;

    The return result is as follows:

    +------+----------+----------+
    | col1 | col1_add | col3_mul |
    +------+----------+----------+
    | 1 | 2 | 2 |
    | 2 | 3 | 4 |
    | 3 | 4 | 6 |
    +------+----------+----------+
    3 rows in set (0.027 sec)

from_list​

  • DUAL: specifies a virtual table name. It is typically used to perform calculations or execute functions without an actual table.

    Here is an example:

    SELECT 1+1, SYSDATE() FROM DUAL;

    The return result is as follows:

    +------+---------------------+
    | 1+1 | SYSDATE() |
    +------+---------------------+
    | 2 | 2024-08-28 15:20:59 |
    +------+---------------------+
    1 row in set (0.009 sec)
  • table_references: specifies a list of table references. Multiple table references are separated by commas (,). It specifies the list of data source tables for the query, which can be a combination of one or more tables. For more information about the syntax of table_references, see JOIN clause.

where_condition​

expression: specifies the condition expression used to filter the rows to be updated.

Here is an example:

Query all rows from tbl1 that satisfy the conditions col1 > 1 and col2 = 'A3'.

SELECT * FROM tbl1
WHERE col1 > 1
AND col2 = 'A3';

The return result is as follows:

+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 3 | A3 | 3 |
+------+------+------+
1 row in set (0.003 sec)

group_by_condition​

expression [ASC | DESC]: specifies the grouping condition expression. Adding ASC or DESC controls the order of groups in the result set, specifying whether to sort them in ascending or descending order.

Here is an example:

Query col1 and col2 from table tbl1, group by col2, and sum col3, then output the result.

SELECT col1, col2, SUM(col3)
FROM tbl1
GROUP BY col2 DESC;

The result is as follows:

+------+------+-----------+
| col1 | col2 | SUM(col3) |
+------+------+-----------+
| 3 | A3 | 3 |
| 2 | A2 | 2 |
| 1 | A1 | 1 |
+------+------+-----------+
3 rows in set (0.017 sec)

group_by_summary_option​

info

seekdb supports advanced grouping and summarizing operations.

  • GROUPING SETS: specifies a custom grouping method, allowing you to define multiple grouping combinations. It has the following characteristics:

    • You can flexibly define the grouping combinations as needed.
    • The result set includes only the grouping combinations specified by you.
    info

    GROUP BY GROUPING SETS ((col1), (col2), (col3)) is equivalent to GROUP BY GROUPING SETS (col1, col2, col3).

    Here is an example:

    SELECT col1, col2, col3, COUNT(*)
    FROM tbl1
    GROUP BY GROUPING SETS ((col1), (col2), (col3));

    The returned result is as follows:

    +------+------+------+----------+
    | col1 | col2 | col3 | COUNT(*) |
    +------+------+------+----------+
    | 1 | NULL | NULL | 1 |
    | 2 | NULL | NULL | 1 |
    | 3 | NULL | NULL | 1 |
    | NULL | A1 | NULL | 1 |
    | NULL | A2 | NULL | 1 |
    | NULL | A3 | NULL | 1 |
    | NULL | NULL | 1 | 1 |
    | NULL | NULL | 2 | 1 |
    | NULL | NULL | 3 | 1 |
    +------+------+------+----------+
    9 rows in set (0.016 sec)
  • ROLLUP: specifies a hierarchical summarizing method. It reduces the number of grouping columns in the order of the grouping columns and generates multi-level summarizing results. It has the following characteristics:

    • It reduces the grouping columns in the order of the columns in expression_list until only the total row remains.

    • The result set includes:

      • The detailed data of each group.
      • The sub-summarized data of each group.
      • The total row.

    ROLLUP can be considered a special case of GROUPING SETS. For example:

    GROUP BY ROLLUP(col1, col2, col3)

    is equivalent to

    GROUP BY GROUPING SETS ((col1, col2, col3), (col1, col2), (col1), ())

    ROLLUP can also be used with GROUPING SETS. For example:

    GROUP BY GROUPING SETS((col2), (col3), ROLLUP(col1, col2, col3))

    is equivalent to

    GROUP BY GROUPING SETS((col2), (col3), (col1, col2, col3), (col1, col2), (col1), ())

    Here is an example:

    SELECT col1, col2, col3, COUNT(*)
    FROM tbl1
    GROUP BY ROLLUP (col1, col2, col3);

    The returned result is as follows:

    +------+------+------+----------+
    | col1 | col2 | col3 | COUNT(*) |
    +------+------+------+----------+
    | 1 | A1 | 1 | 1 |
    | 1 | A1 | NULL | 1 |
    | 1 | NULL | NULL | 1 |
    | 2 | A2 | 2 | 1 |
    | 2 | A2 | NULL | 1 |
    | 2 | NULL | NULL | 1 |
    | 3 | A3 | 3 | 1 |
    | 3 | A3 | NULL | 1 |
    | 3 | NULL | NULL | 1 |
    | NULL | NULL | NULL | 3 |
    +------+------+------+----------+
    10 rows in set (0.109 sec)
  • CUBE: specifies a multi-dimensional summarizing method. It generates all possible grouping combinations. It has the following characteristics:

    • The detailed data of each group.
    • The summarized data of all possible grouping combinations.
    • The total row.

    CUBE can be considered a special case of GROUPING SETS. For example:

    GROUP BY CUBE(col1, col2, col3)

    is equivalent to

    GROUP BY GROUPING SETS ((col1, col2, col3), (col1, col2), (col1, col3), (col2, col3), (col1), (col2), (col3), ())

    CUBE can also be used with GROUPING SETS. For example:

    GROUP BY col1, CUBE(col2, col3), GROUPING SETS((col4), (col5))

    is equivalent to

    GROUP BY GROUPING SETS(
    (col1, col2, col3, col4), (col1, col2, col3, col5),
    (col1, col2, col4), (col1, col2, col5),
    (col1, col3, col4), (col1, col3, col5),
    (col1, col4), (col1, col5))

    Here is an example:

    SELECT col1, col2, col3, COUNT(*)
    FROM tbl1
    GROUP BY CUBE (col1, col2, col3);

    The returned result is as follows:

    +------+------+------+----------+
    | col1 | col2 | col3 | COUNT(*) |
    +------+------+------+----------+
    | NULL | NULL | NULL | 3 |
    | NULL | NULL | 1 | 1 |
    | NULL | NULL | 2 | 1 |
    | NULL | NULL | 3 | 1 |
    | NULL | A1 | NULL | 1 |
    | NULL | A2 | NULL | 1 |
    | NULL | A3 | NULL | 1 |
    | NULL | A1 | 1 | 1 |
    | NULL | A2 | 2 | 1 |
    | NULL | A3 | 3 | 1 |
    | 1 | NULL | NULL | 1 |
    | 2 | NULL | NULL | 1 |
    | 3 | NULL | NULL | 1 |
    | 1 | NULL | 1 | 1 |
    | 2 | NULL | 2 | 1 |
    | 3 | NULL | 3 | 1 |
    | 1 | A1 | NULL | 1 |
    | 2 | A2 | NULL | 1 |
    | 3 | A3 | NULL | 1 |
    | 1 | A1 | 1 | 1 |
    | 2 | A2 | 2 | 1 |
    | 3 | A3 | 3 | 1 |
    +------+------+------+----------+
    22 rows in set (0.113 sec)

having_condition​

expression: specifies the condition expression for filtering the grouped results.

Here is an example:

Query the col1 and col2 columns from the tbl1 table, group the results by col2, and calculate the sum of col3. Return only the rows where the sum of col3 is less than 3.

SELECT col1, col2, SUM(col3)
FROM tbl1
GROUP BY col2
HAVING SUM(col3) < 3;

The returned result is as follows:

+------+------+-----------+
| col1 | col2 | SUM(col3) |
+------+------+-----------+
| 1 | A1 | 1 |
| 2 | A2 | 2 |
+------+------+-----------+
2 rows in set (0.010 sec)

order_by_condition​

expression [ASC | DESC]: specifies the condition expression for sorting the result set.

ASC | DESC: an optional parameter that specifies the sorting order. ASC indicates ascending order (default), and DESC indicates descending order.

Here is an example:

Query the data from the tbl1 table and output the query results in descending order (DESC) based on the col3 column.

SELECT * FROM tbl1
ORDER BY col3 DESC;

The returned result is as follows:

+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 3 | A3 | 3 |
| 2 | A2 | 2 |
| 1 | A1 | 1 |
+------+------+------+
3 rows in set (0.012 sec)

limit_clause​

  • [offset,] row_count: the parameter values must be integer constants. The following table describes the parameters.

    • offset: an optional parameter that specifies the number of rows to skip from the start of the result set (offset). The default offset is 0.
    • row_count: specifies the number of rows to return. If you specify only the row_count parameter, the first row_count rows of the result set are returned.
  • row_count OFFSET offset: specifies to skip offset rows from the start of the result set and return the next row_count rows.

Here is an example:

Query the data from the tbl1 table, force the query to start from the second row, and return two rows of data from the tbl1 table.

SELECT * FROM tbl1
LIMIT 1, 2;

The returned result is as follows:

+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
| 2 | A2 | 2 |
| 3 | A3 | 3 |
+------+------+------+
2 rows in set (0.011 sec)

lock_option​

  • FOR UPDATE [opt_for_update_wait]: This option specifies that exclusive locks are placed on all rows of the query result set to prevent concurrent modifications by other transactions or concurrent reads at certain transaction isolation levels. For more information about exclusive locks, see Lock query results with SELECT FOR UPDATE.

    • opt_for_update_wait is an optional parameter that specifies the behavior for acquiring locks. The options are as follows:

      • WAIT {decimal | intnum}: Specifies the amount of time to wait for other transactions to release resources. If the time limit is exceeded, the resource cannot be obtained. The unit is seconds.
      • NOWAIT/NO_WAIT: Specifies not to wait for other transactions to release resources and to directly return that the resource cannot be obtained.
      • SKIP LOCKED: Specifies not to wait for row locks. The query is executed immediately, and locked rows are removed from the result set.

    Here are some examples:

    • You can use SELECT ... FOR UPDATE to add row-level locks to a table. If the LIMIT 1 clause is used, the optimizer will push the operator down to the table scan step, and only the rows returned by LIMIT will be locked.

      SELECT * FROM tbl1
      LIMIT 1
      FOR UPDATE;
    • If you use the ORDER BY clause to sort the query results, the results will be sorted first, and then LIMIT 1 will be executed. In this case, all selected rows will be locked.

      SELECT * FROM tbl1
      ORDER BY col1
      LIMIT 1
      FOR UPDATE;
  • LOCK IN SHARE MODE: This option specifies that shared locks are placed on the data during the query to prevent other transactions from writing to the data, but allows other transactions to read the data. For more information about shared locks, see Lock query results with LOCK IN SHARE MODE.

Examples​

Example of a simple table query​

  1. Create a table named test_tbl1.

    CREATE TABLE test_tbl1(col1 INT PRIMARY KEY, col2 VARCHAR(20), col3 INT);
  2. Insert test data into the test_tbl1 table.

    INSERT INTO test_tbl1 VALUES (1, 'A1', 10),(2, 'A2', 15),(3, 'A1', 8);
  3. Query the col2 column in the test_tbl1 table and remove duplicates.

    SELECT DISTINCT col2 FROM test_tbl1;

    The returned result is as follows:

    +------+
    | col2 |
    +------+
    | A1 |
    | A2 |
    +------+
    2 rows in set (0.014 sec)

References​

Single-table query