3

I'm on Oracle 11g and rather perplexed how the 3rd query can even compile below. Shouldn't this throw an ORA-00937 error? Can anyone else reproduce this or explain how or why Oracle is ignoring the lack of a GROUP BY clause in the inline view? (it does throw on error just running the subquery by itself though)

DROP TABLE ZZZ_DELETE_ME;
CREATE TABLE ZZZ_DELETE_ME
(
contract NUMBER(6),
lives INTEGER
 );

 INSERT INTO ZZZ_DELETE_ME (contract,lives) VALUES (123456,100);
 INSERT INTO ZZZ_DELETE_ME (contract,lives) VALUES (123456,50);
 INSERT INTO ZZZ_DELETE_ME (contract,lives) VALUES (123457,100);
 INSERT INTO ZZZ_DELETE_ME (contract,lives) VALUES (123457,50);
 INSERT INTO ZZZ_DELETE_ME (contract,lives) VALUES (123458,100);
 INSERT INTO ZZZ_DELETE_ME (contract,lives) VALUES (123458,50);
 INSERT INTO ZZZ_DELETE_ME (contract,lives) VALUES (123459,100);
 INSERT INTO ZZZ_DELETE_ME (contract,lives) VALUES (123459,50);

 -- query 1 returns 100 for each record (which makes sense)
 SELECT contract, SUM(MAX_LIVES) TOTAL_LIVES
 FROM 
     (
     SELECT contract, MAX(lives) MAX_LIVES
     FROM ZZZ_DELETE_ME 
     GROUP BY contract
     )
 GROUP BY contract;

 -- query 2 returns 400 (which makes sense)
 SELECT SUM(MAX_LIVES) TOTAL_LIVES
 FROM 
     (
     SELECT contract, MAX(lives) MAX_LIVES
     FROM ZZZ_DELETE_ME 
     GROUP BY contract
     );

 -- query 3 returns 100 (but why?  Shouldn't this throw an error?)
 SELECT SUM(MAX_LIVES) TOTAL_LIVES
 FROM 
     (
     SELECT contract, MAX(lives) MAX_LIVES
     FROM ZZZ_DELETE_ME 
     -- THERE'S NO GROUP BY HERE!
     );       
3
  • Query 3 is indeed invalid. Should raise an error!
    – jarlh
    May 23, 2018 at 18:11
  • 1
    You're right, it does seem to work on 11g, but it fails in 12.1.0.2. I'd guess that since contract isn't referenced in the outer query, it's being silently removed by the compiler. sqlfiddle.com/#!4/532706/1/0
    – kfinity
    May 23, 2018 at 18:16
  • @kfinity . . . That is a good explanation. Sounds like a bug to me. May 23, 2018 at 18:22

2 Answers 2

2

This was Oracle bug 5520732, fixed in release 11.2. If you have an Oracle Support account, you can read all about it.

To confirm, you can change your query to

SELECT SUM(MAX_LIVES) TOTAL_LIVES
FROM 
   (
   SELECT /*+ NO_MERGE */ contract, MAX(lives) MAX_LIVES
   FROM ZZZ_DELETE_ME 
   -- THERE'S NO GROUP BY HERE!
   );   

... and you should get the expected error.

0

Oracle 12c produces ORA-00937 in that case, I've tested.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.