Select (SQL): Difference between revisions

Content deleted Content added
Сале (talk | contribs)
Non-standard syntax: Can't add reference to: https://mariadb.com/kb/en/select-offset-fetch/
Fjerdingen (talk | contribs)
m Some minor adjustments, mostly towards ISO SQL terminology.
Line 1:
{{Short description|SQL statement that returns a result set of recordsrows from one or more tables}}
The [[SQL]] '''SELECT''' statement returns a [[result set]] of recordsrows, from one or more [[Table (database)|tables]].<ref>{{cite web |url=http://msdn2.microsoft.com/en-us/library/ms189499.aspx |title=Transact-SQL Syntax Conventions |author=Microsoft |date=23 May 2023 }}</ref><ref>{{cite web |url=http://dev.mysql.com/doc/refman/5.0/en/select.html|title=SQL SELECT Syntax |author=MySQL}}</ref>
 
A SELECT statement retrieves zero or more rows from one or more [[Database Tables|database tables]] or database [[View (database)|views]]. In most applications, <code>SELECT</code> is the most commonly used [[data manipulation language]] (DML) command. As SQL is a [[declarative programming]] language, <code>SELECT</code> queries specify a result set, but do not specify how to calculate it. The database translates the query into a "[[query plan]]" which may vary between executions, database versions and database software. This functionality is called the "[[query optimizer]]" as it is responsible for finding the best possible execution plan for the query, within applicable constraints.
Line 6:
The SELECT statement has many optional clauses:
 
* <code>SELECT</code> clauselist is the list of [[column (database)|columns]] or SQL expressions that mustto be returned by the query. This is approximately the [[relational algebra]] [[Projection_(relational_algebra)|projection]] operation.
* <code>[[Alias (SQL)|AS]]</code> optionally provides an alias for each column or expression in the <code>SELECT</code> clauselist. This is the relational algebra [[Rename_(relational_algebra)|rename]] operation.
* <code>[[From (SQL)|FROM]]</code> specifies from which table to get the data.<ref>Omitting FROM clause is not standard, but allowed by most major DBMSes.</ref>
* <code>[[Where (SQL)|WHERE]]</code> specifies which rows to retrieve. This is approximately the relational algebra [[Selection_(relational_algebra)|selection]] operation.
Line 20:
Queries allow the user to describe desired data, leaving the [[Database management system|database management system (DBMS)]] to carry out [[query plan|planning]], [[query optimizer|optimizing]], and performing the physical operations necessary to produce that result as it chooses.
 
A query includes a list of columns to include in the final result, normally immediately following the <code>SELECT</code> keyword. An asterisk ("<code>*</code>") can be used to specify that the query should return all columns of all the queried tables. <code>SELECT</code> is the most complex statement in SQL, with optional keywords and clauses that include:
 
* The <code>[[From (SQL)|FROM]]</code> clause, which indicates the table(s) to retrieve data from. The <code>FROM</code> clause can include optional <code>[[Join (SQL)|JOIN]]</code> subclauses to specify the rules for joining tables.
Line 114:
=== Subqueries ===
 
Queries can be nested so that the results of one query can be used in another query via a [[relational operator]] or aggregation function. A nested query is also known as a ''subquery''. While joins and other table operations provide computationally superior (i.e. faster) alternatives in many cases (all depending on implementation), the use of subqueries introduces a hierarchy in execution that can be useful or necessary. In the following example, the aggregation function <code>AVG</code> receives as input the result of a subquery:
 
<syntaxhighlight lang="sql">
Line 127:
A subquery can use values from the outer query, in which case it is known as a [[correlated subquery]].
 
Since 1999 the SQL standard allows WITH clauses, i.e. named subqueries often called [[common table expression]]s (named and designed after the IBM DB2 version 2 implementation; Oracle calls these [[subquery factoring]]). CTEs can also be [[recursive]] by referring to themselves; [[Hierarchical and recursive queries in SQL|the resulting mechanism]] allows tree or graph traversals (when represented as relations), and more generally [[fixpoint]] computations.
 
=== Derived table ===
 
A derived table is the use of referencing an SQLa subquery in a FROM clause. Essentially, the derived table is a subquery that can be selected from or joined to. Derived table functionality allows the user to reference the subquery as a table. The derived table also is referred to as an ''inline view'' or a ''select in from list''.
 
In the following example, the SQL statement involves a join from the initial Books table to the derived table "Sales". This derived table captures associated book sales information using the ISBN to join to the Books table. As a result, the derived table provides the result set with additional columns (the number of items sold and the company that sold the books):
Line 729:
</syntaxhighlight>
 
SQL Server 2008 supports the "row constructor" feature, specified in the SQL3 ("[[SQL:1999")]] standard
<syntaxhighlight lang="tsql">
select *