1
SELECT MAX(AVG(SYSDATE - inv_date)) FROM invoice;

What's wrong with this query?

Avg returns single value no Max requires a group to work on so it dosent execute and give error? Please explain working It's a quiz question according to which it won't execute I want to know the reason why it dosent execute I can't figure it out nested aggregate functions are allowed right? enter image description here

1 Answer 1

3

Oracle allows nested aggregation functions (see the documentation).

However, it requires a GROUP BY. So this is allowed:

SELECT MAX(AVG(SYSDATE - inv_date))
FROM invoice
GROUP BY Cust_ID;

Basically, this is a short-cut for:

SELECT MAX(x)
FROM (SELECT AVG(SYSDATE - inv_date) as x
      FROM invoice
       GROUP BY Cust_Id
     ) i;

In your case, though, there is no GROUP BY. Oracle doesn't allow nested GROUP BY without the GROUP BY.

And if you are curious, I'm not a fan of this extended functionality. I don't see that it actually solves a problem.

Sign up to request clarification or add additional context in comments.

4 Comments

in the quiz they said this won't execute or throw error . I want to know the reason why it throws error. SELECT MAX(AVG(SYSDATE - inv_date)) FROM invoice;
@MushkanTisekar . . . I elaborated. I hadn't tried a nested GROUP BY without a GROUP BY. Seems a bit odd to me that it doesn't work, but it generates a syntax error without the GROUP BY.
By the way could u tell me about the "FROM(.....) i " can we use a variable like this ? U have used in your explanation query . I am a beginner in SQL so I don't know many things.
@MushkanTisekar . . . The is properly called a table alias.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.