Files
doc-exports/docs/dli/sqlreference/dli_08_0150.html
Su, Xiaomeng be9eabe464 dli_sqlreference_20250305
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>
2025-03-25 09:06:21 +00:00

112 lines
12 KiB
HTML

<a name="dli_08_0150"></a><a name="dli_08_0150"></a>
<h1 class="topictitle1">Basic SELECT Statements</h1>
<div id="body8662426"><div class="section" id="dli_08_0150__en-us_topic_0093946751_s64ba781c7b6f437ea6ea93fb5eeb017a"><h4 class="sectiontitle">Function</h4><p id="dli_08_0150__en-us_topic_0093946751_af7983162fc32438d819f21191736d15d">This statement is a basic query statement and is used to return the query results.</p>
</div>
<div class="section" id="dli_08_0150__en-us_topic_0093946751_s452ed7223e30489e8c84908d006db5f1"><h4 class="sectiontitle">Syntax</h4><div class="codecoloring" codetype="Sql" id="dli_08_0150__screen84001050887"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span>
<span class="normal">6</span>
<span class="normal">7</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="p">[</span><span class="k">WHERE</span><span class="w"> </span><span class="n">where_condition</span><span class="p">]</span>
<span class="w"> </span><span class="p">[</span><span class="k">GROUP</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">col_name_list</span><span class="p">]</span>
<span class="w"> </span><span class="p">[</span><span class="k">ORDER</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">col_name_list</span><span class="p">][</span><span class="k">ASC</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="k">DESC</span><span class="p">]</span>
<span class="w"> </span><span class="p">[</span><span class="k">CLUSTER</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">col_name_list</span><span class="w"> </span><span class="o">|</span><span class="w"> </span><span class="n">DISTRIBUTE</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">col_name_list</span><span class="p">]</span>
<span class="w"> </span><span class="p">[</span><span class="n">SORT</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">col_name_list</span><span class="p">]]</span>
<span class="w"> </span><span class="p">[</span><span class="k">LIMIT</span><span class="w"> </span><span class="nb">number</span><span class="p">];</span>
</pre></div></td></tr></table></div>
</div>
</div>
<div class="section" id="dli_08_0150__en-us_topic_0093946751_s022cdf3e3b794ef6ab379c57a6312479"><h4 class="sectiontitle">Keywords</h4>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="dli_08_0150__en-us_topic_0114776214_en-us_topic_0103157089_t2fa8597e0dde4884bd6412ac207ebea8" frame="border" border="1" rules="all"><caption><b>Table 1 </b>SELECT keywords</caption><thead align="left"><tr id="dli_08_0150__en-us_topic_0114776214_en-us_topic_0103157089_r16c75fd8fdbf41c79f9aee82b032351c"><th align="left" class="cellrowborder" valign="top" width="30%" id="mcps1.3.3.2.2.3.1.1"><p id="dli_08_0150__en-us_topic_0114776214_en-us_topic_0103157089_a9dd69379a3f54904a9c510fb817659ad"><strong id="dli_08_0150__en-us_topic_0114776214_en-us_topic_0103157089_aaf3d8bb717c048e79cca4e0ae1dd34e7">Parameter</strong></p>
</th>
<th align="left" class="cellrowborder" valign="top" width="70%" id="mcps1.3.3.2.2.3.1.2"><p id="dli_08_0150__en-us_topic_0114776214_en-us_topic_0103157089_a7ede0d62539542baa7e367cf902bf2b2"><strong id="dli_08_0150__en-us_topic_0114776214_en-us_topic_0103157089_a704da68912af434baebe5e3bda073b95">Description</strong></p>
</th>
</tr>
</thead>
<tbody><tr id="dli_08_0150__en-us_topic_0114776214_en-us_topic_0103157089_r8ab7399810eb42b7abf51ae705c6cf83"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__en-us_topic_0114776214_en-us_topic_0103157089_p3113411312149">ALL</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p4239851171820">The keyword <strong id="dli_08_0150__b96531415692">ALL</strong> is used to return all matching rows from a database, including duplicates. It can only be followed by an asterisk (*); otherwise, an error will occur.</p>
<p id="dli_08_0150__p223945118185">This is the default behavior in SQL statements and is typically not written explicitly. If neither <strong id="dli_08_0150__b23742011171419">ALL</strong> nor <strong id="dli_08_0150__b79941315181419">DISTINCT</strong> is specified, the query result will include all rows, even if they are duplicates.</p>
</td>
</tr>
<tr id="dli_08_0150__row650614376147"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p25071137191410">DISTINCT</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p8508183731414">When the <strong id="dli_08_0150__b097023420146">DISTINCT</strong> keyword is used in a SELECT statement, the system removes duplicate data from the query results to ensure uniqueness.</p>
</td>
</tr>
<tr id="dli_08_0150__row11272771618"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p1212142715161">WHERE</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p21382741613">Specifies the filter criteria for a query. Arithmetic operators, relational operators, and logical operators are supported.</p>
</td>
</tr>
<tr id="dli_08_0150__row18543810498"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p05203817493">where_condition</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p45103817491">Filter criteria.</p>
</td>
</tr>
<tr id="dli_08_0150__row16260934131619"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p7260203451611">GROUP BY</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p1260173416161">Specifies the grouping field. Single-field grouping and multi-field grouping are supported.</p>
</td>
</tr>
<tr id="dli_08_0150__row1316202210493"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p1316162212493">col_name_list</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p1517142274914">Field list</p>
</td>
</tr>
<tr id="dli_08_0150__row1556101424211"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p95566145426">ORDER BY</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p15565141424">Sort the query results.</p>
</td>
</tr>
<tr id="dli_08_0150__row96957234714"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p186956218477">ASC/DESC</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p269522114717">ASC sorts from the lowest value to the highest value. DESC sorts from the highest value to the lowest value. ASC is the default sort order.</p>
</td>
</tr>
<tr id="dli_08_0150__row469672154710"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p126963214472">CLUSTER BY</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p12696112114718">CLUSTER BY is used to bucket the table according to the bucketing fields and then sort within the bucketed table. If the field of DISTRIBUTE BY is the same as the field of SORT BY and the sorting is in descending order, the combination of DISTRIBUTE BY and SORT BY achieves the same function as CLUSTER BY. </p>
</td>
</tr>
<tr id="dli_08_0150__row1069610214474"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p116961528472">DISTRIBUTE BY</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p166962212472">Specifies the bucketing fields without sorting the table.</p>
</td>
</tr>
<tr id="dli_08_0150__row06965284719"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p869618216471">SORT BY</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p1969613219475">The objects will be sorted in the bucket.</p>
</td>
</tr>
<tr id="dli_08_0150__row1569618210474"><td class="cellrowborder" valign="top" width="30%" headers="mcps1.3.3.2.2.3.1.1 "><p id="dli_08_0150__p46961621479">LIMIT</p>
</td>
<td class="cellrowborder" valign="top" width="70%" headers="mcps1.3.3.2.2.3.1.2 "><p id="dli_08_0150__p1269617224713">LIMIT is used to limit the query results. Only INT type is supported by the <strong id="dli_08_0150__b27521451092120">number</strong> parameter.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="dli_08_0150__en-us_topic_0093946751_sd02019d1afd4479d811d4fc5cb2be648"><h4 class="sectiontitle">Precautions</h4><ul id="dli_08_0150__ul73797101610"><li id="dli_08_0150__li1137915102615">The table to be queried must already exist, or an error message will be displayed.</li><li id="dli_08_0150__li63798102066">When submitting SQL statements to display binary data on the DLI management console, the binary data is converted to Base64.</li></ul>
</div>
<div class="section" id="dli_08_0150__en-us_topic_0093946751_s18c3289e82d94522b31bbdc7d2aae3ed"><h4 class="sectiontitle">Example</h4><p id="dli_08_0150__en-us_topic_0093946751_ab3da007974a548888c2e36ddd6e01c43">To filter the record, in which the name is Mike, from the <strong id="dli_08_0150__en-us_topic_0093946751_b842352706163839">student</strong> table and sort the results in ascending order of score, run the following statement:</p>
<div class="codecoloring" codetype="Sql" id="dli_08_0150__screen981512581916"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span></pre></div></td><td class="code"><div><pre><span></span><span class="k">SELECT</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="k">FROM</span><span class="w"> </span><span class="n">student</span>
<span class="w"> </span><span class="k">WHERE</span><span class="w"> </span><span class="n">name</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s1">'Mike'</span>
<span class="w"> </span><span class="k">ORDER</span><span class="w"> </span><span class="k">BY</span><span class="w"> </span><span class="n">score</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_0221.html">Spark SQL Syntax Reference</a></div>
</div>
</div>