The SELECT statement fetches data from a database. It has this basic syntax:
SELECT [ DISTINCT ]
[ * | expression [ [ AS ] output_name ] [, ...] ]
[ FROM from_item [ [ AS ] alias ] [, ...] ]
[{ join_type from_item ON join_expression ] [...] | CROSS JOIN from_item }]
[ WHERE condition ]
[ GROUP BY grouping_element [, ...] ]
[ HAVING condition [, ...] ]
[ ORDER BY order_expr [ ASC | DESC ] [, ...] ]
- DISTINCT eliminates duplicates from the resultset.
- expression is a valid column name, function or calculation. (*, which may take the place of expression, requests all columns from all tables that the FROM clause specifies.)
- output_name is the name (i.e. the column heading) for expression in the resultset.
- from_item is a table name or a parenthesized subquery.
- alias is an alias. Every table name or subquery in the from_item can have its own alias, which then becomes the reference to the item in the rest (with some exceptions) of the SELECT statement.
- join_type is a valid JOIN type: [ INNER ] JOIN, LEFT [ OUTER ] JOIN, RIGHT [ OUTER ] JOIN or FULL [ OUTER ] JOIN.
- join_condition is an expression that evaluates to true or false, specifying which rows in the joined tables match. Typically, this takes the form table1.field_value = table2.field_value.
- condition is an expression that evaluates to true or false.
- grouping_element is a column name or column names that provide values to aggregate.
- order_expr is a column name or set of names, or a function or expression with an orderable result, e.g. concat(last_name, ', ' first_name).
- count and start are integers.
Each expression may optionally specify an output_name, which will display as a column heading in the resultset. If no name is assigned, the name of the column in the table, or the function if the column is the result of a function call, or the string ?column? otherwise, becomes the column heading.
In its simplest form, SELECT returns the scalar result of a simple calculation or a function call. For example, this SQL statement:
SELECT can also call various functions. For example, SELECT now(); returns the current date and time. The available functions vary from one RDBMS to another.
- The FROM clause defines the set of data from which the resultset will be selected. If the FROM clause is used, at least one from_item is required.
- The JOIN clause is a subclause of the FROM clause. By joining tables together, the clause expands the set of data from which the resultset will be selected.
- The WHERE clause eliminates from the output all rows that do not satisfy the condition(s) given in condition.
- The GROUP BY clause combines the output into groups of rows that match on the values specified in grouping_element. The results of aggregate functions are computed against each such group.
- The HAVING clause eliminates groups specified in the GROUP BY clause that do not satisfy the condition(s) given in condition.
- The ORDER BY clause sorts the resultset as specified in expression, in either ascending or descending order.
SELECT queries run in roughly in the order of the SELECT statement syntax.
- Get all the rows in the FROM clause.
- Filter rows based on WHERE clause.
- Group based on GROUP BY clause. Calculate aggregate functions.
- Filter groups based on HAVING clause.
- Apply any calculations specified in the SELECT list, filter duplicates if DISTINCT is specified, and eliminate any columns not specified in the column list.
- Sort based on ORDER BY clause.
Most execution plans use this order, but there are exceptions.
I have been writing these articles on my "blogfolio" first, and then transferring them over to LinkedIn. If I haven't put the next chapter up here yet, the link to the next chapter will take you to the copy on robertrodes.com.