David Andrei Ned David Andrei Ned - 23 days ago 7
MySQL Question

A single SQL query showing all expressions being used (to be used as a guide)

I've been looking for a single query which shows the order in which a query ought to be written. Having failed to find one, I am now attempting to make one myself.

Help me build up on this single query showing all the various expressions possible in SQL:

SELECT AVG/MIN/MAX/SUM/COUNT(DISTINCT field_list) AS custom_name
FROM table1
JOIN field_list2
ON table2_field_to_join_on
= table1_field_to_join_on
WHERE condition
GROUP BY field_list_condition
HAVING conditions
ORDER BY field_list_order_condition ASC(default)/DESC
LIMIT number_of_records;


I doubt this is exhaustive, so let's make it so.

Database engine unimportant, I guess we can start with Postgres/MySQL and show how the others differ.

Answer

From http://dev.mysql.com/doc/refman/5.7/en/select.html

SELECT
[ALL | DISTINCT | DISTINCTROW ]
  [HIGH_PRIORITY]
  [MAX_STATEMENT_TIME = N]
  [STRAIGHT_JOIN]
  [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
  [SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
  [PARTITION partition_list]
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
  [ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
  [ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[PROCEDURE procedure_name(argument_list)]
[INTO OUTFILE 'file_name'
    [CHARACTER SET charset_name]
    export_options
  | INTO DUMPFILE 'file_name'
  | INTO var_name [, var_name]]
[FOR UPDATE | LOCK IN SHARE MODE]]

Obviously this is mysql-specific, but it gives a pretty good flavour.