SELECT

Warning

This documentation does not reflect the latest version of MemSQL.
This page refers to MemSQL 4.0.

For the latest MemSQL product documentation, visit http://docs.memsql.com/latest.

Retrieve data from a table.

Syntax

SELECT
    [ALL | DISTINCT | DISTINCTROW]
    [SQL_CALC_FOUND_ROWS]
    select_expr [[AS] alias], ...
    [FROM table_references
        [WHERE expr]
        [GROUP BY {col_name | expr | position}, ...]
        [HAVING expr]
        [ORDER BY {col_name | expr | position} [ASC | DESC], ...]
        [LIMIT {[offset,] row_count | row_count OFFSET offset}]
        [INTO OUTFILE 'file_name' export_options]
    ]

Notes

  • export_options clauses are documented in the SELECT ... INTO OUFILE below.

Aggregations for Expression Syntax

MemSQL supports the following aggregations for expression syntax in SELECT statements:

COUNT(*)                        -- This returns the count of rows matching the query
COUNT(expr)                     -- This returns the count of rows matching the query where the expression is non-NULL
COUNT(DISTINCT expr)            -- This returns the count over distinct non-NULL values of the expression
APPROX_COUNT_DISTINCT(expr)     -- This returns an approximation of the number of distinct values of the expression
SUM(expr)                       -- This returns the sum value of the expression
SUM(DISTINCT expr)              -- This returns the sum among distinct values of the expression
AVG(expr)                       -- This returns the average value of the expression
AVG(DISTINCT expr)              -- This returns the average among distinct values of the expression
MAX(expr)                       -- This returns the maximum value of the expression
MIN(expr)                       -- This returns the minimum value of the expression
STDDEV_POP(expr)                -- This is the population standard deviation
STD(expr)                       -- This is a synonym for STDDEV_POP
STDDEV(expr)                    -- This is a synonym for STDDEV_POP
STDDEV_SAMP(expr)               -- This is the sample standard deviation
VAR_POP(expr)                   -- This is the population variance
VARIANCE(expr)                  -- This is a synonym for VAR_POP
VAR_SAMP(expr)                  -- This is the sample variance
GROUP_CONCAT(expr)              -- This concatenates the non-NULL values of the expression into a string
GROUP_CONCAT(DISTINCT expr)     -- This concatenates the distinct non-NULL values of the expression into a string

All aggregate functions exclude NULLs from their computations. For example, SELECT COUNT(c) FROM t returns the number of rows in t where c is not NULL. SELECT AVG(c) FROM t returns the average of the non-NULL c‘s in t.

COUNT(*) is equivalent to COUNT(1). It counts every row matching the query, even rows whose column data contain nothing but NULLs.

JOIN and Subqueries

Syntax

MemSQL supports the following JOIN and subquery syntax for the table_reference part of SELECT statements:

table_reference:
         table_factor | join_table

table_factor:
           tbl_name [[AS] alias]
         | (subquery) [AS] alias

join_table:
           table_reference {LEFT | RIGHT} [OUTER] JOIN table_factor join_condition
         | table_reference [INNER | CROSS] JOIN table_factor [join_condition]
         | table_reference NATURAL {LEFT | RIGHT} [OUTER] JOIN table_factor
         | table_reference STRAIGHT_JOIN table_factor [join_condition]
         | table_reference, table_factor

join_condition:
         ON conditional_expr

Notes

This command must be run on the master aggregator or a child aggregator node (see Node Requirements for MemSQL Commands).

MemSQL doesn’t yet support nested joins. However, in many cases you can rewrite queries that seem to require a nested join without one. For example, the following query is not supported:

... FROM tableA LEFT JOIN (tableB LEFT JOIN tableC on tableB.col = tableC.col) ON tableA.col = tableB.col

The previous query can be rewritten without a nested join in the following way:

... FROM tableA LEFT JOIN tableB ON tableA.col = tableB.col LEFT JOIN tableC on tableB.col = tableC.col

Example

memsql> SELECT * FROM my_MemSQL_table WHERE col = 1;


memsql> SELECT COUNT(*), user_name, page_url from clicks, users, pages
 ->     WHERE clicks.user_id = users.user_id AND pages.page_id = clicks.page_id
 ->     GROUP BY users.user_id, pages.page_id
 ->     ORDER BY COUNT(*) DESC;
 +----------+-----------+--------------------------------+
 | COUNT(*) | user_name | page_url                       |
 +----------+-----------+--------------------------------+
 |        5 | jake      | memsql.com                     |
 |        2 | john      | http://www.memsql.com/download |
 |        1 | jake      | developers.memsql.com          |
 |        1 | jake      | memsql.com                     |
 |        1 | jake      | http://www.memsql.com/download |
 +----------+-----------+--------------------------------+
 5 rows in set (0.00 sec)

SELECT ... INTO OUTFILE

SELECT ... INTO OUTFILE formats and writes the results of a SELECT query to a text file. The export_options are very similar to the parsing options used with LOAD DATA.

export_options:
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]

Notes

  • By default, MemSQL writes the file to the directory specified in the global variable tmpdir (The same as datadir by default). To specify another location, enter the absolute path to the file as the file_name parameter.
  • The default text formatting, used when the user omits the FIELDS and LINES blocks, is to separate values with tabs (\t) and rows with newlines (\n).

Example

The following query writes the contents of table_name to a file in the home directory of username. The file is formatted as a comma-separated varaible (CSV) file.

memsql> SELECT * FROM table_name INTO OUTFILE '/home/username/file_name.csv'
        FIELDS TERMINATED BY ','
        LINES TERMINATED BY '\n'

See Also