SELECT
The SELECT statement is used to read data from TiDB.
Synopsis
SelectStmt:

FromDual:

WhereClauseOptional:

SelectStmtOpts:

SelectStmtFieldList:

TableRefsClause:
- TableRefsClause
- AsOfClause
TableRefsClause ::=
    TableRef AsOfClause? ( ',' TableRef AsOfClause? )*
AsOfClause ::=
    'AS' 'OF' 'TIMESTAMP' Expression
WhereClauseOptional:

SelectStmtGroup:

HavingClause:

OrderByOptional:

SelectStmtLimit:

FirstOrNext:

FetchFirstOpt:

RowOrRows:

SelectLockOpt:
- SelectLockOpt
- TableList
SelectLockOpt ::=
    ( ( 'FOR' 'UPDATE' ( 'OF' TableList )? 'NOWAIT'? )
|   ( 'LOCK' 'IN' 'SHARE' 'MODE' ) )?
TableList ::=
    TableName ( ',' TableName )*
WindowClauseOptional

Description of the syntax elements
| Syntax Element | Description | 
|---|---|
| TableOptimizerHints | This is the hint to control the behavior of TiDB's optimizer. For more information, refer to Optimizer Hints. | 
| ALL,DISTINCT,DISTINCTROW | The ALL,DISTINCT/DISTINCTROWmodifiers specify whether duplicate rows should be returned. ALL (the default) specifies that all matching rows should be returned. | 
| HIGH_PRIORITY | HIGH_PRIORITYgives the current statement higher priority than other statements. | 
| SQL_CALC_FOUND_ROWS | TiDB does not support this feature, and will return an error unless tidb_enable_noop_functions=1is set. | 
| SQL_CACHE,SQL_NO_CACHE | SQL_CACHEandSQL_NO_CACHEare used to control whether to cache the request results to theBlockCacheof TiKV (RocksDB). For a one-time query on a large amount of data, such as thecount(*)query, it is recommended to fill inSQL_NO_CACHEto avoid flushing the hot user data inBlockCache. | 
| STRAIGHT_JOIN | STRAIGHT_JOINforces the optimizer to do a union query in the order of the tables used in theFROMclause. When the optimizer chooses a join order that is not good, you can use this syntax to speed up the execution of the query. | 
| select_expr | Each select_exprindicates a column to retrieve. including the column names and expressions.\*represents all the columns. | 
| FROM table_references | The FROM table_referencesclause indicates the table (such asselect * from t;), or tables (such asselect * from t1 join t2;) or even 0 tables (such asselect 1+1 from dual;which is equivalent toselect 1+1;) from which to retrieve rows. | 
| WHERE where_condition | The WHEREclause, if given, indicates the condition or conditions that rows must satisfy to be selected. The result contains only the data that meets the condition(s). | 
| GROUP BY | The GROUP BYstatement is used to group the result-set. | 
| HAVING where_condition | The HAVINGclause and theWHEREclause are both used to filter the results. TheHAVINGclause filters the results ofGROUP BY, while theWHEREclause filter the results before aggregation. | 
| ORDER BY | The ORDER BYclause is used to sort the data in ascending or descending order, based on columns, expressions or items in theselect_exprlist. | 
| LIMIT | The LIMITclause can be used to constrain the number of rows.LIMITtakes one or two numeric arguments. With one argument, the argument specifies the maximum number of rows to return, the first row to return is the first row of the table by default; with two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. TiDB also supports theFETCH FIRST/NEXT n ROW/ROWS ONLYsyntax, which has the same effect asLIMIT n. You can omitnin this syntax and its effect is the same asLIMIT 1. | 
| Window window_definition | This is the syntax for window function, which is usually used to do some analytical computation. For more information, refer to Window Function. | 
| FOR UPDATE | The SELECT FOR UPDATEclause locks all the data in the result sets to detect concurrent updates from other transactions. Data that match the query conditions but do not exist in the result sets are not read-locked, such as the row data written by other transactions after the current transaction is started. TiDB uses the Optimistic Transaction Model. The transaction conflicts are not detected in the statement execution phase. Therefore, the current transaction does not block other transactions from executingUPDATE,DELETEorSELECT FOR UPDATElike other databases such as PostgreSQL. In the committing phase, the rows read bySELECT FOR UPDATEare committed in two phases, which means they can also join the conflict detection. If write conflicts occur, the commit fails for all transactions that include theSELECT FOR UPDATEclause. If no conflict is detected, the commit succeeds. And a new version is generated for the locked rows, so that write conflicts can be detected when other uncommitted transactions are being committed later. When using pessimistic transaction mode, the behavior is basically the same as other databases. Refer to Difference with MySQL InnoDB to see the details. TiDB supports theNOWAITmodifier forFOR UPDATE. See TiDB Pessimistic Transaction Mode for details. | 
| LOCK IN SHARE MODE | To guarantee compatibility, TiDB parses these three modifiers, but will ignore them. | 
Examples
mysql> CREATE TABLE t1 (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, c1 INT NOT NULL);
Query OK, 0 rows affected (0.11 sec)
mysql> INSERT INTO t1 (c1) VALUES (1),(2),(3),(4),(5);
Query OK, 5 rows affected (0.03 sec)
Records: 5  Duplicates: 0  Warnings: 0
mysql> SELECT * FROM t1;
+----+----+
| id | c1 |
+----+----+
|  1 |  1 |
|  2 |  2 |
|  3 |  3 |
|  4 |  4 |
|  5 |  5 |
+----+----+
5 rows in set (0.00 sec)
MySQL compatibility
- The syntax SELECT ... INTO @variableis not supported.
- The syntax SELECT ... GROUP BY ... WITH ROLLUPis not supported.
- The syntax SELECT .. GROUP BY exprdoes not implyGROUP BY expr ORDER BY expras it does in MySQL 5.7. TiDB instead matches the behavior of MySQL 8.0 and does not imply a default order.
See also
Was this page helpful?