SELECT statement
The SELECT grammar of Raft DB: joins, grouping, ROLLUP, windows, subqueries, LIMIT, INTO and locking clauses, with the differences between MySQL and MariaDB.
SELECT reads rows. One executor runs every SELECT (D55). It reads the tables the statement names (the first table with the part of the WHERE that concerns only that table, so a primary-key lookup is one read), then works on rows in memory. An equality of two columns of the same kind is joined with a hash; any other join condition is a nested loop. Groups come out in the order their first row was met unless ORDER BY says otherwise.
Syntax#
[WITH [RECURSIVE] cte_name [(col, ...)] AS (query) [, ...]]
SELECT
[ALL | DISTINCT | DISTINCTROW]
[SQL_CALC_FOUND_ROWS]
[HIGH_PRIORITY | STRAIGHT_JOIN | SQL_SMALL_RESULT | SQL_BIG_RESULT
| SQL_BUFFER_RESULT | SQL_CACHE | SQL_NO_CACHE] ...
select_expr [, select_expr] ...
[INTO @var [, @var] ...]
[FROM table_references]
[WHERE condition]
[GROUP BY expr [, expr] ... [WITH ROLLUP]]
[HAVING condition]
[WINDOW window_name AS (window_spec) [, ...]]
[ORDER BY expr [ASC | DESC] [, ...]]
[LIMIT { row_count | offset, row_count | row_count OFFSET offset }]
[OFFSET n {ROW | ROWS} [FETCH {FIRST | NEXT} [n] {ROW | ROWS} ONLY]] -- MariaDB only
[INTO @var [, @var] ...]
[FOR { UPDATE | SHARE } [NOWAIT | SKIP LOCKED] | LOCK IN SHARE MODE]
select_expr:
* | tbl_name.* | expr [[AS] alias]
table_references:
table_factor [join_clause] ...
table_factor:
[db_name.]tbl_name [[AS] alias] [index_hint] ...
| ( query_expression ) [AS] alias
join_clause:
, table_factor
| [INNER | CROSS] JOIN table_factor [ON condition | USING (col, ...)]
| STRAIGHT_JOIN table_factor [ON condition | USING (col, ...)]
| { LEFT | RIGHT } [OUTER] JOIN table_factor { ON condition | USING (col, ...) }
| NATURAL [{ LEFT | RIGHT } [OUTER] | INNER] JOIN table_factor
index_hint:
{ USE | FORCE | IGNORE } { INDEX | KEY } [FOR {JOIN | ORDER BY | GROUP BY}] (index_name, ...)
window_spec:
[window_name] [PARTITION BY expr, ...] [ORDER BY expr [ASC | DESC], ...]
[{ ROWS | RANGE } { frame_start | BETWEEN frame_start AND frame_end }]
frame_start, frame_end:
UNBOUNDED PRECEDING | n PRECEDING | CURRENT ROW | n FOLLOWING | UNBOUNDED FOLLOWINGA function call or an aggregate becomes a window function with OVER window_name or OVER (window_spec). SELECT can also stand as VALUES and TABLE t (see UNION and set operations).
Arguments#
| Name | Meaning | Default |
|---|---|---|
DISTINCT / DISTINCTROW | Remove duplicate rows. | ALL |
SQL_CALC_FOUND_ROWS | Count the rows without LIMIT; read with FOUND_ROWS(). | off |
other SELECT modifiers | HIGH_PRIORITY, STRAIGHT_JOIN, SQL_SMALL_RESULT, SQL_BIG_RESULT, SQL_BUFFER_RESULT, SQL_CACHE, SQL_NO_CACHE are accepted and have no effect. | none |
INTO @var, ... | Put one row into user variables. More than one row is error 1172; no row leaves the variables as they were (as MySQL). | none |
index_hint | Accepted and ignored. | none |
WITH ROLLUP | Add the totals rows after each leading part of the group key. | off |
FOR UPDATE, FOR SHARE, LOCK IN SHARE MODE, NOWAIT, SKIP LOCKED | Accepted. They lock nothing (see transactions). | none |
Remarks#
- Joins.
INNER,CROSS,LEFT,RIGHT, comma andNATURALjoins withONandUSING. A derived table must have an alias (a syntax error otherwise). - Grouping.
GROUP BYtakes expressions, select aliases and positions.ONLY_FULL_GROUP_BYis checked with MySQL's errors 1055 and 1140; grouping by a primary key makes the other columns of its table legal. Aggregates may stand anywhere in an expression. An aggregate over several expressions (other thanGROUP_CONCAT) is refused. GROUP BY ... WITH ROLLUP(D84): groups are put in key order (NULLfirst); after the last group of each leading part of the key a totals row follows with the later grouping columnsNULL; the last row is the grand total. MySQL allowsORDER BYwith it and answers an empty input with the totals row. MariaDB refusesORDER BYwith it (error 1221) and answers nothing for an empty input.GROUP_CONCAT:DISTINCT,ORDER BY,SEPARATOR; MariaDB alsoLIMIT [offset,] ninside the call. The result is cut atgroup_concat_max_len.- Subqueries. Scalar,
IN,EXISTS,ANY/SOME/ALLand row comparisons, correlated or not. A scalar subquery that returns more than one row is error 1242. - Windows.
ROW_NUMBER,RANK,DENSE_RANK,NTILE,PERCENT_RANK,CUME_DIST,LAG,LEAD,FIRST_VALUE,LAST_VALUE,NTH_VALUEand the aggregatesCOUNT,SUM,AVG,MIN,MAXover a window; named windows withWINDOW;ROWSandRANGEframes. Verified against MySQL and MariaDB. A window function inWHEREis an error. LIMIT.LIMIT n,LIMIT offset, n,LIMIT n OFFSET offset. MariaDB only:OFFSET n ROWS FETCH FIRST|NEXT [n] ROWS ONLY. MySQL refuses theFETCHform with a syntax error.- Column names. An unaliased expression is headed with its text as written; an unaliased string literal with the string itself.
- Hex literals.
X'4142'is a string in MariaDB and a number in arithmetic in MySQL. - Privileges.
SELECTon every table of the query, its derived tables and its subqueries, for the columns named where the account has only column grants. - Performance. Reads use Raft DB's indexes: bounds on one field become one index range, and an integer primary key is indexed (D63).
Not supported yet#
Refused with error 1235:
SELECT ... INTO OUTFILEandINTO DUMPFILEare read and refused with error 1290 (--secure-file-priv), as the servers do by default; Raft DB writes no files for a client.FETCH FIRST ... WITH TIES(MariaDB).- Window frames in
GROUPSmode. DISTINCTinside an ordinary function call.- Parenthesised joins such as
FROM (a JOIN b ON ...). LIKE ... ESCAPE, andCOLLATEto a binary or case-sensitive collation.ORDER BYorLIMITafter aVALUESlist.GROUPING()andCUBE.
Examples#
Example:
SELECT c.name, o.id, o.total
FROM cust c JOIN ord o ON o.cust_id = c.id
ORDER BY o.id;
SELECT c.name, COUNT(o.id) n
FROM cust c LEFT JOIN ord o ON o.cust_id = c.id
GROUP BY c.id, c.name ORDER BY n DESC, c.name;
SELECT status, GROUP_CONCAT(id ORDER BY id SEPARATOR '|')
FROM ord GROUP BY status ORDER BY status;
SELECT SQL_CALC_FOUND_ROWS id FROM ord ORDER BY id LIMIT 2;
SELECT FOUND_ROWS();
SELECT id, RANK() OVER w r, SUM(sal) OVER w s
FROM e WINDOW w AS (PARTITION BY dept ORDER BY sal) ORDER BY id;
SELECT y, c, SUM(n), COUNT(*) FROM s GROUP BY y, c WITH ROLLUP;
SELECT x, x * 2 INTO @p, @q FROM a WHERE x = 1;