doc-exports/docs/dli/sqlreference/dli_08_0175.html
Su, Xiaomeng 04d4597cf3 dli_sqlreference_0511_version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
Co-committed-by: Su, Xiaomeng <suxiaomeng1@huawei.com>
2023-11-02 14:34:08 +00:00

33 lines
5.4 KiB
HTML

<a name="dli_08_0175"></a><a name="dli_08_0175"></a>
<h1 class="topictitle1">Subquery Nested by WHERE</h1>
<div id="body8662426"><div class="section" id="dli_08_0175__en-us_topic_0093946759_s17305bae692245a49e5cc2a0e423217a"><h4 class="sectiontitle">Function</h4><p id="dli_08_0175__en-us_topic_0093946759_a601d015fe6c54709a8473ed8653e1665">Subqueries are nested in the WHERE clause, and the subquery result is used as the filtering condition.</p>
</div>
<div class="section" id="dli_08_0175__en-us_topic_0093946759_s12f545e267fe4f06bb322def53ddbe19"><h4 class="sectiontitle">Syntax</h4><div class="codecoloring" codetype="Sql" id="dli_08_0175__screen039552713713"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="p">[</span><span class="k">ALL</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">DISTINCT</span><span class="p">]</span><span class="w"> </span><span class="n">attr_expr_list</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">table_reference</span>
<span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="err">{</span><span class="n">col_name</span><span class="w"> </span><span class="k">operator</span><span class="w"> </span><span class="p">(</span><span class="n">sub_query</span><span class="p">)</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="p">[</span><span class="k">NOT</span><span class="p">]</span><span class="w"> </span><span class="k">EXISTS</span><span class="w"> </span><span class="n">sub_query</span><span class="err">}</span><span class="p">;</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="dli_08_0175__en-us_topic_0093946759_sdc8ab9b85e8f42c1975f84352960ef7d"><h4 class="sectiontitle">Keyword</h4><ul id="dli_08_0175__en-us_topic_0093946759_ua63a7ffceee24401bb80034eae63854f"><li id="dli_08_0175__en-us_topic_0093946759_l6cff2b098d5742a689e74ca17586628d">All is used to return repeated rows. By default, all repeated rows are returned. It is followed by asterisks (*) only. Otherwise, an error will occur.</li><li id="dli_08_0175__en-us_topic_0093946759_l6f5797e26b53483283dd3295ba5dfd42">DISTINCT is used to remove the repeated line from the result.</li><li id="dli_08_0175__en-us_topic_0093946759_l81cdcc2f9fa74f91bff136da93799909">The subquery results are used as the filter condition in the subquery nested by WHERE. </li><li id="dli_08_0175__en-us_topic_0093946759_lb6d172103c234cad939026f11b7c47cc">The operator includes the equation and inequation operators, and IN, NOT IN, EXISTS, and NOT EXISTS operators. <ul id="dli_08_0175__en-us_topic_0093946759_uac4a54732b1f46aaac66fbd4b7ae1d55"><li id="dli_08_0175__en-us_topic_0093946759_lde9c0c3f42b34cf48b425eceaf6accc4">If the operator is IN or NOT IN, the returned records are in a single column. </li><li id="dli_08_0175__en-us_topic_0093946759_lf2e4d92bed0b462582d8f3871a6925e5">If the operator is EXISTS or NOT EXISTS, the subquery must contain WHERE. If any a field in the subquery is the same as that in the external query, add the table name before the field in the subquery. </li></ul>
</li></ul>
</div>
<div class="section" id="dli_08_0175__en-us_topic_0093946759_sdf3b5b6fe75449608205cbd9ff72c09c"><h4 class="sectiontitle">Precautions</h4><p id="dli_08_0175__en-us_topic_0093946759_en-us_topic_0047959901_p131579210465">The to-be-queried table must exist. If this statement is used to query a table that does not exist, an error is reported. </p>
</div>
<div class="section" id="dli_08_0175__en-us_topic_0093946759_s2a1e581e8ebb40978c41481c348bf145"><h4 class="sectiontitle">Example</h4><p id="dli_08_0175__en-us_topic_0093946759_a72d5e6f27e6e492494d7c540973236a4">To query the courseId of Biology from the course_info table, and then query the student name matched the courseId from the student_info table, run the following statement: </p>
<div class="codecoloring" codetype="Sql" id="dli_08_0175__screen10146721163817"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">student_info</span>
<span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">courseId</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="k">SELECT</span><span class="w"> </span><span class="n">courseId</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">course_info</span><span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">courseName</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'Biology'</span><span class="p">);</span>
</pre></div></td></tr></table></div>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="dli_08_0174.html">Subquery</a></div>
</div>
</div>