SELECT Command
|
Previous Top Next |
[ ALL | DISTINCT ]
|
[ TOP row_count [, offset ] ]
|
* | column_reference | select_expr [ [ AS ] column_alias ] [, ...]
|
[ INTO output_table ]
|
FROM from_item [, ...]
|
[ WHERE condition ]
|
[ GROUP BY { column_name | expression } [, ...] ]
|
[ HAVING condition ]
|
[ { UNION | INTERSECT | EXCEPT } [ ALL ] [ CORRESPONDING [ BY (column_list) ] ] SELECT ... ]
|
[ ORDER BY { unsigned_integer | column_name } [ ASC | DESC ] [, ...] ]
|
[ MEMORY ] ["database_file_name".] table_name [ [ AS ] table_alias ] [ PASSWORD database_password ]
|
select ... [ [ AS ] table_alias ]
|
from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ] [ [ AS ] table_alias ]
|
SELECT TOP 10,20 * FROM employee /* returns rows 20-29 */
|
|
SELECT FirstName, LastName FROM employee
|
|
SELECT Price * Quantity AS Total FROM orders
|
|
SELECT DISTINCT FirstName INTO names FROM employee
|
If MEMORY keyword is specified before the table_name then an in-memory table is referenced, not a disk one
|
The database file name which must be specified only if SELECT operates with the tables from multiple databases.
|
The name of an existing table.
|
A substitute name for the FROM item containing the alias. An alias is used for brevity or to eliminate ambiguity for self-joins (where the same table is scanned multiple times). When an alias is provided, it completely hides the actual name of the table or function; for example given FROM foo AS f, the remainder of the SELECT must refer to this FROM item as f not foo. If an alias is written, a column alias list can also be written to provide substitute names for one or more columns of the table.
|
The password to open encrypted external database specified by database_file_name.
|
A sub-SELECT can appear in the FROM clause. This acts as though its output were created as a temporary table for the duration of this single SELECT command. Note that the sub-SELECT must be surrounded by parentheses.
|
One of
|
· | [ INNER ] JOIN
|
· | LEFT [ OUTER ] JOIN
|
· | RIGHT [ OUTER ] JOIN
|
· | FULL [ OUTER ] JOIN
|
· | CROSS JOIN
|
|
For the INNER and OUTER join types, a join condition must be specified, namely exactly one of NATURAL, ON join_condition, or USING (join_column [, ...]). See below for the meaning. For CROSS JOIN, none of these clauses may appear.
|
|
A JOIN clause combines two FROM items. Use parentheses if necessary to determine the order of nesting. In the absence of parentheses, JOINs nest left-to-right. In any case JOIN binds more tightly than the commas separating FROM items.
|
|
CROSS JOIN and INNER JOIN produce a simple Cartesian product, the same result as you get from listing the two items at the top level of FROM, but restricted by the join condition (if any). CROSS JOIN is equivalent to INNER JOIN ON (TRUE), that is, no rows are removed by qualification. These join types are just a notational convenience, since they do nothing you couldn't do with plain FROM and WHERE.
|
|
LEFT OUTER JOIN returns all rows in the qualified Cartesian product (i.e., all combined rows that pass its join condition), plus one copy of each row in the left-hand table for which there was no right-hand row that passed the join condition. This left-hand row is extended to the full width of the joined table by inserting null values for the right-hand columns. Note that only the JOIN clause's own condition is considered while deciding which rows have matches. Outer conditions are applied afterwards.
|
|
Conversely, RIGHT OUTER JOIN returns all the joined rows, plus one row for each unmatched right-hand row (extended with nulls on the left). This is just a notational convenience, since you could convert it to a LEFT OUTER JOIN by switching the left and right inputs.
|
|
FULL OUTER JOIN returns all the joined rows, plus one row for each unmatched left-hand row (extended with nulls on the right), plus one row for each unmatched right-hand row (extended with nulls on the left).
|
|
join_condition is an expression resulting in a value of type boolean (similar to a WHERE clause) that specifies which rows in a join are considered to match.
|
|
A clause of the form USING ( a, b, ... ) is shorthand for ON left_table.a = right_table.a AND left_table.b = right_table.b .... Also, USING implies that only one of each pair of equivalent columns will be included in the join output, not both.
|
|
NATURAL is shorthand for a USING list that mentions all columns in the two tables that have the same names.
|
SELECT Event_Name, Venue FROM Events e INNER JOIN Venues v ON (e.VenueNo=v.VenueNo) ORDER BY Event_Name
|
SELECT * FROM events WHERE Event_Name LIKE '%men%'
|
SELECT FirstName, COUNT(FirstName) FROM Employee GROUP BY FirstName
|
SELECT VenueNo, Avg(Ticket_Price) FROM events GROUP BY VenueNo HAVING Event_Date < NOW
|
SELECT Event_Name FROM events WHERE VenueNo < 3
|
UNION
|
SELECT Event_Name FROM events WHERE VenueNo > 8
|
SELECT VenueNo FROM Venues
|
INTERSECT CORRESPONDING BY (VenueNo)
|
SELECT VenueNo FROM Events
|
SELECT VenueNo FROM Venues
|
EXCEPT CORRESPONDING BY (VenueNo)
|
SELECT VenueNo FROM Events
|
SELECT Event_Name, Venue, Ticket_Price FROM Events e, Venues v WHERE e.VenueNo = v.VenueNo ORDER BY Ticket_Price ASC, Venue DESC
|