Oracle's grouping function nesting and table joins

Source: Internet
Author: User
Tags joins

--1 Data Environment Preparation

Emp,dept table below for Scott users

--2 requirements: The highest average wage department number, department name, department average salary

Select D.deptno,d.dname,e.sal
From
Select AVG (SAL) Sal,deptno
From EMP E
GROUP BY Deptno
Have avg (sal) = (select Max (SAL) from EMP Group by DEPTNO)) E
Left JOIN Dept D
On E.deptno=d.deptno;

Some people say, red bold section of superfluous, red part can be written in the following form

Select Max (SAL) Sal,deptno
From EMP E
Group BY Deptno;

Note, however, that grouping functions can be nested, but it is not possible to have column names for grouping conditions when the group functions are nested. So this statement will be a direct error .

In other words, once nested grouping functions are used here, Deptno cannot appear on the column.

Well, someone said, I wrote this.

Select Max (sal) Sal
From EMP E
Group BY Deptno;

This is no longer an error, but the DEPTNO as an association condition is gone.

So for a nested grouping function, and use the table to join, you can write a subquery, sub-query inside the nested grouping function.

Oracle's grouping function nesting and table joins

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.