Search code examples
sqloracle-databaseoracle10gora-00904

oracle 10g sql with clause compilation error


The compilation error says "mm" and "cc" is invalid identifier!

with m as (
  select instructor, 
         count(*) as c 
    from class 
group by instructor),
     mm as ( 
  select max(m.c) as cc 
    from m)
select m.instructor 
  from m 
 where m.c = mm.cc;

Solution

  • The error is because mm is the name of the Subquery Factoring (AKA CTE) instance, but as you can see:

    SELECT m.instructor 
     FROM m 
    WHERE m.c = mm.cc;
    

    You haven't declared mm as a JOIN to the m instance. Use:

    WITH m AS (
        SELECT instructor, 
               COUNT(*) as c 
          FROM CLASS
      GROUP BY instructor),
         mm AS ( 
        SELECT MAX(m.c) as cc 
          FROM m)
    SELECT m.instructor 
      FROM m
      JOIN mm ON mm.cc = m.c