Use the having keyword in Oracle to limit your grouping results

Source: Internet
Author: User

We have introduced how to useOracleAggregate functions andGroupKeyword to group the result set. A new problem arises. For example, we need to find the grouping results that meet the conditions for the column of the aggregate function. We can useWhereClause? No, we need to useHavingKeyword.

For example, we need to find out$2000The above departments. UseHavingKeyword Syntax:

Select deptno, AVG (SAL) avg_sal

From EMP

Group by deptno

Having AVG (SAL) & gt; 2000;

In this way, the correct result can be obtained.

If you are using

Select deptno, AVG (SAL) avg_sal

From EMP

Group by deptno

Where AVG (SAL)> 2000;

Will appearORA-00934.

AboutOracleOfHavingThe keyword cannot be inHavingAlias for Using Aggregate functions,For example

Select deptno, AVG (SAL) avg_sal

From EMP

Group by deptno

Having avg_sal & gt; 2000;

:ORA-00904: 'avg_sal ":Invalid identifier error.

Of courseHavingKeywords can only be used to process the results of Aggregate functions, and cannot be used in common columns.

The result of the preceding example:

SQL> select deptno, AVG (SAL) avg_sal

2 from EMP

3 group by deptno

4 having AVG (SAL)> 2000;

 

Deptno avg_sal

--------

10 2916.66667

20 2235

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.