SQL SELECT: Difference between revisions
Jump to navigation
Jump to search
(→SELECT) |
|||
(23 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=Internal= | =Internal= | ||
* [[SQL#Queries_with_SELECT|SQL]] | * [[SQL#Queries_with_SELECT|SQL]] | ||
* [[SQL_WHERE#Overview|SQL <tt>WHERE</tt>]] | |||
=Overview= | =Overview= | ||
A query consists in at least one (<code>[[#SELECT|SELECT]]</code>) and at most six categories of [[#Clauses|clauses]]: | |||
<font> | |||
[[#SELECT|SELECT]] <font color=teal>[one or more things]</font> | |||
[[#FROM|FROM]] <font color=teal>[one or more places]</font> | |||
[[#WHERE|WHERE]] <font color=teal>[one or more conditions apply]</font> | |||
[[SQL_GROUP_BY#Overview|GROUP BY]] [...] [[SQL_GROUP_BY#Overview|HAVING]] [...] | |||
[[#ORDER_BY|ORDER BY]] [...]; | |||
</font> | |||
Almost every query will include at least three of these clauses (<code>[[#SELECT|SELECT]]</code>, <code>[[#FROM|FROM]]</code> and <code>[[#WHERE|WHERE]]</code>). | |||
Upon execution, an SQL query returns a [[SQL#Result_Set|result set]]. | |||
Select all (possibly across multiple tables) then filter and discard with where. | Select all (possibly across multiple tables) then filter and discard with where. | ||
=Example= | |||
<syntaxhighlight lang='sql'> | |||
SELECT id, name FROM person WHERE id = 1; | |||
</syntaxhighlight> | |||
The following query: | |||
<syntaxhighlight lang='sql'> | |||
SELECT; | |||
</syntaxhighlight> | |||
is valid, it returns one empty row. | |||
=Clauses= | =Clauses= | ||
==<tt>SELECT</tt>== | |||
<font> | |||
SELECT <font color=teal>[one or more things]</font> ... | |||
</font> | |||
<code>SELECT</code> specifies which columns from the <code>[[#FROM|FROM]]</code> sources need to be retrieved. | |||
==<tt>FROM</tt>== | ==<tt>FROM</tt>== | ||
<font> | <font> | ||
FROM | FROM <font color=teal>[one or more places]</font> | ||
</font> | </font> | ||
The "places" we select from can be: | |||
* permanent | |||
* derived | |||
* temporary | |||
* virtual(view) | |||
Table alias. | Table alias. | ||
Line 24: | Line 57: | ||
==<tt>WHERE</tt>== | ==<tt>WHERE</tt>== | ||
{{Internal|SQL_WHERE#Overview|The <tt>WHERE</tt> Clause}} | |||
==<tt>GROUP BY ... HAVING</tt>== | ==<tt>GROUP BY ... HAVING</tt>== | ||
{{Internal|SQL_GROUP_BY#Overview|<tt>GROUP BY ... HAVING</tt>}} | |||
==<tt>ORDER BY</tt>== | |||
=Subqueries= | =Subqueries= |
Revision as of 23:23, 22 May 2024
Internal
Overview
A query consists in at least one (SELECT
) and at most six categories of clauses:
SELECT [one or more things] FROM [one or more places] WHERE [one or more conditions apply] GROUP BY [...] HAVING [...] ORDER BY [...];
Almost every query will include at least three of these clauses (SELECT
, FROM
and WHERE
).
Upon execution, an SQL query returns a result set.
Select all (possibly across multiple tables) then filter and discard with where.
Example
SELECT id, name FROM person WHERE id = 1;
The following query:
SELECT;
is valid, it returns one empty row.
Clauses
SELECT
SELECT [one or more things] ...
SELECT
specifies which columns from the FROM
sources need to be retrieved.
FROM
FROM [one or more places]
The "places" we select from can be:
- permanent
- derived
- temporary
- virtual(view)
Table alias.