<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>SQLite Query Language: SELECT</title>
<style type="text/css">
body {
    margin: auto;
    font-family: Verdana, sans-serif;
    padding: 8px 1%;
}

a { color: #044a64 }
a:visited { color: #734559 }

.logo { position:absolute; margin:3px; }
.tagline {
  float:right;
  text-align:right;
  font-style:italic;
  width:300px;
  margin:12px;
  margin-top:58px;
}

.menubar {
  clear: both;
  border-radius: 8px;
  background: #044a64;
  padding: 0px;
  margin: 0px;
  cell-spacing: 0px;
}    
.toolbar {
  text-align: center;
  line-height: 1.6em;
  margin: 0;
  padding: 0px 8px;
}
.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
.toolbar a:visited { color: white; }
.toolbar a:hover { color: #044a64; background: white; }

.content    { margin: 5%; }
.content dt { font-weight:bold; }
.content dd { margin-bottom: 25px; margin-left:20%; }
.content ul { padding:0px; padding-left: 15px; margin:0px; }

/* Things for "fancyformat" documents start here. */
.fancy img+p {font-style:italic}
.fancy .codeblock i { color: darkblue; }
.fancy h1,.fancy h2,.fancy h3,.fancy h4 {font-weight:normal;color:#044a64}
.fancy h2 { margin-left: 10px }
.fancy h3 { margin-left: 20px }
.fancy h4 { margin-left: 30px }
.fancy th {white-space:nowrap;text-align:left;border-bottom:solid 1px #444}
.fancy th, .fancy td {padding: 0.2em 1ex; vertical-align:top}
.fancy #toc a        { color: darkblue ; text-decoration: none }
.fancy .todo         { color: #AA3333 ; font-style : italic }
.fancy .todo:before  { content: 'TODO:' }
.fancy p.todo        { border: solid #AA3333 1px; padding: 1ex }
.fancy img { display:block; }
.fancy :link:hover, .fancy :visited:hover { background: wheat }
.fancy p,.fancy ul,.fancy ol { margin: 1em 5ex }
.fancy li p { margin: 1em 0 }
/* End of "fancyformat" specific rules. */

</style>
  
</head>
<body>
<div><!-- container div to satisfy validator -->

<a href="index.html">
<img class="logo" src="images/sqlite370_banner.gif" alt="SQLite Logo"
 border="0"></a>
<div><!-- IE hack to prevent disappearing logo--></div>
<div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div>

<table width=100% class="menubar"><tr>
  <td width=100%>
  <div class="toolbar">
    <a href="about.html">About</a>
    <a href="sitemap.html">Sitemap</a>
    <a href="docs.html">Documentation</a>
    <a href="download.html">Download</a>
    <a href="copyright.html">License</a>
    <a href="news.html">News</a>
    <a href="support.html">Support</a>
  </div>
<script>
  gMsg = "Search SQLite Docs..."
  function entersearch() {
    var q = document.getElementById("q");
    if( q.value == gMsg ) { q.value = "" }
    q.style.color = "black"
    q.style.fontStyle = "normal"
  }
  function leavesearch() {
    var q = document.getElementById("q");
    if( q.value == "" ) { 
      q.value = gMsg
      q.style.color = "#044a64"
      q.style.fontStyle = "italic"
    }
  }
  function hideorshow(btn,obj){
    var x = document.getElementById(obj);
    var b = document.getElementById(btn);
    if( x.style.display!='none' ){
      x.style.display = 'none';
      b.innerHTML='show';
    }else{
      x.style.display = '';
      b.innerHTML='hide';
    }
    return false;
  }
</script>
<td>
    <div style="padding:0 1em 0px 0;white-space:nowrap">
    <form name=f method="GET" action="http://www.sqlite.org/search">
      <input id=q name=q type=text
       onfocus="entersearch()" onblur="leavesearch()" style="width:24ex;padding:1px 1ex; border:solid white 1px; font-size:0.9em ; font-style:italic;color:#044a64;" value="Search SQLite Docs...">
      <input type=submit value="Go" style="border:solid white 1px;background-color:#044a64;color:white;font-size:0.9em;padding:0 1ex">
    </form>
    </div>
  </table>

<div class=startsearch></div>
  
<h1 align="center">SQL As Understood By SQLite</h1><p><a href="lang.html">[Top]</a></p><h2>SELECT</h2><p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1157' onclick='hideorshow("x1157","x1158")'>hide</button></p>
 <blockquote id='x1158'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#common-table-expression">common-table-expression:</a></b>
<button id='x1159' onclick='hideorshow("x1159","x1160")'>show</button></p>
 <blockquote id='x1160' style='display:none;'>
 <img alt="syntax diagram common-table-expression" src="images/syntax/common-table-expression.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#compound-operator">compound-operator:</a></b>
<button id='x1161' onclick='hideorshow("x1161","x1162")'>show</button></p>
 <blockquote id='x1162' style='display:none;'>
 <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#expr">expr:</a></b>
<button id='x1163' onclick='hideorshow("x1163","x1164")'>show</button></p>
 <blockquote id='x1164' style='display:none;'>
 <img alt="syntax diagram expr" src="images/syntax/expr.gif" />
<p><b><a href="syntaxdiagrams.html#literal-value">literal-value:</a></b>
<button id='x1165' onclick='hideorshow("x1165","x1166")'>show</button></p>
 <blockquote id='x1166' style='display:none;'>
 <img alt="syntax diagram literal-value" src="images/syntax/literal-value.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#raise-function">raise-function:</a></b>
<button id='x1167' onclick='hideorshow("x1167","x1168")'>show</button></p>
 <blockquote id='x1168' style='display:none;'>
 <img alt="syntax diagram raise-function" src="images/syntax/raise-function.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#type-name">type-name:</a></b>
<button id='x1169' onclick='hideorshow("x1169","x1170")'>show</button></p>
 <blockquote id='x1170' style='display:none;'>
 <img alt="syntax diagram type-name" src="images/syntax/type-name.gif" />
<p><b><a href="syntaxdiagrams.html#signed-number">signed-number:</a></b>
<button id='x1171' onclick='hideorshow("x1171","x1172")'>show</button></p>
 <blockquote id='x1172' style='display:none;'>
 <img alt="syntax diagram signed-number" src="images/syntax/signed-number.gif" />
</blockquote>
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1173' onclick='hideorshow("x1173","x1174")'>show</button></p>
 <blockquote id='x1174' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1175' onclick='hideorshow("x1175","x1176")'>show</button></p>
 <blockquote id='x1176' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1177' onclick='hideorshow("x1177","x1178")'>show</button></p>
 <blockquote id='x1178' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#ordering-term">ordering-term:</a></b>
<button id='x1179' onclick='hideorshow("x1179","x1180")'>show</button></p>
 <blockquote id='x1180' style='display:none;'>
 <img alt="syntax diagram ordering-term" src="images/syntax/ordering-term.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1181' onclick='hideorshow("x1181","x1182")'>show</button></p>
 <blockquote id='x1182' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1183' onclick='hideorshow("x1183","x1184")'>show</button></p>
 <blockquote id='x1184' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
</blockquote>
</blockquote>



<p>The SELECT statement is used to query the database.  The
result of a SELECT is zero or more rows of data where each row
has a fixed number of columns.  A SELECT statement does not make
any changes to the database.

<p>The "<a href="syntaxdiagrams.html#select-stmt">select-stmt</a>" syntax diagram above attempts to show as much of the
SELECT statement syntax as possible in a single diagram, because some readers
find that helpful.  The following "<a href="syntaxdiagrams.html#factored-select-stmt">factored-select-stmt</a>" is an alternative
syntax diagrams that expresses the same syntax but tries to break the syntax 
down into smaller chunks.

<p><b><a href="syntaxdiagrams.html#factored-select-stmt">factored-select-stmt:</a></b>
<button id='x1185' onclick='hideorshow("x1185","x1186")'>show</button></p>
 <blockquote id='x1186' style='display:none;'>
 <img alt="syntax diagram factored-select-stmt" src="images/syntax/factored-select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#common-table-expression">common-table-expression:</a></b>
<button id='x1187' onclick='hideorshow("x1187","x1188")'>show</button></p>
 <blockquote id='x1188' style='display:none;'>
 <img alt="syntax diagram common-table-expression" src="images/syntax/common-table-expression.gif" />
<p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1189' onclick='hideorshow("x1189","x1190")'>show</button></p>
 <blockquote id='x1190' style='display:none;'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1191' onclick='hideorshow("x1191","x1192")'>show</button></p>
 <blockquote id='x1192' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1193' onclick='hideorshow("x1193","x1194")'>show</button></p>
 <blockquote id='x1194' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1195' onclick='hideorshow("x1195","x1196")'>show</button></p>
 <blockquote id='x1196' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1197' onclick='hideorshow("x1197","x1198")'>show</button></p>
 <blockquote id='x1198' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1199' onclick='hideorshow("x1199","x1200")'>show</button></p>
 <blockquote id='x1200' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
</blockquote>
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#compound-operator">compound-operator:</a></b>
<button id='x1201' onclick='hideorshow("x1201","x1202")'>show</button></p>
 <blockquote id='x1202' style='display:none;'>
 <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#expr">expr:</a></b>
<button id='x1203' onclick='hideorshow("x1203","x1204")'>show</button></p>
 <blockquote id='x1204' style='display:none;'>
 <img alt="syntax diagram expr" src="images/syntax/expr.gif" />
<p><b><a href="syntaxdiagrams.html#literal-value">literal-value:</a></b>
<button id='x1205' onclick='hideorshow("x1205","x1206")'>show</button></p>
 <blockquote id='x1206' style='display:none;'>
 <img alt="syntax diagram literal-value" src="images/syntax/literal-value.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#raise-function">raise-function:</a></b>
<button id='x1207' onclick='hideorshow("x1207","x1208")'>show</button></p>
 <blockquote id='x1208' style='display:none;'>
 <img alt="syntax diagram raise-function" src="images/syntax/raise-function.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1209' onclick='hideorshow("x1209","x1210")'>show</button></p>
 <blockquote id='x1210' style='display:none;'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1211' onclick='hideorshow("x1211","x1212")'>show</button></p>
 <blockquote id='x1212' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1213' onclick='hideorshow("x1213","x1214")'>show</button></p>
 <blockquote id='x1214' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1215' onclick='hideorshow("x1215","x1216")'>show</button></p>
 <blockquote id='x1216' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1217' onclick='hideorshow("x1217","x1218")'>show</button></p>
 <blockquote id='x1218' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1219' onclick='hideorshow("x1219","x1220")'>show</button></p>
 <blockquote id='x1220' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#type-name">type-name:</a></b>
<button id='x1221' onclick='hideorshow("x1221","x1222")'>show</button></p>
 <blockquote id='x1222' style='display:none;'>
 <img alt="syntax diagram type-name" src="images/syntax/type-name.gif" />
<p><b><a href="syntaxdiagrams.html#signed-number">signed-number:</a></b>
<button id='x1223' onclick='hideorshow("x1223","x1224")'>show</button></p>
 <blockquote id='x1224' style='display:none;'>
 <img alt="syntax diagram signed-number" src="images/syntax/signed-number.gif" />
</blockquote>
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#ordering-term">ordering-term:</a></b>
<button id='x1225' onclick='hideorshow("x1225","x1226")'>show</button></p>
 <blockquote id='x1226' style='display:none;'>
 <img alt="syntax diagram ordering-term" src="images/syntax/ordering-term.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#select-core">select-core:</a></b>
<button id='x1227' onclick='hideorshow("x1227","x1228")'>show</button></p>
 <blockquote id='x1228' style='display:none;'>
 <img alt="syntax diagram select-core" src="images/syntax/select-core.gif" />
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1229' onclick='hideorshow("x1229","x1230")'>show</button></p>
 <blockquote id='x1230' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1231' onclick='hideorshow("x1231","x1232")'>show</button></p>
 <blockquote id='x1232' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1233' onclick='hideorshow("x1233","x1234")'>show</button></p>
 <blockquote id='x1234' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1235' onclick='hideorshow("x1235","x1236")'>show</button></p>
 <blockquote id='x1236' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1237' onclick='hideorshow("x1237","x1238")'>show</button></p>
 <blockquote id='x1238' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
<p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1239' onclick='hideorshow("x1239","x1240")'>show</button></p>
 <blockquote id='x1240' style='display:none;'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
</blockquote>
</blockquote>
</blockquote>
</blockquote>


<p>Note that there are paths through the syntax diagrams that
are not allowed in practice.  Some examples:
<ul>
<li>A <a href="lang_select.html#values">VALUES</a> clause can be the first element in a <a href="lang_select.html#compound">compound SELECT</a>
    that uses a <a href="lang_with.html">WITH</a> clause, but a <a href="lang_select.html#simpleselect">simple SELECT</a> that consists of
    just a <a href="lang_select.html#values">VALUES</a> clause cannot be preceded by a <a href="lang_with.html">WITH</a> clause.
<li>The <a href="lang_with.html">WITH</a> clause must occur on the first SELECT of a <a href="lang_select.html#compound">compound SELECT</a>.
    It cannot follow a <a href="syntaxdiagrams.html#compound-operator">compound-operator</a>.
</ul>
These and other similar syntax restrictions are described in the text.

<p>The SELECT statement is the most complicated command in the SQL language.
To make the description easier to follow, some of the passages below describe
the way the data returned by a SELECT statement is determined as a series of
steps. It is important to keep in mind that this is purely illustrative -
in practice neither SQLite nor any other SQL engine is required to follow 
this or any other specific process.

<a name="simpleselect"></a>

<h3>Simple Select Processing</h3>

<p>The core of a SELECT statement is a "simple SELECT" shown by the
<a href="syntaxdiagrams.html#select-core">select-core</a> and <a href="syntaxdiagrams.html#simple-select-stmt">simple-select-stmt</a> syntax diagrams below.  
In practice, most SELECT statements are simple SELECT statements.

<p><b><a href="syntaxdiagrams.html#simple-select-stmt">simple-select-stmt:</a></b>
<button id='x1241' onclick='hideorshow("x1241","x1242")'>hide</button></p>
 <blockquote id='x1242'>
 <img alt="syntax diagram simple-select-stmt" src="images/syntax/simple-select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#common-table-expression">common-table-expression:</a></b>
<button id='x1243' onclick='hideorshow("x1243","x1244")'>show</button></p>
 <blockquote id='x1244' style='display:none;'>
 <img alt="syntax diagram common-table-expression" src="images/syntax/common-table-expression.gif" />
<p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1245' onclick='hideorshow("x1245","x1246")'>show</button></p>
 <blockquote id='x1246' style='display:none;'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#compound-operator">compound-operator:</a></b>
<button id='x1247' onclick='hideorshow("x1247","x1248")'>show</button></p>
 <blockquote id='x1248' style='display:none;'>
 <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1249' onclick='hideorshow("x1249","x1250")'>show</button></p>
 <blockquote id='x1250' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1251' onclick='hideorshow("x1251","x1252")'>show</button></p>
 <blockquote id='x1252' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1253' onclick='hideorshow("x1253","x1254")'>show</button></p>
 <blockquote id='x1254' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1255' onclick='hideorshow("x1255","x1256")'>show</button></p>
 <blockquote id='x1256' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1257' onclick='hideorshow("x1257","x1258")'>show</button></p>
 <blockquote id='x1258' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
</blockquote>
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#expr">expr:</a></b>
<button id='x1259' onclick='hideorshow("x1259","x1260")'>show</button></p>
 <blockquote id='x1260' style='display:none;'>
 <img alt="syntax diagram expr" src="images/syntax/expr.gif" />
<p><b><a href="syntaxdiagrams.html#literal-value">literal-value:</a></b>
<button id='x1261' onclick='hideorshow("x1261","x1262")'>show</button></p>
 <blockquote id='x1262' style='display:none;'>
 <img alt="syntax diagram literal-value" src="images/syntax/literal-value.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#raise-function">raise-function:</a></b>
<button id='x1263' onclick='hideorshow("x1263","x1264")'>show</button></p>
 <blockquote id='x1264' style='display:none;'>
 <img alt="syntax diagram raise-function" src="images/syntax/raise-function.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1265' onclick='hideorshow("x1265","x1266")'>show</button></p>
 <blockquote id='x1266' style='display:none;'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#compound-operator">compound-operator:</a></b>
<button id='x1267' onclick='hideorshow("x1267","x1268")'>show</button></p>
 <blockquote id='x1268' style='display:none;'>
 <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1269' onclick='hideorshow("x1269","x1270")'>show</button></p>
 <blockquote id='x1270' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1271' onclick='hideorshow("x1271","x1272")'>show</button></p>
 <blockquote id='x1272' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1273' onclick='hideorshow("x1273","x1274")'>show</button></p>
 <blockquote id='x1274' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1275' onclick='hideorshow("x1275","x1276")'>show</button></p>
 <blockquote id='x1276' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1277' onclick='hideorshow("x1277","x1278")'>show</button></p>
 <blockquote id='x1278' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#type-name">type-name:</a></b>
<button id='x1279' onclick='hideorshow("x1279","x1280")'>show</button></p>
 <blockquote id='x1280' style='display:none;'>
 <img alt="syntax diagram type-name" src="images/syntax/type-name.gif" />
<p><b><a href="syntaxdiagrams.html#signed-number">signed-number:</a></b>
<button id='x1281' onclick='hideorshow("x1281","x1282")'>show</button></p>
 <blockquote id='x1282' style='display:none;'>
 <img alt="syntax diagram signed-number" src="images/syntax/signed-number.gif" />
</blockquote>
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#ordering-term">ordering-term:</a></b>
<button id='x1283' onclick='hideorshow("x1283","x1284")'>show</button></p>
 <blockquote id='x1284' style='display:none;'>
 <img alt="syntax diagram ordering-term" src="images/syntax/ordering-term.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#select-core">select-core:</a></b>
<button id='x1285' onclick='hideorshow("x1285","x1286")'>hide</button></p>
 <blockquote id='x1286'>
 <img alt="syntax diagram select-core" src="images/syntax/select-core.gif" />
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1287' onclick='hideorshow("x1287","x1288")'>show</button></p>
 <blockquote id='x1288' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1289' onclick='hideorshow("x1289","x1290")'>show</button></p>
 <blockquote id='x1290' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1291' onclick='hideorshow("x1291","x1292")'>show</button></p>
 <blockquote id='x1292' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1293' onclick='hideorshow("x1293","x1294")'>show</button></p>
 <blockquote id='x1294' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1295' onclick='hideorshow("x1295","x1296")'>show</button></p>
 <blockquote id='x1296' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
<p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1297' onclick='hideorshow("x1297","x1298")'>show</button></p>
 <blockquote id='x1298' style='display:none;'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#compound-operator">compound-operator:</a></b>
<button id='x1299' onclick='hideorshow("x1299","x1300")'>show</button></p>
 <blockquote id='x1300' style='display:none;'>
 <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif" />
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>


<p>Generating the results of a simple SELECT
statement is presented as a four step process in the description below:

<ol>
  <li> <p><a href="lang_select.html#fromclause">FROM clause</a> processing: The input data for the simple SELECT is
       determined. The input data is either implicitly a single row with 0
       columns (if there is no FROM clause) or is determined by the FROM
       clause.
  <li> <p><a href="lang_select.html#whereclause">WHERE clause</a> processing: The input data is filtered using the WHERE
       clause expression.  
  <li> <p><a href="lang_select.html#resultset">GROUP BY, HAVING and result-column expression</a> processing: 
       The set of result rows is computed by aggregating the data according to
       any GROUP BY clause and calculating the result-set expressions for the
       rows of the filtered input dataset.  
  <li> <p><a href="lang_select.html#distinct">DISTINCT/ALL keyword</a> processing: If the query is a "SELECT
       DISTINCT" query, duplicate rows are removed from the set of result rows.
</ol>

<p>There are two types of simple SELECT statement - aggregate and 
non-aggregate queries. A simple SELECT statement is an aggregate query if
it contains either a GROUP BY clause or one or more aggregate functions
in the result-set. Otherwise, if a simple SELECT contains no aggregate
functions or a GROUP BY clause, it is a non-aggregate query.

<p><b>1. Determination of input data (FROM clause processing).</b>
<a name="fromclause"></a>



<p>The input data used by a simple SELECT query is a set of <i>N</i> rows 
each <i>M</i> columns wide.

<p>If the FROM clause is omitted from a simple SELECT statement, then the 
input data is implicitly a single row zero columns wide (i.e. <i>N</i>=1 and
<i>M</i>=0).

<p>If a FROM clause is specified, the data on which a simple SELECT query
operates comes from the one or more tables or subqueries (SELECT statements
in parenthesis) specified following the FROM keyword. A subquery specified
in the table-or-subquery following the FROM clause in a 
simple SELECT statement is
handled as if it was a table containing the data returned by executing the
subquery statement. Each column of the subquery has the
<a href="datatype3.html#collation">collation sequence</a> and <a href="datatype3.html#affinity">affinity</a> of the corresponding expression
in the subquery statement.

<p>If there is only a single table or subquery in the FROM
clause, then the input data used by the SELECT statement is the contents of the
named table. If there is more than one table or subquery in FROM clause
then the contents of all tables and/or subqueries
are joined into a single dataset for the simple SELECT statement to operate on.
Exactly how the data is combined depends on the specific <a href="syntaxdiagrams.html#join-operator">join-operator</a> and
<a href="syntaxdiagrams.html#join-constraint">join-constraint</a> used to connect the tables or subqueries together.

<p>All joins in SQLite are based on the cartesian product of the left and
right-hand datasets. The columns of the cartesian product dataset are, in 
order, all the columns of the left-hand dataset followed by all the columns
of the right-hand dataset. There is a row in the cartesian product dataset
formed by combining each unique combination of a row from the left-hand 
and right-hand datasets. In other words, if the left-hand dataset consists of
<i>N<sub><small>left</small></sub></i> rows of 
<i>M<sub><small>left</small></sub></i> columns, and the right-hand dataset of
<i>N<sub><small>right</small></sub></i> rows of
<i>M<sub><small>right</small></sub></i> columns, then the cartesian product is a
dataset of 
<i>N<sub><small>left</small></sub>&times;N<sub><small>right</small></sub></i>
rows, each containing 
<i>M<sub><small>left</small></sub>+M<sub><small>right</small></sub></i> columns.

<p>If the join-operator is "CROSS JOIN", "INNER JOIN", "JOIN" or a comma
(",") and there is no ON or USING clause, then the result of the join is
simply the cartesian product of the left and right-hand datasets. 
If join-operator does have ON or USING clauses, those are handled according to
the following bullet points:

<ul>
  <li> <p>If there is an ON clause then the ON expression is
       evaluated for each row of the cartesian product as a 
       <a href="lang_expr.html#booleanexpr">boolean expression</a>. Only rows for which the expression evaluates to 
       true are included from the dataset.

  <li> <p>If there is a USING clause
       then each of the column names specified must exist in the datasets to 
       both the left and right of the join-operator. For each pair of named
       columns, the expression "lhs.X = rhs.X" is evaluated for each row of
       the cartesian product as a <a href="lang_expr.html#booleanexpr">boolean expression</a>. Only rows for which
       all such expressions evaluates to true are included from the
       result set. When comparing values as a result of a USING clause, the
       normal rules for handling affinities, collation sequences and NULL
       values in comparisons apply. The column from the dataset on the
       left-hand side of the join-operator is considered to be on the left-hand
       side of the comparison operator (=) for the purposes of collation 
       sequence and affinity precedence.

       <p>For each pair of columns identified by a USING clause, the column
       from the right-hand dataset is omitted from the joined dataset. This 
       is the only difference between a USING clause and its equivalent ON
       constraint.

  <li> <p>If the NATURAL keyword is in the join-operator then an
       implicit USING clause is added to the join-constraints. The implicit
       USING clause contains each of the column names that appear in both
       the left and right-hand input datasets. If the left and right-hand
       input datasets feature no common column names, then the NATURAL keyword
       has no effect on the results of the join. A USING or ON clause may
       not be added to a join that specifies the NATURAL keyword.

  <li> <p>If the join-operator is a "LEFT JOIN" or "LEFT OUTER JOIN", then
       after
       the ON or USING filtering clauses have been applied, an extra row is 
       added to the output for each row in the original left-hand input 
       dataset that corresponds to no rows at all in the composite
       dataset (if any). The added rows contain NULL values in the columns
       that would normally contain values copied from the right-hand input
       dataset.  
</ul>

<p>When more than two tables are joined together as part of a FROM clause,
the join operations are processed in order from left to right. In other 
words, the FROM clause (A join-op-1 B join-op-2 C) is computed as 
((A join-op-1 B) join-op-2 C).

<a name="crossjoin"></a>

<p><b>Side note: Special handling of CROSS JOIN.</b>
There is no difference between the "INNER JOIN", "JOIN" and "," join
operators. They are completely interchangeable in SQLite.
The "CROSS JOIN" join operator produces the same result as the 
"INNER JOIN", "JOIN" and "," operators, but is 
<a href="optoverview.html#crossjoin">handled differently by the query
optimizer</a> in that it prevents the query optimizer from reordering
the tables in the join.  An application programmer can use the CROSS JOIN 
operator to directly influence the algorithm that is chosen to implement
the SELECT statement.  Avoid using CROSS JOIN except in specific situations 
where manual control of the query optimizer is desired.  Avoid using
CROSS JOIN early in the development of an application as doing so is
a <a href="http://c2.com/cgi/wiki?PrematureOptimization">premature
optimization</a>.  The special handling of CROSS JOIN is an SQLite-specific
feature and is not a part of standard SQL.
       

<a name="whereclause"></a>


<p><b>2. WHERE clause filtering.</b>

<p>If a WHERE clause is specified, the WHERE expression is evaluated for 
each row in the input data as a <a href="lang_expr.html#booleanexpr">boolean expression</a>. Only rows for which the
WHERE clause expression evaluates to true are included from the dataset before
continuing.  Rows are excluded from the result if the WHERE clause
evaluates to either false or NULL.

<p>For a JOIN or INNER JOIN or CROSS JOIN, there is no difference between 
a constraint expression in the WHERE clause and one in the ON clause.  However,
for a LEFT JOIN or LEFT OUTER JOIN, the difference is very important.  
In a LEFT JOIN,
the extra NULL row for the right-hand table is added after ON clause processing
but before WHERE clause processing.  A constraint of the form "left.x=right.y"
in an ON clause will therefore allow through the added all-NULL rows of the
right table.  But if that same constraint is in the WHERE clause a NULL in
"right.y" will prevent the expression "left.x=right.y" from being true, and
thus exclude that row from the output.

<p><b>3. Generation of the set of result rows.</b>
<a name="resultset"></a>



<p>Once the input data from the FROM clause has been filtered by the
WHERE clause expression (if any), the set of result rows for the simple 
SELECT are calculated. Exactly how this is done depends on whether the simple 
SELECT is an aggregate or non-aggregate query, and whether or not a GROUP
BY clause was specified.

<p> The list of expressions between the SELECT and FROM keywords is known as
the result expression list.  If a result expression is the special expression
"*" then all columns in the input data are substituted for that one expression.
If the expression is the alias of a table or subquery in the FROM clause
followed by ".*" then all columns from the named table or subquery are
substituted for the single expression. It is an error to use a "*" or
"alias.*" expression in any context other than a result expression list.
It is also an error to use a "*" or "alias.*" expression in a simple SELECT
query that does not have a FROM clause.

<p> The number of columns in the rows returned by a simple SELECT statement
is equal to the number of expressions in the result expression list after
substitution of * and alias.* expressions. Each result row is calculated by
evaluating the expressions in the result expression list with respect to a
single row of input data or, for aggregate queries, with respect to a group
of rows.

<ul>
  <li><p>If the SELECT statement is <b>a non-aggregate query</b>, then 
    each expression in the result expression list is evaluated for each row in
    the dataset filtered by the WHERE clause.

  <li><p>If the SELECT statement is <b>an aggregate query without a GROUP
    BY</b> clause, then each aggregate expression in the result-set is 
    evaluated once across the entire dataset. Each non-aggregate expression
    in the result-set is evaluated once for an arbitrarily selected row of
    the dataset. The same arbitrarily selected row is used for each
    non-aggregate expression. Or, if the dataset contains zero rows, then 
    each non-aggregate expression is evaluated against a row consisting
    entirely of NULL values.

   <p>The single row of result-set data created by evaluating the aggregate
    and non-aggregate expressions in the result-set forms the result of an
    aggregate query without a GROUP BY clause. An aggregate query without a
    GROUP BY clause always returns exactly one row of data, even if there are
    zero rows of input data.

  <li><p>If the SELECT statement is <b>an aggregate query with a GROUP
    BY</b> clause, then each of the expressions specified as part of the
    GROUP BY clause is evaluated for each row of the dataset. Each row
    is then assigned to a "group" based on the results; rows for which
    the results of evaluating the GROUP BY expressions are the same get
    assigned to the same group. For the purposes of grouping rows, NULL 
    values are considered equal. The usual rules for <a href="datatype3.html#collation">selecting a
    collation sequence</a> with which to compare text values apply when evaluating
    expressions in a GROUP BY clause.  The expressions in the GROUP BY clause
    do <em>not</em> have to be expressions that appear in the result. The
    expressions in a GROUP BY clause may not be aggregate expressions.

    <p>If a HAVING clause is specified, it is evaluated once for each group 
    of rows as a <a href="lang_expr.html#booleanexpr">boolean expression</a>. If the result of evaluating the
    HAVING clause is false, the group is discarded. If the HAVING clause is
    an aggregate expression, it is evaluated across all rows in the group. If
    a HAVING clause is a non-aggregate expression, it is evaluated with respect
    to an arbitrarily selected row from the group.  The HAVING expression may
    refer to values, even aggregate functions, that are not in the result.</p>

    <p>Each expression in the result-set is then evaluated once for each
    group of rows. If the expression is an aggregate expression, it is 
    evaluated across all rows in the group. Otherwise, it is evaluated against
    a single arbitrarily chosen row from within the group. If there is more
    than one non-aggregate expression in the result-set, then all such
    expressions are evaluated for the same row.

    <p>Each group of input dataset rows contributes a single row to the 
    set of result rows. Subject to filtering associated with the DISTINCT
    keyword, the number of rows returned by an aggregate query with a GROUP
    BY clause is the same as the number of groups of rows produced by applying
    the GROUP BY and HAVING clauses to the filtered input dataset.
</ul>

<p><b>4. Removal of duplicate rows (DISTINCT processing).</b>
<a name="distinct"></a>



<p>One of the ALL or DISTINCT keywords may follow the SELECT keyword in a 
simple SELECT statement. If the simple SELECT is a SELECT ALL, then the
entire set of result rows are returned by the SELECT. If neither ALL or
DISTINCT are present, then the behavior is as if ALL were specified. 
If the simple SELECT is a SELECT DISTINCT, then duplicate rows are removed
from the set of result rows before it is returned. For the purposes of
detecting duplicate rows, two NULL values are considered to be equal. The
normal rules for selecting a collation sequence to compare text values with
apply.

<a name="compound"></a>

<h3>Compound Select Statements</h3>

<p>Two or more <a href="lang_select.html#simpleselect">simple SELECT</a> statements may be connected together to form
a compound SELECT using the UNION, UNION ALL, INTERSECT or EXCEPT operator,
as shown by the following diagram:

<p><b><a href="syntaxdiagrams.html#compound-select-stmt">compound-select-stmt:</a></b>
<button id='x1301' onclick='hideorshow("x1301","x1302")'>hide</button></p>
 <blockquote id='x1302'>
 <img alt="syntax diagram compound-select-stmt" src="images/syntax/compound-select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#common-table-expression">common-table-expression:</a></b>
<button id='x1303' onclick='hideorshow("x1303","x1304")'>show</button></p>
 <blockquote id='x1304' style='display:none;'>
 <img alt="syntax diagram common-table-expression" src="images/syntax/common-table-expression.gif" />
<p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1305' onclick='hideorshow("x1305","x1306")'>show</button></p>
 <blockquote id='x1306' style='display:none;'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#compound-operator">compound-operator:</a></b>
<button id='x1307' onclick='hideorshow("x1307","x1308")'>show</button></p>
 <blockquote id='x1308' style='display:none;'>
 <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1309' onclick='hideorshow("x1309","x1310")'>show</button></p>
 <blockquote id='x1310' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1311' onclick='hideorshow("x1311","x1312")'>show</button></p>
 <blockquote id='x1312' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1313' onclick='hideorshow("x1313","x1314")'>show</button></p>
 <blockquote id='x1314' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1315' onclick='hideorshow("x1315","x1316")'>show</button></p>
 <blockquote id='x1316' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1317' onclick='hideorshow("x1317","x1318")'>show</button></p>
 <blockquote id='x1318' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
</blockquote>
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#expr">expr:</a></b>
<button id='x1319' onclick='hideorshow("x1319","x1320")'>show</button></p>
 <blockquote id='x1320' style='display:none;'>
 <img alt="syntax diagram expr" src="images/syntax/expr.gif" />
<p><b><a href="syntaxdiagrams.html#literal-value">literal-value:</a></b>
<button id='x1321' onclick='hideorshow("x1321","x1322")'>show</button></p>
 <blockquote id='x1322' style='display:none;'>
 <img alt="syntax diagram literal-value" src="images/syntax/literal-value.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#raise-function">raise-function:</a></b>
<button id='x1323' onclick='hideorshow("x1323","x1324")'>show</button></p>
 <blockquote id='x1324' style='display:none;'>
 <img alt="syntax diagram raise-function" src="images/syntax/raise-function.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1325' onclick='hideorshow("x1325","x1326")'>show</button></p>
 <blockquote id='x1326' style='display:none;'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#compound-operator">compound-operator:</a></b>
<button id='x1327' onclick='hideorshow("x1327","x1328")'>show</button></p>
 <blockquote id='x1328' style='display:none;'>
 <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1329' onclick='hideorshow("x1329","x1330")'>show</button></p>
 <blockquote id='x1330' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1331' onclick='hideorshow("x1331","x1332")'>show</button></p>
 <blockquote id='x1332' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1333' onclick='hideorshow("x1333","x1334")'>show</button></p>
 <blockquote id='x1334' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1335' onclick='hideorshow("x1335","x1336")'>show</button></p>
 <blockquote id='x1336' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1337' onclick='hideorshow("x1337","x1338")'>show</button></p>
 <blockquote id='x1338' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#type-name">type-name:</a></b>
<button id='x1339' onclick='hideorshow("x1339","x1340")'>show</button></p>
 <blockquote id='x1340' style='display:none;'>
 <img alt="syntax diagram type-name" src="images/syntax/type-name.gif" />
<p><b><a href="syntaxdiagrams.html#signed-number">signed-number:</a></b>
<button id='x1341' onclick='hideorshow("x1341","x1342")'>show</button></p>
 <blockquote id='x1342' style='display:none;'>
 <img alt="syntax diagram signed-number" src="images/syntax/signed-number.gif" />
</blockquote>
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#ordering-term">ordering-term:</a></b>
<button id='x1343' onclick='hideorshow("x1343","x1344")'>show</button></p>
 <blockquote id='x1344' style='display:none;'>
 <img alt="syntax diagram ordering-term" src="images/syntax/ordering-term.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#select-core">select-core:</a></b>
<button id='x1345' onclick='hideorshow("x1345","x1346")'>show</button></p>
 <blockquote id='x1346' style='display:none;'>
 <img alt="syntax diagram select-core" src="images/syntax/select-core.gif" />
<p><b><a href="syntaxdiagrams.html#join-clause">join-clause:</a></b>
<button id='x1347' onclick='hideorshow("x1347","x1348")'>show</button></p>
 <blockquote id='x1348' style='display:none;'>
 <img alt="syntax diagram join-clause" src="images/syntax/join-clause.gif" />
<p><b><a href="syntaxdiagrams.html#join-constraint">join-constraint:</a></b>
<button id='x1349' onclick='hideorshow("x1349","x1350")'>show</button></p>
 <blockquote id='x1350' style='display:none;'>
 <img alt="syntax diagram join-constraint" src="images/syntax/join-constraint.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#join-operator">join-operator:</a></b>
<button id='x1351' onclick='hideorshow("x1351","x1352")'>show</button></p>
 <blockquote id='x1352' style='display:none;'>
 <img alt="syntax diagram join-operator" src="images/syntax/join-operator.gif" />
</blockquote>
</blockquote>
<p><b><a href="syntaxdiagrams.html#result-column">result-column:</a></b>
<button id='x1353' onclick='hideorshow("x1353","x1354")'>show</button></p>
 <blockquote id='x1354' style='display:none;'>
 <img alt="syntax diagram result-column" src="images/syntax/result-column.gif" />
</blockquote>
<p><b><a href="syntaxdiagrams.html#table-or-subquery">table-or-subquery:</a></b>
<button id='x1355' onclick='hideorshow("x1355","x1356")'>show</button></p>
 <blockquote id='x1356' style='display:none;'>
 <img alt="syntax diagram table-or-subquery" src="images/syntax/table-or-subquery.gif" />
<p><b><a href="syntaxdiagrams.html#select-stmt">select-stmt:</a></b>
<button id='x1357' onclick='hideorshow("x1357","x1358")'>show</button></p>
 <blockquote id='x1358' style='display:none;'>
 <img alt="syntax diagram select-stmt" src="images/syntax/select-stmt.gif" />
<p><b><a href="syntaxdiagrams.html#compound-operator">compound-operator:</a></b>
<button id='x1359' onclick='hideorshow("x1359","x1360")'>show</button></p>
 <blockquote id='x1360' style='display:none;'>
 <img alt="syntax diagram compound-operator" src="images/syntax/compound-operator.gif" />
</blockquote>
</blockquote>
</blockquote>
</blockquote>
</blockquote>


<p>In a compound SELECT, all the constituent SELECTs must return the same 
number of result columns. As the components of a compound SELECT must
be simple SELECT statements, they may not contain <a href="lang_select.html#orderby">ORDER BY</a> or <a href="lang_select.html#limitoffset">LIMIT</a> clauses.
<a href="lang_select.html#orderby">ORDER BY</a> and <a href="lang_select.html#limitoffset">LIMIT</a> clauses may only occur at the end of the entire compound
SELECT, and then only if the final element of the compound is not a <a href="lang_select.html#values">VALUES</a> clause.

<p>A compound SELECT created using UNION ALL operator returns all the rows
from the SELECT to the left of the UNION ALL operator, and all the rows
from the SELECT to the right of it. The UNION operator works the same way as
UNION ALL, except that duplicate rows are removed from the final result set.
The INTERSECT operator returns the intersection of the results of the left and
right SELECTs.  The EXCEPT operator returns the subset of rows returned by the
left SELECT that are not also returned by the right-hand SELECT. Duplicate
rows are removed from the results of INTERSECT and EXCEPT operators before the
result set is returned.

<p>For the purposes of determining duplicate rows for the results of compound
SELECT operators, NULL values are considered equal to other NULL values and
distinct from all non-NULL values. The collation sequence used to compare 
two text values is determined as if the columns of the left and right-hand
SELECT statements were the left and right-hand operands of the equals (=)
operator, except that greater precedence is not assigned to a collation 
sequence specified with the postfix COLLATE operator. No affinity
transformations are applied to any values when comparing rows as part of a
compound SELECT. 

<p>When three or more simple SELECTs are connected into a compound SELECT,
they group from left to right. In other words, if "A", "B" and "C" are all
simple SELECT statements, (A op B op C) is processed as ((A op B) op C).

</p>

<a name="orderby"></a>

<h3>The ORDER BY clause</h3>

<p>If a SELECT statement that returns more than one row does not have an
ORDER BY clause, the order in which the rows are returned is undefined.
Or, if a SELECT statement does have an ORDER BY clause, then the list of
expressions attached to the ORDER BY determine the order in which rows
are returned to the user.

<p>
In a <a href="lang_select.html#compound">compound SELECT</a> statement, only the last or right-most <a href="lang_select.html#simpleselect">simple SELECT</a>
may have an ORDER BY clause. That ORDER BY clause will apply across all elements of
the compound. If the right-most element of a <a href="lang_select.html#compound">compound SELECT</a> is a <a href="lang_select.html#values">VALUES</a> clause,
then no ORDER BY clause is allowed on that statement.


<p>Rows are first sorted based on the results of
evaluating the left-most expression in the ORDER BY list, then ties are broken
by evaluating the second left-most expression and so on. The order in which
two rows for which all ORDER BY expressions evaluate to equal values are
returned is undefined. Each ORDER BY expression may be optionally followed
by one of the keywords ASC (smaller values are returned first) or DESC (larger
values are returned first). If neither ASC or DESC are specified, rows
are sorted in ascending (smaller values first) order by default.

<p>Each ORDER BY expression is processed as follows:</p>

<ol>
<li><p>If the ORDER BY expression is a constant integer K then the
expression is considered an alias for the K-th column of the result set
(columns are numbered from left to right starting with 1).

<li><p>If the ORDER BY expression is an identifier that corresponds to
the alias of one of the output columns, then the expression is considered
an alias for that column.

<li><p>Otherwise, if the ORDER BY expression is any other expression, it 
is evaluated and the returned value used to order the output rows. If
the SELECT statement is a simple SELECT, then an ORDER BY may contain any
arbitrary expressions. However, if the SELECT is a compound SELECT, then
ORDER BY expressions that are not aliases to output columns must be exactly
the same as an expression used as an output column.
</ol>

<p>For the purposes of sorting rows, values are compared in the same way
as for <a href="datatype3.html#comparisons">comparison expressions</a>. The collation sequence used to compare
two text values is determined as follows:

<ol>
  <li><p>If the ORDER BY expression is assigned a collation sequence using
  the postfix <a href="lang_expr.html#collateop">COLLATE operator</a>, then the specified collation sequence is
  used.
  <li><p>Otherwise, if the ORDER BY expression is an alias to an expression
  that has been assigned a collation sequence using the postfix 
  <a href="lang_expr.html#collateop">COLLATE operator</a>, then the collation sequence assigned to the aliased
  expression is used.
  <li><p>Otherwise, if the ORDER BY expression is a column or an alias of
  an expression that is a column, then the default collation sequence for
  the column is used. 
  <li><p>Otherwise, the <a href="datatype3.html#collation">BINARY</a> collation sequence is used.
</ol>

<p>In a <a href="lang_select.html#compound">compound SELECT</a> statement, all ORDER BY expressions are handled
as aliases for one of the result columns of the compound.
If an ORDER BY expression is not an integer alias, then SQLite searches
the left-most SELECT in the compound for a result column that matches either
the second or third rules above. If a match is found, the search stops and
the expression is handled as an alias for the result column that it has been
matched against. Otherwise, the next SELECT to the right is tried, and so on.
If no matching expression can be found in the result columns of any
constituent SELECT, it is an error. Each term of the ORDER BY clause is
processed separately and may be matched against result columns from different
SELECT statements in the compound.</p>

<a name="limitoffset"></a>

<h3>The LIMIT clause</h3>

<p>The LIMIT clause is used to place an upper bound on the number of rows
returned by the entire SELECT statement.

<p>In a <a href="lang_select.html#compound">compound SELECT</a>, only the
last or right-most <a href="lang_select.html#simpleselect">simple SELECT</a> may contain a LIMIT clause.  
In a <a href="lang_select.html#compound">compound SELECT</a>, 
the LIMIT clause applies to the entire compound, not just the final SELECT.
If the right-most <a href="lang_select.html#simpleselect">simple SELECT</a> is a <a href="lang_select.html#values">VALUES clause</a> then no LIMIT clause
is allowed.

<p>Any scalar expression may be used in the 
LIMIT clause, so long as it evaluates to an integer or a value that can be
losslessly converted to an integer. If the expression evaluates to a NULL 
value or any other value that cannot be losslessly converted to an integer, an
error is returned. If the LIMIT expression evaluates to a negative value,
then there is no upper bound on the number of rows returned. Otherwise, the
SELECT returns the first N rows of its result set only, where N is the value
that the LIMIT expression evaluates to. Or, if the SELECT statement would
return less than N rows without a LIMIT clause, then the entire result set is
returned. 

<p>The expression attached to the optional OFFSET clause that may follow a
LIMIT clause must also evaluate to an integer, or a value that can be
losslessly converted to an integer. If an expression has an OFFSET clause,
then the first M rows are omitted from the result set returned by the SELECT
statement and the next N rows are returned, where M and N are the values that
the OFFSET and LIMIT clauses evaluate to, respectively. Or, if the SELECT
would return less than M+N rows if it did not have a LIMIT clause, then the
first M rows are skipped and the remaining rows (if any) are returned. If the
OFFSET clause evaluates to a negative value, the results are the same as if it
had evaluated to zero.

<p>Instead of a separate OFFSET clause, the LIMIT clause may specify two
scalar expressions separated by a comma. In this case, the first expression
is used as the OFFSET expression and the second as the LIMIT expression.
This is counter-intuitive, as when using the OFFSET clause the second of
the two expressions is the OFFSET and the first the LIMIT. 
This reversal of the offset and limit is intentional
- it maximizes compatibility with other SQL database systems.
However, to avoid confusion, programmers are strongly encouraged to use
the form of the LIMIT clause that uses the "OFFSET" keyword and avoid
using a LIMIT clause with a comma-separated offset.

<a name="values"></a>

<h3>The VALUES clause</h3>

<p>The phrase "VALUES(<i>expr-list</i>)" means the same thing
as "SELECT <i>expr-list</i>".  The phrase
"VALUES(<i>expr-list-1</i>),...,(<i>expr-list-N</i>)" means the same
thing as "SELECT <i>expr-list-1</i> UNION ALL ... UNION ALL
SELECT <i>expr-list-N</i>".  There is no advantage to using one form
over the other.  Both forms yield the same result and both forms use
the same amount of memory and processing time.

<p>There are some restrictions on the use of a VALUES clause that are
not shown on the syntax diagrams:

<ul>
<li><p>
A VALUES clause cannot be followed by <a href="lang_select.html#orderby">ORDER BY</a> or <a href="lang_select.html#limitoffset">LIMIT</a>.

<li><p>
A VALUES clause cannot be used together with a <a href="lang_with.html">WITH</a> clause in a
<a href="lang_select.html#simpleselect">simple SELECT</a>.
</ul>


<h3>The WITH Clause</h3>

<p>SELECT statements may be optionally preceded by a single
<a href="lang_with.html">WITH clause</a> that defines one or more <a href="lang_with.html">common table expressions</a>
for use within the SELECT statement.



