blob: feafce1850e15810872c8665805912ad555733e8 [file]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="copyright" content="(C) Copyright 2025" />
<meta name="DC.rights.owner" content="(C) Copyright 2025" />
<meta name="DC.Type" content="concept" />
<meta name="DC.Title" content="WITH Clause" />
<meta name="DC.Relation" scheme="URI" content="../topics/impala_select.html" />
<meta name="prodname" content="Impala" />
<meta name="prodname" content="Impala" />
<meta name="version" content="Impala 3.4.x" />
<meta name="version" content="Impala 3.4.x" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="with" />
<link rel="stylesheet" type="text/css" href="../commonltr.css" />
<title>WITH Clause</title>
</head>
<body id="with">
<h1 class="title topictitle1" id="ariaid-title1">WITH Clause</h1>
<div class="body conbody">
<p class="p">
A clause that can be added before a <code class="ph codeph">SELECT</code> statement, to define aliases for complicated
expressions that are referenced multiple times within the body of the <code class="ph codeph">SELECT</code>. Similar to
<code class="ph codeph">CREATE VIEW</code>, except that the table and column names defined in the <code class="ph codeph">WITH</code>
clause do not persist after the query finishes, and do not conflict with names used in actual tables or
views. Also known as <span class="q">"subquery factoring"</span>.
</p>
<p class="p">
You can rewrite a query using subqueries to work the same as with the <code class="ph codeph">WITH</code> clause. The
purposes of the <code class="ph codeph">WITH</code> clause are:
</p>
<ul class="ul">
<li class="li">
Convenience and ease of maintenance from less repetition with the body of the query. Typically used with
queries involving <code class="ph codeph">UNION</code>, joins, or aggregation functions where the similar complicated
expressions are referenced multiple times.
</li>
<li class="li">
SQL code that is easier to read and understand by abstracting the most complex part of the query into a
separate block.
</li>
<li class="li">
Improved compatibility with SQL from other database systems that support the same clause (primarily Oracle
Database).
<div class="note note"><span class="notetitle">Note:</span>
<p class="p">
The Impala <code class="ph codeph">WITH</code> clause does not support recursive queries in the
<code class="ph codeph">WITH</code>, which is supported in some other database systems.
</p>
</div>
</li>
</ul>
<p class="p">
<strong class="ph b">Standards compliance:</strong> Introduced in
<a class="xref" href="http://en.wikipedia.org/wiki/SQL:1999" target="_blank">SQL:1999</a>.
</p>
<p class="p">
<strong class="ph b">Examples:</strong>
</p>
<pre class="pre codeblock"><code>-- Define 2 subqueries that can be referenced from the body of a longer query.
with t1 as (select 1), t2 as (select 2) insert into tab select * from t1 union all select * from t2;
-- Define one subquery at the outer level, and another at the inner level as part of the
-- initial stage of the UNION ALL query.
with t1 as (select 1) (with t2 as (select 2) select * from t2) union all select * from t1;</code></pre>
</div>
<div class="related-links">
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a class="link" href="../topics/impala_select.html">SELECT Statement</a></div>
</div>
</div></body>
</html>