Usage of group by, having, and order

Source: Internet
Author: User

1. If a query uses the grouping function, any column or expression that is not in the grouping function must be in group by; otherwise, an error occurs.
 
In the first query, deptno does not appear in group by or in the grouping function. Therefore, an error occurs. Change deptno to job. The meaning of this statement is: Find the average salary of each job by group, having clause

Oracle requires that the where clause cannot use the grouping function. In this case, we must use the having clause to complete the function.

Select job, avg (sal) from emp having avg (sal)> 1500 group by job;

When the having clause is used, the oracle system processing sequence is:

1. Group data rows first.

2. Apply the group to the grouping function.

3. It is best to display records that meet the having condition.

3. Nesting of grouping Functions

Not to mention direct
 
The statement execution sequence is:

1. Execute the where clause to find the job's qualified records.

2. Group jobs.

3. Calculate the average salary of each group based on the grouping conditions.

4. Find the largest and smallest average salaries in each group.

Related Article

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.