Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-30575][DOC] Document HAVING Clause of SELECT statement in SQL Reference #27284

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 98 additions & 1 deletion docs/sql-ref-syntax-qry-select-having.md
Expand Up @@ -18,5 +18,102 @@ license: |
See the License for the specific language governing permissions and
limitations under the License.
---
The <code>HAVING</code> clause is used to filter the results produced by
<code>GROUP BY</code> based on the specified condition. It is often used
srowen marked this conversation as resolved.
Show resolved Hide resolved
in the conjunction with a [GROUP BY](sql-ref-syntax-qry-select-groupby.html)
clause.

**This page is under construction**
### Syntax
{% highlight sql %}
HAVING boolean_expression
{% endhighlight %}

### Parameters
<dl>
<dt><code><em>boolean_expression</em></code></dt>
<dd>
Specifies any expression that evaluates to a result type <code>boolean</code>. Two or
more expressions may be combined together using the logical
operators ( <code>AND</code>, <code>OR</code> ).<br><br>

<b>Note</b><br>
The expressions specified in the <code>HAVING</code> clause can only refer to:
<ol>
<li>Constants</li>
<li>Expressions that appear in GROUP BY</li>
<li>Aggregate functions</li>
</ol>
</dd>
</dl>

### Examples
{% highlight sql %}
CREATE TABLE dealer (id INT, city STRING, car_model STRING, quantity INT);
INSERT INTO dealer VALUES (100, 'Fremont', 'Honda Civic', 10),
(100, 'Fremont', 'Honda Accord', 15),
(100, 'Fremont', 'Honda CRV', 7),
(200, 'Dublin', 'Honda Civic', 20),
(200, 'Dublin', 'Honda Accord', 10),
(200, 'Dublin', 'Honda CRV', 3),
(300, 'San Jose', 'Honda Civic', 5),
(300, 'San Jose', 'Honda Accord', 8);

-- `HAVING` clause referring to column in `GROUP BY`.
SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING city = 'Fremont';

+-------+---+
|city |sum|
+-------+---+
|Fremont|32 |
+-------+---+

-- `HAVING` clause referring to aggregate function.
SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING sum(quantity) > 15;

+-------+---+
| city|sum|
+-------+---+
| Dublin| 33|
|Fremont| 32|
+-------+---+

-- `HAVING` clause referring to aggregate function by its alias.
SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING sum > 15;

+-------+---+
| city|sum|
+-------+---+
| Dublin| 33|
|Fremont| 32|
+-------+---+

-- `HAVING` clause referring to a different aggregate function than what is present in
-- `SELECT` list.
SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING max(quantity) > 15;

+------+---+
|city |sum|
+------+---+
|Dublin|33 |
+------+---+

-- `HAVING` clause referring to constant expression.
SELECT city, sum(quantity) AS sum FROM dealer GROUP BY city HAVING 1 > 0 ORDER BY city;

+--------+---+
| city|sum|
+--------+---+
| Dublin| 33|
| Fremont| 32|
|San Jose| 13|
+--------+---+

-- `HAVING` clause without a `GROUP BY` clause.
SELECT sum(quantity) AS sum FROM dealer HAVING sum(quantity) > 10;
+---+
|sum|
+---+
| 78|
+---+

{% endhighlight %}