forked from docs/doc-exports
Reviewed-by: Wagner, Fabian <fabian.wagner@t-systems.com> Co-authored-by: Ru, Li Yi <liyiru7@huawei.com> Co-committed-by: Ru, Li Yi <liyiru7@huawei.com>
49 lines
3.6 KiB
HTML
49 lines
3.6 KiB
HTML
<a name="ddm_08_0012"></a><a name="ddm_08_0012"></a>
|
|
|
|
<h1 class="topictitle1">SELECT Subquery Syntax</h1>
|
|
<div id="body1597045079944"><div class="section" id="ddm_08_0012__section15771114685915"><h4 class="sectiontitle">Subquery as Scalar Operand</h4><p id="ddm_08_0012__p1039662451412">Example</p>
|
|
<pre class="screen" id="ddm_08_0012__screen1262053510144">SELECT (SELECT id FROM test1 where id=1);
|
|
SELECT (SELECT id FROM test2 where id=1)FROM test1;
|
|
SELECT UPPER((SELECT name FROM test1 limit 1)) FROM test2;</pre>
|
|
</div>
|
|
<div class="section" id="ddm_08_0012__section14287162151520"><h4 class="sectiontitle">Comparisons Using Subqueries</h4><p id="ddm_08_0012__p11282043517">Syntax</p>
|
|
<pre class="screen" id="ddm_08_0012__screen1890319172572">non_subquery_operand comparison_operator (subquery)
|
|
comparison_operator: = > < >= <= <> != <=> like</pre>
|
|
</div>
|
|
<p id="ddm_08_0012__p382568105419">Example</p>
|
|
<pre class="screen" id="ddm_08_0012__screen1815165619545">select name from test1 where id > (select id from test2 where id=1);
|
|
select name from test1 where id = (select id from test2 where id=1);
|
|
select id from test1 where name like (select name from test2 where id=1);</pre>
|
|
<div class="section" id="ddm_08_0012__section14597152195810"><h4 class="sectiontitle">Subqueries with ANY, IN, NOT IN, SOME,ALL,Exists,NOT Exists</h4><p id="ddm_08_0012__p8367105435810">Syntax</p>
|
|
<pre class="screen" id="ddm_08_0012__screen1271201815595">operand comparison_operator SOME (subquery)
|
|
operand comparison_operator ALL (subquery)
|
|
operand comparison_operator ANY (subquery)
|
|
operand IN (subquery)
|
|
operand not IN (subquery)
|
|
operand exists (subquery)
|
|
operand not exists (subquery)</pre>
|
|
</div>
|
|
<p id="ddm_08_0012__p1776154113594">Example</p>
|
|
<pre class="screen" id="ddm_08_0012__screen1956056704">select id from test1 where id > any (select id from test2);
|
|
select id from test1 where id > some (select id from test2);
|
|
select id from test1 where id > all (select id from test2);
|
|
select id from test1 where id in (select id from test2);
|
|
select id from test1 where id not in (select id from test2);
|
|
select id from test1 where exists (select id from test2 where id=1);
|
|
select id from test1 where not exists (select id from test2 where id=1);</pre>
|
|
<div class="section" id="ddm_08_0012__section892675411611"><h4 class="sectiontitle">Derived Tables (Subqueries in the FROM Clause)</h4><p id="ddm_08_0012__p429917571068">Syntax</p>
|
|
<pre class="screen" id="ddm_08_0012__screen265714182071">SELECT ... FROM (subquery) [AS] tbl_name ...</pre>
|
|
<p id="ddm_08_0012__p115911111713">Example</p>
|
|
<pre class="screen" id="ddm_08_0012__screen10792173519719">select id from (select id,name from test2 where id>1) a order by a.id;</pre>
|
|
</div>
|
|
<div class="section" id="ddm_08_0012__section104153553013"><h4 class="sectiontitle">Syntax Restrictions</h4><ul id="ddm_08_0012__ul12718734141018"><li id="ddm_08_0012__li1871863410107">Each derived table must have an alias.</li><li id="ddm_08_0012__li12191738161012">A derived table cannot be a correlated subquery.</li><li id="ddm_08_0012__li38921059181017">In some cases, correct results cannot be obtained using a scalar subquery. Using JOIN instead is recommended to improve query performance.</li><li id="ddm_08_0012__li1617524191119">Using subqueries in the HAVING clause and the JOIN ON condition is not supported.</li></ul>
|
|
<ul id="ddm_08_0012__ul13328552193020"><li id="ddm_08_0012__li194921336107">Row subqueries are not supported.</li></ul>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="ddm_08_0004.html">DML</a></div>
|
|
</div>
|
|
</div>
|
|
|