2
0

lang_select.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>SQLite Query Language: SELECT</title>
  4. <style type="text/css">
  5. body {
  6. margin: auto;
  7. font-family: Verdana, sans-serif;
  8. padding: 8px 1%;
  9. }
  10. a { color: #45735f }
  11. a:visited { color: #734559 }
  12. .logo { position:absolute; margin:3px; }
  13. .tagline {
  14. float:right;
  15. text-align:right;
  16. font-style:italic;
  17. width:240px;
  18. margin:12px;
  19. margin-top:58px;
  20. }
  21. .toolbar {
  22. font-variant: small-caps;
  23. text-align: center;
  24. line-height: 1.6em;
  25. margin: 0;
  26. padding:1px 8px;
  27. }
  28. .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
  29. .toolbar a:visited { color: white; }
  30. .toolbar a:hover { color: #80a796; background: white; }
  31. .content { margin: 5%; }
  32. .content dt { font-weight:bold; }
  33. .content dd { margin-bottom: 25px; margin-left:20%; }
  34. .content ul { padding:0px; padding-left: 15px; margin:0px; }
  35. /* rounded corners */
  36. .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
  37. .sw { background: url(images/sw.png) 0% 100% no-repeat }
  38. .ne { background: url(images/ne.png) 100% 0% no-repeat }
  39. .nw { background: url(images/nw.png) 0% 0% no-repeat }
  40. </style>
  41. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  42. </head>
  43. <body>
  44. <div><!-- container div to satisfy validator -->
  45. <a href="lang.html">
  46. <h2 align="center">SQL As Understood By SQLite</h2></a><h1>SELECT</h1><h4><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></h4><blockquote> <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif"></img> </blockquote>
  47. <h4><a href="syntaxdiagrams.html#select-core">select-core:</a></h4><blockquote> <img alt="syntax diagram select-core" src="images/syntax/select-core.gif"></img> </blockquote>
  48. <h4><a href="syntaxdiagrams.html#result-column">result-column:</a></h4><blockquote> <img alt="syntax diagram result-column" src="images/syntax/result-column.gif"></img> </blockquote>
  49. <h4><a href="syntaxdiagrams.html#join-source">join-source:</a></h4><blockquote> <img alt="syntax diagram join-source" src="images/syntax/join-source.gif"></img> </blockquote>
  50. <h4><a href="syntaxdiagrams.html#single-source">single-source:</a></h4><blockquote> <img alt="syntax diagram single-source" src="images/syntax/single-source.gif"></img> </blockquote>
  51. <h4><a href="syntaxdiagrams.html#join-op">join-op:</a></h4><blockquote> <img alt="syntax diagram join-op" src="images/syntax/join-op.gif"></img> </blockquote>
  52. <h4><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></h4><blockquote> <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif"></img> </blockquote>
  53. <h4><a href="syntaxdiagrams.html#ordering-term">ordering-term:</a></h4><blockquote> <img alt="syntax diagram ordering-term" src="images/syntax/ordering-term.gif"></img> </blockquote>
  54. <h4><a href="syntaxdiagrams.html#compound-operator">compound-operator:</a></h4><blockquote> <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif"></img> </blockquote>
  55. <p>The SELECT statement is used to query the database. The
  56. result of a SELECT is zero or more rows of data where each row
  57. has a fixed number of columns. The number of columns in the
  58. result is specified by the expression list in between the
  59. SELECT and FROM keywords. Any arbitrary expression can be used
  60. as a result. If a result expression is <font color="#2c2cf0"><big>*</big></font> then all columns of all tables are substituted
  61. for that one expression. If the expression is the name of
  62. a table followed by <font color="#2c2cf0"><big>.*</big></font> then the result is all columns
  63. in that one table.</p>
  64. <p>The DISTINCT keyword causes a subset of result rows to be returned,
  65. in which each result row is different. NULL values are not treated as
  66. distinct from each other. The default behavior is that all result rows
  67. be returned, which can be made explicit with the keyword ALL.</p>
  68. <p>The query is executed against one or more tables specified after
  69. the FROM keyword. If multiple tables names are separated by commas,
  70. then the query is against the cross join of the various tables.
  71. The full SQL-92 join syntax can also be used to specify joins.
  72. A sub-query
  73. in parentheses may be substituted for any table name in the FROM clause.
  74. The entire FROM clause may be omitted, in which case the result is a
  75. single row consisting of the values of the expression list.
  76. </p>
  77. <p>The WHERE clause can be used to limit the number of rows over
  78. which the query operates.</p>
  79. <p>The GROUP BY clause causes one or more rows of the result to
  80. be combined into a single row of output. This is especially useful
  81. when the result contains aggregate functions. The expressions in
  82. the GROUP BY clause do <em>not</em> have to be expressions that
  83. appear in the result. The HAVING clause is similar to WHERE except
  84. that HAVING applies after grouping has occurred. The HAVING expression
  85. may refer to values, even aggregate functions, that are not in the result.</p>
  86. <p>The ORDER BY clause causes the output rows to be sorted.
  87. The argument to ORDER BY is a list of expressions that are used as the
  88. key for the sort. The expressions do not have to be part of the
  89. result for a simple SELECT, but in a compound SELECT each sort
  90. expression must exactly match one of the result columns. Each
  91. sort expression may be optionally followed by a COLLATE keyword and
  92. the name of a collating function used for ordering text and/or
  93. keywords ASC or DESC to specify the sort order.</p>
  94. <p>Each term of an ORDER BY expression is processed as follows:</p>
  95. <ol>
  96. <li><p>If the ORDER BY expression is a constant integer K then the
  97. output is ordered by the K-th column of the result set.</p></li>
  98. <li><p>If the ORDER BY expression is an identifier and one of the
  99. output columns has an alias by the same name, then the output is
  100. ordered by the identified column.</p></li>
  101. <li><p>Otherwise, the ORDER BY expression is evaluated and the output
  102. is ordered by the value of that expression.</p></li>
  103. </ol>
  104. <p>In a compound SELECT statement, the third ORDER BY matching rule
  105. requires that the expression be identical to one of the columns in
  106. the result set. The three rules are first applied to the left-most
  107. SELECT in the compound. If a match is found, the search stops. Otherwise,
  108. the next SELECT to the right is tried. This continues until a match
  109. is found. Each term of the ORDER BY clause is processed separately
  110. and may come from different SELECT statements in the compound.</p>
  111. <p>The LIMIT clause places an upper bound on the number of rows
  112. returned in the result. A negative LIMIT indicates no upper bound.
  113. The optional OFFSET following LIMIT specifies how many
  114. rows to skip at the beginning of the result set.
  115. In a compound query, the LIMIT clause may only appear on the
  116. final SELECT statement.
  117. The limit is applied to the entire query not
  118. to the individual SELECT statement to which it is attached.
  119. Note that if the OFFSET keyword is used in the LIMIT clause, then the
  120. limit is the first number and the offset is the second number. If a
  121. comma is used instead of the OFFSET keyword, then the offset is the
  122. first number and the limit is the second number. This seeming
  123. contradition is intentional - it maximizes compatibility with legacy
  124. SQL database systems.
  125. </p>
  126. <p>A compound SELECT is formed from two or more simple SELECTs connected
  127. by one of the operators UNION, UNION ALL, INTERSECT, or EXCEPT. In
  128. a compound SELECT, all the constituent SELECTs must specify the
  129. same number of result columns. There may be only a single ORDER BY
  130. clause at the end of the compound SELECT. The UNION and UNION ALL
  131. operators combine the results of the SELECTs to the right and left into
  132. a single big table. The difference is that in UNION all result rows
  133. are distinct where in UNION ALL there may be duplicates.
  134. The INTERSECT operator takes the intersection of the results of the
  135. left and right SELECTs. EXCEPT takes the result of left SELECT after
  136. removing the results of the right SELECT. When three or more SELECTs
  137. are connected into a compound, they group from left to right.</p>